Module: Humpyard::ActiveRecord::Has::TitleForUrl::ClassMethods

Defined in:
lib/humpyard/active_record/has/title_for_url.rb

Instance Method Summary collapse

Instance Method Details

#find_by_title_for_url(url, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/humpyard/active_record/has/title_for_url.rb', line 11

def find_by_title_for_url(url, options = {})
  options[:locale] ||= ::I18n.locale

  unless Humpyard::config.locales.include? options[:locale].to_sym
    options[:locale] = Humpyard::config.locales.first
  end
  
  if options[:skip_fallbacks]
    locales = [options[:locale].to_sym]
  else
    locales = [options[:locale].to_sym] + (Humpyard::config.locales.map{|l| l.to_sym} - [options[:locale].to_sym])
  end
  
  locales.each do |l|
    #t = self.with_locale(l){find_first_by_translated_attr_and_locales(:title_for_url, url)}
    t = self.translation_class.where('locale = ? AND title_for_url = ?', l.to_s, url.to_s).first
    
    if t
      return self.find(t["#{self.name.underscore.gsub('/', '_')}_id"])
    end
  end
  
  nil
end