class ActivePermalink::Querying::PermalinkLocator

Attributes

model[R]
options[R]

Public Class Methods

new(model, options) click to toggle source
# File lib/active_permalink/querying.rb, line 42
def initialize(model, options)
  @model   = model
  @options = options
end

Public Instance Methods

locate(*args) click to toggle source
# File lib/active_permalink/querying.rb, line 52
def locate(*args)
  find_record(:find_by, *args)
end
locate!(*args) click to toggle source
# File lib/active_permalink/querying.rb, line 56
def locate!(*args)
  find_record(:find_by!, *args)
end
scope(value, locale: nil) click to toggle source
# File lib/active_permalink/querying.rb, line 47
def scope(value, locale: nil)
  params = localize(slug: value, locale: locale)
  model.joins(:permalinks).where(permalinks: params)
end

Private Instance Methods

find_record(method, value, locale: nil) click to toggle source
# File lib/active_permalink/querying.rb, line 75
def find_record(method, value, locale: nil)
  params = localize(slug: value, locale: locale)
  record = model.joins(:permalinks).send(method, permalinks: params)

  record.found_by_slug = value if record.present?
  record
end
locale_column() click to toggle source
# File lib/active_permalink/querying.rb, line 66
def locale_column
  options[:locale_column]
end
localize(locale: nil, **params) click to toggle source
# File lib/active_permalink/querying.rb, line 70
def localize(locale: nil, **params)
  params[locale_column] = (locale || I18n.locale) if localized?
  params
end
localized?() click to toggle source
# File lib/active_permalink/querying.rb, line 62
def localized?
  options[:localized].present?
end