Skip to content

Commit

Permalink
Allow using find(, :scope => active_record_object) with sluggable ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Empact committed Dec 5, 2009
1 parent 4f9ff06 commit a94e343
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/friendly_id/sluggable_class_methods.rb
Expand Up @@ -6,6 +6,7 @@ module FriendlyId::SluggableClassMethods
def find_one(id_or_name, options) #:nodoc:#

scope = options.delete(:scope)
scope = scope.to_param if scope && scope.respond_to?(:to_param)

if id_or_name.is_a?(Integer) || id_or_name.kind_of?(ActiveRecord::Base)
return super(id_or_name, options)
Expand All @@ -23,7 +24,6 @@ def find_one(id_or_name, options) #:nodoc:#
}

result = with_scope(:find => find_options) { find_initial(options) }

if result
result.finder_slug_name = id_or_name
elsif id_or_name.to_i.to_s != id_or_name
Expand Down
6 changes: 5 additions & 1 deletion test/scoped_model_test.rb
Expand Up @@ -27,10 +27,14 @@ class ScopedModelTest < Test::Unit::TestCase
assert_equal 2, Resident.find(:all, @resident.friendly_id).size
end

should "find a single scoped records with a scope" do
should "find a single scoped records with a scope as a string" do
assert Resident.find(@resident.friendly_id, :scope => @resident.country.to_param)
end

should "find a single scoped records with a scope" do
assert Resident.find(@resident.friendly_id, :scope => @resident.country)
end

should "raise an error when finding a single scoped record with no scope" do
assert_raises ActiveRecord::RecordNotFound do
Resident.find(@resident.friendly_id)
Expand Down

0 comments on commit a94e343

Please sign in to comment.