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

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

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
# File 'lib/humpyard/active_record/has/title_for_url.rb', line 5

def self.included(base)
  base.extend ClassMethods
  base.before_save :assign_title_for_url         
end

Instance Method Details

#query_title_for_url(locale = I18n.locale) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/humpyard/active_record/has/title_for_url.rb', line 37

def query_title_for_url(locale = I18n.locale)
  given = translations.all
  ([locale.to_sym] + (Humpyard::config.locales.map{|l| l.to_sym} - [locale.to_sym])).each do |l|
    t = given.select{|tx| tx.locale.to_sym == l}.first
    return t.title_for_url if t and not t.title_for_url.blank?
  end    
  nil 
end

#suggested_title_for_url(locale = I18n.locale) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/humpyard/active_record/has/title_for_url.rb', line 46

def suggested_title_for_url(locale = I18n.locale)
  return nil if title.blank?
  
  title_for_url = (self.title(locale) ? self.title(locale) : self.title).parameterize('_').to_s
  
  # Check if parameterized totally failed
  if title_for_url == ''
    title_for_url = CGI::escape(self.title.gsub(/[a-z0-9\-_\x00-\x7F]+/, '_'))
  end 
  
  while obj = self.class.find_by_title_for_url(title_for_url, :skip_fallbacks => true, :locale => locale) and obj.id != self.id do
    title_for_url += '_'
  end
  return title_for_url
end