Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/doesprettyurls.rb
Class Method Summary collapse
-
.doesprettyurls(opts = {}) ⇒ Object
Call this method within your class to get fancy URLs.
-
.from_param(param) ⇒ Object
We need to help ActiveRecord go back the other way:.
Instance Method Summary collapse
-
#to_param ⇒ Object
Provide a mechanism to automagically “slugify” URL parameters.
Class Method Details
.doesprettyurls(opts = {}) ⇒ Object
Call this method within your class to get fancy URLs
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/doesprettyurls.rb', line 27 def self.doesprettyurls(opts={}) self.instance_eval do cattr_accessor :pretty_param_attribute cattr_accessor :pretty_param_proc cattr_accessor :pretty_param_reverse_proc # Typical use case is to send a column/attribute name: @@pretty_param_attribute = opts[:attribute] || :name # A Proc may also be sent for realtime URL beautification: if opts[:proc] @@pretty_param_proc = opts[:proc] end end end |
.from_param(param) ⇒ Object
We need to help ActiveRecord go back the other way:
55 56 57 |
# File 'lib/doesprettyurls.rb', line 55 def self.from_param(param) find(param.to_i) end |
Instance Method Details
#to_param ⇒ Object
Provide a mechanism to automagically “slugify” URL parameters
45 46 47 48 49 50 51 52 |
# File 'lib/doesprettyurls.rb', line 45 def to_param slug_source = if pretty_param_proc pretty_param_proc.call(self) else self.send(pretty_param_attribute) end "#{id}-#{slug_source.downcase.gsub("'", "").gsub(/[^a-z1-9]+/i, "-")}" end |