module Humpyard::ActiveRecord::Has::TitleForUrl::ClassMethods

Public Instance Methods

find_by_title_for_url(url, options = {}) click to toggle source
# 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