From e3a0920dc0ccee3f4ba0e8d946451309feb9d87c Mon Sep 17 00:00:00 2001 From: Ken Collins Date: Thu, 2 Oct 2008 16:26:37 -0400 Subject: [PATCH] Add additional NamedScope patches for attribute_condition. Also added GroupedScope::CoreExt to follow suite for GroupedScope::SelfGrouping attribute_conditions. --- Rakefile | 24 ++++++++++++++++++++++-- lib/grouped_scope.rb | 3 ++- lib/grouped_scope/core_ext.rb | 21 +++++++++++++++++++++ test/grouped_scope/self_grouping_test.rb | 6 ++++++ test/lib/boot.rb | 2 +- test/lib/named_scope.rb | 2 +- test/lib/named_scope_patch_1.2.6.rb | 13 +++++++++++++ test/lib/named_scope_patch_2.0.rb | 12 ++++++++++++ 8 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 lib/grouped_scope/core_ext.rb diff --git a/Rakefile b/Rakefile index e0e91bd..4ed5c36 100644 --- a/Rakefile +++ b/Rakefile @@ -2,10 +2,17 @@ require 'rake' require 'rake/testtask' require 'rake/rdoctask' +def reset_invoked + ['test_rails','test'].each do |name| + Rake::Task[name].instance_variable_set '@already_invoked', false + end +end + + desc 'Default: run unit tests.' task :default => :test -desc 'Test the grouped_scope plugin.' +desc 'Test the GroupedScope plugin.' Rake::TestTask.new(:test) do |t| t.libs << 'lib' t.libs << 'test' @@ -13,7 +20,18 @@ Rake::TestTask.new(:test) do |t| t.verbose = true end -desc 'Generate documentation for the grouped_scope plugin.' +desc 'Test the GroupedScope plugin with Rails 2.1.1, 2.0.4 & 1.2.6 gems' +task :test_rails do + test = Rake::Task['test'] + versions = ['2.1.1','2.0.4','1.2.6'] + versions.each do |version| + ENV['RAILS_VERSION'] = "#{version}" + test.invoke + reset_invoked unless version == versions.last + end +end + +desc 'Generate documentation for the GroupedScope plugin.' Rake::RDocTask.new(:rdoc) do |rdoc| rdoc.rdoc_dir = 'rdoc' rdoc.title = 'GroupedScope' @@ -21,3 +39,5 @@ Rake::RDocTask.new(:rdoc) do |rdoc| rdoc.rdoc_files.include('README') rdoc.rdoc_files.include('lib/**/*.rb') end + + diff --git a/lib/grouped_scope.rb b/lib/grouped_scope.rb index ec70452..6215d70 100644 --- a/lib/grouped_scope.rb +++ b/lib/grouped_scope.rb @@ -5,10 +5,11 @@ require 'grouped_scope/class_methods' require 'grouped_scope/has_many_association' require 'grouped_scope/has_many_through_association' +require 'grouped_scope/core_ext' module GroupedScope - VERSION = '0.2' + VERSION = '0.3' end diff --git a/lib/grouped_scope/core_ext.rb b/lib/grouped_scope/core_ext.rb new file mode 100644 index 0000000..061a7fe --- /dev/null +++ b/lib/grouped_scope/core_ext.rb @@ -0,0 +1,21 @@ + +class ActiveRecord::Base + + class << self + + private + + def attribute_condition_with_grouped_scope(argument) + case argument + when GroupedScope::SelfGroupping then "IN (?)" + else attribute_condition_without_grouped_scope(argument) + end + end + + alias_method_chain :attribute_condition, :grouped_scope + + end + +end + + diff --git a/test/grouped_scope/self_grouping_test.rb b/test/grouped_scope/self_grouping_test.rb index 924988a..9ff7d15 100644 --- a/test/grouped_scope/self_grouping_test.rb +++ b/test/grouped_scope/self_grouping_test.rb @@ -33,6 +33,12 @@ def setup assert_raise(GroupedScope::NoGroupIdError) { GroupedScope::SelfGroupping.new(FooBar.new) } end + should 'return correct attribute_condition for GroupedScope::SelfGroupping object' do + assert_sql(/"?group_id"? IN \(#{@employee.id}\)/) do + Employee.find :all, :conditions => {:group_id => @employee.group} + end + end + context 'for Array delegates' do should 'respond to first/last' do diff --git a/test/lib/boot.rb b/test/lib/boot.rb index d01aba0..198f292 100644 --- a/test/lib/boot.rb +++ b/test/lib/boot.rb @@ -33,6 +33,6 @@ require 'core_ext' require 'named_scope' require ActiveRecord::Base.respond_to?(:find_first) ? 'named_scope_patch_1.2.6' : 'named_scope_patch_2.0' - ActiveRecord::Base.send :include, GroupedScope::NamedScope + ActiveRecord::Base.send :include, ActiveRecord::NamedScope end diff --git a/test/lib/named_scope.rb b/test/lib/named_scope.rb index ccb3460..b30ef97 100644 --- a/test/lib/named_scope.rb +++ b/test/lib/named_scope.rb @@ -1,4 +1,4 @@ -module GroupedScope +module ActiveRecord module NamedScope # All subclasses of ActiveRecord::Base have two named_scopes: # * all, which is similar to a find(:all) query, and diff --git a/test/lib/named_scope_patch_1.2.6.rb b/test/lib/named_scope_patch_1.2.6.rb index 7f2a84a..a63e2b8 100644 --- a/test/lib/named_scope_patch_1.2.6.rb +++ b/test/lib/named_scope_patch_1.2.6.rb @@ -6,6 +6,19 @@ def with_scope(*args, &block) end end +class ActiveRecord::Base + class << self + private + def attribute_condition_with_named_scope(argument) + case argument + when ActiveRecord::Associations::AssociationCollection, ActiveRecord::NamedScope::Scope then "IN (?)" + else attribute_condition_without_named_scope(argument) + end + end + alias_method_chain :attribute_condition, :named_scope + end +end + ActiveRecord::Associations::HasManyAssociation.class_eval do protected def method_missing(method, *args, &block) diff --git a/test/lib/named_scope_patch_2.0.rb b/test/lib/named_scope_patch_2.0.rb index 9a6250b..0751b94 100644 --- a/test/lib/named_scope_patch_2.0.rb +++ b/test/lib/named_scope_patch_2.0.rb @@ -6,6 +6,18 @@ def with_scope(*args, &block) end end +class ActiveRecord::Base + class << self + private + def attribute_condition_with_named_scope(argument) + case argument + when ActiveRecord::NamedScope::Scope then "IN (?)" + else attribute_condition_without_named_scope(argument) + end + end + alias_method_chain :attribute_condition, :named_scope + end +end ActiveRecord::Associations::AssociationCollection.class_eval do protected