Module: Rails::Permalink::ClassMethods
- Defined in:
- lib/rails-permalink.rb
Instance Method Summary collapse
-
#permalink(field_name, permalink_field_name = 'permalink') ⇒ Object
Specifies the given field as using permalink.
Instance Method Details
#permalink(field_name, permalink_field_name = 'permalink') ⇒ Object
Specifies the given field as using permalink.
class Foo < ActiveRecord::Base
# stores the permalink version of title on permalink
permalink :title
# stores the permalink version of title on slug
permalink :title, :slug
end
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rails-permalink.rb', line 22 def permalink(field_name, permalink_field_name = 'permalink') before_save do |record| if (record.send(permalink_field_name).blank?) || (record.send(field_name) && record.send(permalink_field_name).nil?) record.send "#{permalink_field_name}=", record.send(field_name).parameterize end end define_method "to_param" do send(permalink_field_name) end end |