# A Url represents a page that can have Snapshots taken on it.
class Url < ActiveRecord::Base
  validates_format_of   :address, with: %r[\Ahttps?://.+]

  belongs_to :project
  validates_presence_of :project

  has_many :snapshots

  default_scope { order(:address) }

  # @return [String]
  def to_s
    address
  end

  def title
    accepted_snapshot = snapshots.where('accepted_at is not null').first
    if accepted_snapshot.try(:title)
      accepted_snapshot.title
    else
      simplified_address
    end
  end

  # @return [Snapshot]
  def baseline(viewport)
    snapshots
      .order('accepted_at DESC')
      .where(viewport: viewport)
      .where('accepted_at IS NOT NULL')
      .first
  end

  def simplified_address
    address.gsub(%r[(?:\Ahttp://|/\Z)], '')
  end

  # Get the two latest snapshots
  #
  # @return [Hash] (at most) two snapshots grouped by viewport
  def last_snapshots_by_viewport
    project.viewports.reduce({}) do |hash, viewport|
      hash[viewport] = snapshots.where(viewport_id: viewport).first(2)
      hash
    end
  end
end

# A Url represents a page that can have Snapshots taken on it.
class Url < ActiveRecord::Base
  validates :address, format: { with: %r[\Ahttps?://.+] }

  belongs_to :project
  validates :project, presence: true

  has_many :snapshots

  default_scope { order(:address) }

  # @return [String]
  def to_s
    address
  end

  def title
    accepted_snapshot = snapshots.where('accepted_at is not null').first
    if accepted_snapshot.try(:title)
      accepted_snapshot.title
    else
      simplified_address
    end
  end

  # @return [Snapshot]
  def baseline(viewport)
    snapshots
      .order('accepted_at DESC')
      .where(viewport: viewport)
      .where('accepted_at IS NOT NULL')
      .first
  end

  def simplified_address
    address.gsub(%r[(?:\Ahttp://|/\Z)], '')
  end

  # Get the two latest snapshots
  #
  # @return [Hash] (at most) two snapshots grouped by viewport
  def last_snapshots_by_viewport
    project.viewports.reduce({}) do |hash, viewport|
      hash[viewport] = snapshots.where(viewport_id: viewport).first(2)
      hash
    end
  end
end

Use the new "sexy" validations for Url
Use the new "sexy" validations for Url

As pointed out by Rubocop.

Change-Id: If825dd9df3496c49c4bd64671ce429b323f99da8
