Sha256: 7c276654fa8eff2bb02dd380bf27a8232414b1fc56651cc2922d7b75d381f8c6

Contents?: true

Size: 1.26 KB

Versions: 12

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
  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!
      self.path_from.downcase!

      return true
    end


end




Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
ants-0.3.3 app/models/redirect.rb
ants-0.3.2 app/models/redirect.rb
ants-0.3.1 app/models/redirect.rb
ants-0.3.0 app/models/redirect.rb
ants-0.2.8 app/models/redirect.rb
ants-0.2.7 app/models/redirect.rb
ants-0.2.6 app/models/redirect.rb
ants-0.2.5 app/models/redirect.rb
ants-0.2.4 app/models/redirect.rb
ants-0.2.3 app/models/redirect.rb
ants-0.2.2 app/models/redirect.rb
ants-0.2.1 app/models/redirect.rb