Skip to content

Commit

Permalink
finders and params
Browse files Browse the repository at this point in the history
  • Loading branch information
philsmy committed Oct 16, 2010
1 parent 4e8fe2d commit 8892331
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions lib/permalink_fu.rb
Expand Up @@ -83,12 +83,48 @@ def has_permalink(attr_names = [], permalink_field = nil, options = {})
before_validation :create_common_permalink
end

define_method :"#{self.permalink_field}=" do |value|
write_attribute(self.permalink_field, value.blank? ? '' : PermalinkFu.escape(value))
# define_method :"#{self.permalink_field}=" do |value|
# write_attribute(self.permalink_field, value.blank? ? '' : PermalinkFu.escape(value))
# end

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
end
module ToParam
def to_param
send(self.class.read_inheritable_attribute(:permalink_field))
end
end

module ToParamWithID
def to_param
permalink = send(self.class.read_inheritable_attribute(:permalink_field))
return super if new_record? || permalink.blank?
"#{id}-#{permalink}"
end
end

module PermalinkFinders
def find_by_permalink(value)
find(:first, :conditions => { permalink_field => value })
end

def find_by_permalink!(value)
find_by_permalink(value) ||
raise(ActiveRecord::RecordNotFound, "Couldn't find #{name} with permalink #{value.inspect}")
end
end

# This contains instance methods for ActiveRecord models that have permalinks.
module InstanceMethods
Expand Down

0 comments on commit 8892331

Please sign in to comment.