Sha256: ab64da8e8a9e168a54efbf5efab8752e51bd0cfdfbaaf3df273b870157f062f7

Contents?: true

Size: 1.26 KB

Versions: 6

Compression:

Stored size: 1.26 KB

Contents

class Redirect
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Search
  include Ants::Id

  ## Attributes
  field :path_from, type: String
  field :path_to,   type: String

  ## Validations
  validates :path_from, presence: true
  validates :path_to,   presence: true

  ## Search
  search_in :path_from, :path_to

  ## Scope
  default_scope -> { asc(:path_from) }

  ## Indexes
  index({ path_from: 1 })

  ## Callbacks
  after_validation :downcase_from_path!

  ## Helpers
  def _list_item_title
    "#{path_from} → #{path_to}"
  end

  # def _list_item_subtitle
  #   'Created ' + ActionController::Base.helpers.time_ago_in_words(created_at) + ' ago'
  # end

  ## Class Methods
  def self.match(request)
    # request.fullpath includes parameters and leading / so
    # add ending slash (Rails skips it) as alternative option
    fullpath      = request.fullpath.downcase
    fullpath_alt1 = fullpath.gsub('?', '/?')

    # decode URL to UTF string, e.g. %C3%A9 => é
    fullpath_alt2 = URI.unescape(fullpath)
    fullpath_alt3 = URI.unescape(fullpath_alt1)

    self.where(:path_from.in => [ fullpath, fullpath_alt1, fullpath_alt2, fullpath_alt3 ]).first
  end

  private

  def downcase_from_path!
    path_from.strip!.downcase!

    return true
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ants-0.3.14 app/models/redirect.rb
ants-0.3.13 app/models/redirect.rb
ants-0.3.12 app/models/redirect.rb
ants-0.3.11 app/models/redirect.rb
ants-0.3.10 app/models/redirect.rb
ants-0.3.9 app/models/redirect.rb