class OffsiteLink < ActiveRecord::Base
  belongs_to :parent, polymorphic: true
  validates :title, :summary, :link_type, :url, presence: true
  validate :url_is_govuk
  validates :link_type, presence: true, inclusion: {in: %w{alert blog_post campaign careers service}}

  def url_is_govuk
    begin
      host = URI.parse(url).host
      split_host = host.split(".") if host
      if !host || split_host[split_host.length - 1] != "uk" || split_host[split_host.length - 2] != "gov"
        errors.add(:base, "Please enter a valid GOV.UK URL, such as https://www.gov.uk/jobsearch")
      end
    rescue URI::InvalidURIError
      errors.add(:base, "Please enter a valid URL, such as https://www.gov.uk/jobsearch")
    end
  end

  def humanized_link_type
    link_type.humanize
  end

  def to_s
    title
  end
end

class OffsiteLink < ActiveRecord::Base
  belongs_to :parent, polymorphic: true
  validates :title, :summary, :link_type, :url, presence: true, length: { maximum: 255 }
  validate :url_is_govuk
  validates :link_type, presence: true, inclusion: {in: %w{alert blog_post campaign careers service}}

  def url_is_govuk
    begin
      host = URI.parse(url).host
      split_host = host.split(".") if host
      if !host || split_host[split_host.length - 1] != "uk" || split_host[split_host.length - 2] != "gov"
        errors.add(:base, "Please enter a valid GOV.UK URL, such as https://www.gov.uk/jobsearch")
      end
    rescue URI::InvalidURIError
      errors.add(:base, "Please enter a valid URL, such as https://www.gov.uk/jobsearch")
    end
  end

  def humanized_link_type
    link_type.humanize
  end

  def to_s
    title
  end
end

Validate the length of fields in Offsite Links
Validate the length of fields in Offsite Links

Rather than letting the database raise an error (now that we have a MySQL
version which doesn't silently truncate), let's validate it in the model and
show the user a helpful validation message, not a 5xx error page.
