Skip to content

Commit

Permalink
Overriding to_param by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
djanowski committed Oct 16, 2008
1 parent 589de2f commit cf57a94
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
11 changes: 9 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@ These methods keep their name no matter what attribute is used to store the perm

The `find_by_permalink` method returns `nil` if there is no match; the `find_by_permalink!` method will raise `ActiveRecord::RecordNotFound`.

You can override the model's `to_param` method with
By default, this fork will override `ActiveRecord::Base#to_param` so that your controllers can still find your models without further work:

article.to_param
# => "1-foo-bar"

You can change this behavior by using the `param` option:

has_permalink :title, :param => :permalink

has_permalink :title, :param => true
has_permalink :title, :param => false

This means that the permalink will be used instead of the primary key (id) in generated URLs. Remember to change your controller code from e.g. `find` to `find_by_permalink!`.

Expand Down
16 changes: 15 additions & 1 deletion lib/permalink_fu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ def has_permalink(attr_names = [], 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
include ToParam if options[:param]

case options[:param]
when false
# nothing
when :permalink
include ToParam
else
include ToParamWithID
end
end
end

Expand All @@ -64,6 +72,12 @@ def to_param
end
end

module ToParamWithID
def to_param
"#{id}-#{send(self.class.permalink_field)}"
end
end

module PermalinkFinders
def find_by_permalink(value)
find(:first, :conditions => { permalink_field => value })
Expand Down
16 changes: 13 additions & 3 deletions test/permalink_fu_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ class AsModel < BaseModel
end

class OverrideParamModel < BaseModel
has_permalink :title, :param => true
has_permalink :title, :param => :permalink
end

class NoParamModel < BaseModel
has_permalink :title, :param => false
end

class IfProcConditionModel < BaseModel
Expand Down Expand Up @@ -300,13 +304,19 @@ def test_should_optionally_override_to_param
@m.permalink = 'My Param'
@m.validate
assert_equal 'my-param', @m.to_param

@m = NoParamModel.new
@m.permalink = 'My Param'
@m.validate
assert_not_equal 'my-param', @m.to_param
end

def test_should_not_override_to_param_by_default
def test_should_override_to_param_by_default
@m = MockModel.new
@m.permalink = 'My Param'
@m.id = 1
@m.validate
assert_not_equal 'my-param', @m.to_param
assert_equal '1-my-param', @m.to_param
end

end

0 comments on commit cf57a94

Please sign in to comment.