class ActiveRecord::Base

Public Class Methods

doesprettyurls(opts={}) click to toggle source

Call this method within your class to get fancy URLs

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

We need to help ActiveRecord go back the other way:

# File lib/doesprettyurls.rb, line 55
def self.from_param(param)
  find(param.to_i)
end

Public Instance Methods

to_param() click to toggle source

Provide a mechanism to automagically “slugify” URL parameters

# 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