module Humpyard::ActiveRecord::Has::TitleForUrl

Public Class Methods

included(base) click to toggle source
# 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

Public Instance Methods

query_title_for_url(locale = I18n.locale) click to toggle source
# 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) click to toggle source
# 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

Protected Instance Methods

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