Skip to content

Commit

Permalink
Add additional NamedScope patches for attribute_condition. Also added…
Browse files Browse the repository at this point in the history
… GroupedScope::CoreExt to follow suite for GroupedScope::SelfGrouping attribute_conditions.
  • Loading branch information
metaskills committed Oct 2, 2008
1 parent 443eee3 commit e3a0920
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 5 deletions.
24 changes: 22 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,42 @@ 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'
t.pattern = 'test/**/*_test.rb'
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'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end


3 changes: 2 additions & 1 deletion lib/grouped_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

21 changes: 21 additions & 0 deletions lib/grouped_scope/core_ext.rb
Original file line number Diff line number Diff line change
@@ -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


6 changes: 6 additions & 0 deletions test/grouped_scope/self_grouping_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/lib/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion test/lib/named_scope.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module GroupedScope
module ActiveRecord
module NamedScope
# All subclasses of ActiveRecord::Base have two named_scopes:
# * <tt>all</tt>, which is similar to a <tt>find(:all)</tt> query, and
Expand Down
13 changes: 13 additions & 0 deletions test/lib/named_scope_patch_1.2.6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions test/lib/named_scope_patch_2.0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e3a0920

Please sign in to comment.