Module: PermalinkFu::ClassMethods
- Defined in:
- lib/permalink_fu.rb
Instance Method Summary collapse
-
#has_permalink(*args) ⇒ Object
Specifies the given field(s) as a permalink, meaning it is passed through PermalinkFu.escape and set to the permalink_field.
Instance Method Details
#has_permalink(*args) ⇒ Object
Specifies the given field(s) as a permalink, meaning it is passed through PermalinkFu.escape and set to the permalink_field. This is done
class Foo < ActiveRecord::Base
# stores permalink form of #title to the #permalink attribute
has_permalink :title
# stores a permalink form of "#{category}-#{title}" to the #permalink attribute
has_permalink [:category, :title]
# stores permalink form of #title to the #category_permalink attribute
has_permalink [:category, :title], :as => :category_permalink
# add a scope
has_permalink :title, :scope => :blog_id
# add a scope and specify the permalink field name
has_permalink :title, :as => :slug, :scope => :blog_id
end
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/permalink_fu.rb', line 50 def has_permalink(*args) = args. attr_names = args.first || :to_s self.permalink_attributes = Array(attr_names) self.permalink_field = (.delete(:as) || 'permalink').to_s self. = before_validation :create_unique_permalink evaluate_attribute_method permalink_field, "def #{self.permalink_field}=(new_value);write_attribute(:#{self.permalink_field}, PermalinkFu.escape(new_value));end", "#{self.permalink_field}=" extend PermalinkFinders case [:param] when false # nothing when :permalink include ToParam else include ToParamWithID end end |