Permalink
Browse files

Fixed that #find_param was aliasing #param before it was defined.

Refactored to use AR's #read_attribute and #write_attribute instead of #send.
  • Loading branch information...
1 parent 085b4bc commit 60c17dfec0c579c58fe9df045cbab87e33651d6f Tyler Hunt committed May 29, 2008
Showing with 11 additions and 10 deletions.
  1. +8 −7 lib/find_by_param.rb
  2. +3 −3 test/find_by_param_test.rb
View
@@ -28,7 +28,9 @@ def find_param(param, options={})
end
end
- self.send(:alias_method, :to_param, param)
+ define_method(:to_param) do
+ read_attribute(param)
+ end
initialize_parameter(param, initialize_with, options) if initialize_with
end
@@ -37,15 +39,14 @@ def initialize_parameter(param, source, options={})
raise ColumnNotFoundError unless column_names.include?(source.to_s)
using = options.delete(:using)
- self.send(:define_method, :set_param) do
- value = self.send(source)
+ define_method(:set_param) do
+ value = read_attribute(source)
value = using.respond_to?(:call) ? using.call(value) : value.downcase.gsub(/[^\w]+/, '-')
-
- self.send("#{param}=", value)
+ write_attribute(param, value)
end
- self.send(:private, :set_param)
+ private :set_param
- self.send(:before_create, :set_param)
+ before_create(:set_param)
end
private :initialize_parameter
end
@@ -2,9 +2,9 @@
class FindByParamTest < Test::Unit::TestCase
def setup
+ Post.find_param(:slug)
Post.create(:slug => 'adam-west', :title => 'Adam West')
Post.create(:slug => 'burt-ward', :title => 'Burt Ward')
- Post.find_param(:slug)
end
def teardown
@@ -22,7 +22,7 @@ def test_find_by_param_was_defined
def test_find_by_param_is_defined_in_subclasses
assert Blog.respond_to?(:find_by_param)
end
-
+
def test_returns_valid_data
post = Post.find_by_param('adam-west')
assert_equal Post.find_by_slug('adam-west'), post
@@ -41,7 +41,7 @@ def test_can_define_find_parameter_with_string
end
def test_correctly_goes_to_param
- post = Post.find(:first, :conditions => { :slug => 'adam-west' })
+ post = Post.find_by_slug('adam-west')
assert_equal 'adam-west', post.to_param
end

0 comments on commit 60c17df

Please sign in to comment.