module Humpyard::ActiveRecord::Has::TitleForUrl

Public Class Methods

included(base) click to toggle source
  # File lib/humpyard/active_record/has/title_for_url.rb
5 def self.included(base)
6   base.extend ClassMethods
7   base.before_save :assign_title_for_url         
8 end

Public Instance Methods

query_title_for_url(locale = I18n.locale) click to toggle source
   # File lib/humpyard/active_record/has/title_for_url.rb
37 def query_title_for_url(locale = I18n.locale)
38   given = translations.all
39   ([locale.to_sym] + (Humpyard::config.locales.map{|l| l.to_sym} - [locale.to_sym])).each do |l|
40     t = given.select{|tx| tx.locale.to_sym == l}.first
41     return t.title_for_url if t and not t.title_for_url.blank?
42   end    
43   nil 
44 end
suggested_title_for_url(locale = I18n.locale) click to toggle source
   # File lib/humpyard/active_record/has/title_for_url.rb
46 def suggested_title_for_url(locale = I18n.locale)
47   return nil if title.blank?
48   
49   title_for_url = (self.title(locale) ? self.title(locale) : self.title).parameterize('_').to_s
50   
51   # Check if parameterized totally failed
52   if title_for_url == ''
53     title_for_url = CGI::escape(self.title.gsub(/[a-z0-9\-_\x00-\x7F]+/, '_'))
54   end 
55   
56   while obj = self.class.find_by_title_for_url(title_for_url, :skip_fallbacks => true, :locale => locale) and obj.id != self.id do
57     title_for_url += '_'
58   end
59   return title_for_url
60 end

Protected Instance Methods

assign_title_for_url() click to toggle source
   # File lib/humpyard/active_record/has/title_for_url.rb
63 def assign_title_for_url
64   self.title_for_url = suggested_title_for_url
65 end