Module: PermalinkFu::ClassMethods

Defined in:
lib/permalink_fu.rb

Instance Method Summary collapse

Instance Method Details

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)
  options = args.extract_options!
  attr_names = args.first || :to_s
  self.permalink_attributes = Array(attr_names)
  self.permalink_field      = (options.delete(:as) || 'permalink').to_s
  self.permalink_options    = options
  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 options[:param]
  when false
    # nothing
  when :permalink
    include ToParam
  else
    include ToParamWithID
  end
end