From d84209b8afea9b86f3237453cbfc4287fcd14bb7 Mon Sep 17 00:00:00 2001 From: Tammer Saleh Date: Tue, 29 Jul 2008 17:21:38 -0400 Subject: [PATCH] modified the calling structure of should_have_named_scope for flexibility --- lib/shoulda/active_record_helpers.rb | 23 ++++++++++------------- test/unit/user_test.rb | 12 +++++++++--- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/shoulda/active_record_helpers.rb b/lib/shoulda/active_record_helpers.rb index 0104a71f..2a0fc219 100644 --- a/lib/shoulda/active_record_helpers.rb +++ b/lib/shoulda/active_record_helpers.rb @@ -603,9 +603,10 @@ module ThoughtBot # :nodoc: end end - # Ensures that the model has a named scope named scope_name that matches the definition given. - # You can optionally specify the arguments that will be passed into the named_scope after the - # scope name. + # Ensures that the model has a method named scope_name that returns a NamedScope object with the + # proxy options set to the options you supply. scope_name can be either a symbol, or a method + # call which will be evaled against the model. The eval'd method call has access to all the same + # instance variables that a should statement would. # # Options: Any of the options that the named scope would pass on to find. # @@ -625,8 +626,8 @@ module ThoughtBot # :nodoc: # # You can test lambdas or methods that return ActiveRecord#scoped calls: # - # should_have_named_scope :recent, 5, :limit => 5 - # should_have_named_scope :recent, 1, :limit => 1 + # should_have_named_scope 'recent(5)', :limit => 5 + # should_have_named_scope 'recent(1)', :limit => 1 # # Passes for # named_scope :recent, lambda {|c| {:limit => c}} @@ -637,18 +638,14 @@ module ThoughtBot # :nodoc: # scoped(:limit => c) # end # - def should_have_named_scope(scope_name, *args) + def should_have_named_scope(scope_call, *args) klass = model_class scope_opts = args.extract_options! - scope_args = args + scope_call = scope_call.to_s - context_name = "#{model_class}.#{scope_name}" - context_name << "(#{scope_args.map(&:inspect).join(', ')})" unless scope_args.empty? - - context context_name do + context scope_call do setup do - assert_respond_to klass, scope_name - @scope = klass.send(scope_name, *scope_args) + @scope = eval("#{klass}.#{scope_call}") end should "return a scope object" do diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index f36d6d2e..06ba6c22 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -13,9 +13,15 @@ class UserTest < Test::Unit::TestCase should_have_named_scope :old, :conditions => "age > 50" should_have_named_scope :eighteen, :conditions => { :age => 18 } - should_have_named_scope :recent, 5, :limit => 5 - should_have_named_scope :recent, 1, :limit => 1 - should_have_named_scope :recent_via_method, 7, :limit => 7 + + should_have_named_scope 'recent(5)', :limit => 5 + should_have_named_scope 'recent(1)', :limit => 1 + should_have_named_scope 'recent_via_method(7)', :limit => 7 + + context "when given an instance variable" do + setup { @count = 2 } + should_have_named_scope 'recent(@count)', :limit => 2 + end should_not_allow_values_for :email, "blah", "b lah" should_allow_values_for :email, "a@b.com", "asdf@asdf.com"