diff --git a/CHANGELOG b/CHANGELOG
index 03ed5e7..9e91cc2 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,10 @@
+
+*0.3* (October 2nd 2008)
+
+* Add additional NamedScope patches for attribute_condition.
+ Also added GroupedScope::CoreExt to follow suite for GroupedScope::SelfGrouping attribute_conditions.
+
+
*0.2* (September 29th 2008)
* Add WillPaginate test and confirm grouped scope, named scope, and will paginate all play together. [Ken Collins]
diff --git a/test/lib/boot.rb b/test/lib/boot.rb
index 198f292..00c21c8 100644
--- a/test/lib/boot.rb
+++ b/test/lib/boot.rb
@@ -28,11 +28,5 @@
gem 'mislav-will_paginate', '2.3.4'
require 'will_paginate'
WillPaginate.enable_activerecord
-
-unless defined? ActiveRecord::NamedScope
- 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, ActiveRecord::NamedScope
-end
+require 'named_scope'
diff --git a/test/lib/named_scope.rb b/test/lib/named_scope.rb
old mode 100644
new mode 100755
index b30ef97..589dae3
--- a/test/lib/named_scope.rb
+++ b/test/lib/named_scope.rb
@@ -1,168 +1,7 @@
-module ActiveRecord
- module NamedScope
- # All subclasses of ActiveRecord::Base have two named_scopes:
- # * all, which is similar to a find(:all) query, and
- # * scoped, which allows for the creation of anonymous scopes, on the fly: Shirt.scoped(:conditions => {:color => 'red'}).scoped(:include => :washing_instructions)
- #
- # These anonymous scopes tend to be useful when procedurally generating complex queries, where passing
- # intermediate values (scopes) around as first-class objects is convenient.
- def self.included(base)
- base.class_eval do
- extend ClassMethods
- named_scope :scoped, lambda { |scope| scope }
- end
- end
-
- module ClassMethods
- def scopes
- read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
- end
-
- # Adds a class method for retrieving and querying objects. A scope represents a narrowing of a database query,
- # such as :conditions => {:color => :red}, :select => 'shirts.*', :include => :washing_instructions.
- #
- # class Shirt < ActiveRecord::Base
- # named_scope :red, :conditions => {:color => 'red'}
- # named_scope :dry_clean_only, :joins => :washing_instructions, :conditions => ['washing_instructions.dry_clean_only = ?', true]
- # end
- #
- # The above calls to named_scope define class methods Shirt.red and Shirt.dry_clean_only. Shirt.red,
- # in effect, represents the query Shirt.find(:all, :conditions => {:color => 'red'}).
- #
- # Unlike Shirt.find(...), however, the object returned by Shirt.red is not an Array; it resembles the association object
- # constructed by a has_many declaration. For instance, you can invoke Shirt.red.find(:first), Shirt.red.count,
- # Shirt.red.find(:all, :conditions => {:size => 'small'}). Also, just
- # as with the association objects, name scopes acts like an Array, implementing Enumerable; Shirt.red.each(&block),
- # Shirt.red.first, and Shirt.red.inject(memo, &block) all behave as if Shirt.red really were an Array.
- #
- # These named scopes are composable. For instance, Shirt.red.dry_clean_only will produce all shirts that are both red and dry clean only.
- # Nested finds and calculations also work with these compositions: Shirt.red.dry_clean_only.count returns the number of garments
- # for which these criteria obtain. Similarly with Shirt.red.dry_clean_only.average(:thread_count).
- #
- # All scopes are available as class methods on the ActiveRecord::Base descendent upon which the scopes were defined. But they are also available to
- # has_many associations. If,
- #
- # class Person < ActiveRecord::Base
- # has_many :shirts
- # end
- #
- # then elton.shirts.red.dry_clean_only will return all of Elton's red, dry clean
- # only shirts.
- #
- # Named scopes can also be procedural.
- #
- # class Shirt < ActiveRecord::Base
- # named_scope :colored, lambda { |color|
- # { :conditions => { :color => color } }
- # }
- # end
- #
- # In this example, Shirt.colored('puce') finds all puce shirts.
- #
- # Named scopes can also have extensions, just as with has_many declarations:
- #
- # class Shirt < ActiveRecord::Base
- # named_scope :red, :conditions => {:color => 'red'} do
- # def dom_id
- # 'red_shirts'
- # end
- # end
- # end
- #
- #
- # For testing complex named scopes, you can examine the scoping options using the
- # proxy_options method on the proxy itself.
- #
- # class Shirt < ActiveRecord::Base
- # named_scope :colored, lambda { |color|
- # { :conditions => { :color => color } }
- # }
- # end
- #
- # expected_options = { :conditions => { :colored => 'red' } }
- # assert_equal expected_options, Shirt.colored('red').proxy_options
- def named_scope(name, options = {}, &block)
- name = name.to_sym
- scopes[name] = lambda do |parent_scope, *args|
- Scope.new(parent_scope, case options
- when Hash
- options
- when Proc
- options.call(*args)
- end, &block)
- end
- (class << self; self end).instance_eval do
- define_method name do |*args|
- scopes[name].call(self, *args)
- end
- end
- end
- end
-
- class Scope
- attr_reader :proxy_scope, :proxy_options
-
- [].methods.each do |m|
- unless m =~ /(^__|^nil\?|^send|^object_id$|class|extend|^find$|count|sum|average|maximum|minimum|paginate|first|last|empty\?|respond_to\?)/
- delegate m, :to => :proxy_found
- end
- end
-
- delegate :scopes, :with_scope, :to => :proxy_scope
-
- def initialize(proxy_scope, options, &block)
- [options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
- extend Module.new(&block) if block_given?
- @proxy_scope, @proxy_options = proxy_scope, options.except(:extend)
- end
-
- def reload
- load_found; self
- end
-
- def first(*args)
- if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
- proxy_found.first(*args)
- else
- find(:first, *args)
- end
- end
-
- def last(*args)
- if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
- proxy_found.last(*args)
- else
- find(:last, *args)
- end
- end
-
- def empty?
- @found ? @found.empty? : count.zero?
- end
-
- def respond_to?(method, include_private = false)
- super || @proxy_scope.respond_to?(method, include_private)
- end
-
- protected
- def proxy_found
- @found || load_found
- end
-
- private
- def method_missing(method, *args, &block)
- if scopes.include?(method)
- scopes[method].call(self, *args)
- else
- with_scope :find => proxy_options do
- proxy_scope.send(method, *args, &block)
- end
- end
- end
-
- def load_found
- @found = find(:all)
- end
- end
- end
+unless defined? ActiveRecord::NamedScope
+ require "named_scope/core_ext"
+ require "named_scope/named_scope"
+ require "named_scope/named_scope_patch_#{ActiveRecord::Base.respond_to?(:find_first) ? '1.2' : '2.0'}"
+ ActiveRecord::Base.send :include, ActiveRecord::NamedScope
end
+
diff --git a/test/lib/named_scope/core_ext.rb b/test/lib/named_scope/core_ext.rb
new file mode 100755
index 0000000..10b4410
--- /dev/null
+++ b/test/lib/named_scope/core_ext.rb
@@ -0,0 +1,82 @@
+
+unless Hash.instance_methods.include? 'except'
+ Hash.class_eval do
+
+ # Returns a new hash without the given keys.
+ def except(*keys)
+ rejected = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
+ reject { |key,| rejected.include?(key) }
+ end
+
+ # Replaces the hash without only the given keys.
+ def except!(*keys)
+ replace(except(*keys))
+ end
+
+ end
+end
+
+class ActiveRecord::Base
+ class << self
+
+ def first(*args)
+ find(:first, *args)
+ end
+
+ def last(*args)
+ find(:last, *args)
+ end
+
+ def all(*args)
+ find(:all, *args)
+ end
+
+ private
+
+ def find_last(options)
+ order = options[:order]
+ if order
+ order = reverse_sql_order(order)
+ elsif !scoped?(:find, :order)
+ order = "#{table_name}.#{primary_key} DESC"
+ end
+ if scoped?(:find, :order)
+ scoped_order = reverse_sql_order(scope(:find, :order))
+ scoped_methods.select { |s| s[:find].update(:order => scoped_order) }
+ end
+ find_initial(options.merge({ :order => order }))
+ end
+
+ def reverse_sql_order(order_query)
+ reversed_query = order_query.split(/,/).each { |s|
+ if s.match(/\s(asc|ASC)$/)
+ s.gsub!(/\s(asc|ASC)$/, ' DESC')
+ elsif s.match(/\s(desc|DESC)$/)
+ s.gsub!(/\s(desc|DESC)$/, ' ASC')
+ elsif !s.match(/\s(asc|ASC|desc|DESC)$/)
+ s.concat(' DESC')
+ end
+ }.join(',')
+ end
+
+ end
+end
+
+ActiveRecord::Associations::AssociationCollection.class_eval do
+
+ def last(*args)
+ if fetch_first_or_last_using_find? args
+ find(:last, *args)
+ else
+ load_target unless loaded?
+ @target.last(*args)
+ end
+ end
+
+ private
+
+ def fetch_first_or_last_using_find?(args)
+ args.first.kind_of?(Hash) || !(loaded? || @owner.new_record? || @reflection.options[:finder_sql] || !@target.blank? || args.first.kind_of?(Integer))
+ end
+
+end
diff --git a/test/lib/named_scope/named_scope.rb b/test/lib/named_scope/named_scope.rb
new file mode 100755
index 0000000..b30ef97
--- /dev/null
+++ b/test/lib/named_scope/named_scope.rb
@@ -0,0 +1,168 @@
+module ActiveRecord
+ module NamedScope
+ # All subclasses of ActiveRecord::Base have two named_scopes:
+ # * all, which is similar to a find(:all) query, and
+ # * scoped, which allows for the creation of anonymous scopes, on the fly: Shirt.scoped(:conditions => {:color => 'red'}).scoped(:include => :washing_instructions)
+ #
+ # These anonymous scopes tend to be useful when procedurally generating complex queries, where passing
+ # intermediate values (scopes) around as first-class objects is convenient.
+ def self.included(base)
+ base.class_eval do
+ extend ClassMethods
+ named_scope :scoped, lambda { |scope| scope }
+ end
+ end
+
+ module ClassMethods
+ def scopes
+ read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
+ end
+
+ # Adds a class method for retrieving and querying objects. A scope represents a narrowing of a database query,
+ # such as :conditions => {:color => :red}, :select => 'shirts.*', :include => :washing_instructions.
+ #
+ # class Shirt < ActiveRecord::Base
+ # named_scope :red, :conditions => {:color => 'red'}
+ # named_scope :dry_clean_only, :joins => :washing_instructions, :conditions => ['washing_instructions.dry_clean_only = ?', true]
+ # end
+ #
+ # The above calls to named_scope define class methods Shirt.red and Shirt.dry_clean_only. Shirt.red,
+ # in effect, represents the query Shirt.find(:all, :conditions => {:color => 'red'}).
+ #
+ # Unlike Shirt.find(...), however, the object returned by Shirt.red is not an Array; it resembles the association object
+ # constructed by a has_many declaration. For instance, you can invoke Shirt.red.find(:first), Shirt.red.count,
+ # Shirt.red.find(:all, :conditions => {:size => 'small'}). Also, just
+ # as with the association objects, name scopes acts like an Array, implementing Enumerable; Shirt.red.each(&block),
+ # Shirt.red.first, and Shirt.red.inject(memo, &block) all behave as if Shirt.red really were an Array.
+ #
+ # These named scopes are composable. For instance, Shirt.red.dry_clean_only will produce all shirts that are both red and dry clean only.
+ # Nested finds and calculations also work with these compositions: Shirt.red.dry_clean_only.count returns the number of garments
+ # for which these criteria obtain. Similarly with Shirt.red.dry_clean_only.average(:thread_count).
+ #
+ # All scopes are available as class methods on the ActiveRecord::Base descendent upon which the scopes were defined. But they are also available to
+ # has_many associations. If,
+ #
+ # class Person < ActiveRecord::Base
+ # has_many :shirts
+ # end
+ #
+ # then elton.shirts.red.dry_clean_only will return all of Elton's red, dry clean
+ # only shirts.
+ #
+ # Named scopes can also be procedural.
+ #
+ # class Shirt < ActiveRecord::Base
+ # named_scope :colored, lambda { |color|
+ # { :conditions => { :color => color } }
+ # }
+ # end
+ #
+ # In this example, Shirt.colored('puce') finds all puce shirts.
+ #
+ # Named scopes can also have extensions, just as with has_many declarations:
+ #
+ # class Shirt < ActiveRecord::Base
+ # named_scope :red, :conditions => {:color => 'red'} do
+ # def dom_id
+ # 'red_shirts'
+ # end
+ # end
+ # end
+ #
+ #
+ # For testing complex named scopes, you can examine the scoping options using the
+ # proxy_options method on the proxy itself.
+ #
+ # class Shirt < ActiveRecord::Base
+ # named_scope :colored, lambda { |color|
+ # { :conditions => { :color => color } }
+ # }
+ # end
+ #
+ # expected_options = { :conditions => { :colored => 'red' } }
+ # assert_equal expected_options, Shirt.colored('red').proxy_options
+ def named_scope(name, options = {}, &block)
+ name = name.to_sym
+ scopes[name] = lambda do |parent_scope, *args|
+ Scope.new(parent_scope, case options
+ when Hash
+ options
+ when Proc
+ options.call(*args)
+ end, &block)
+ end
+ (class << self; self end).instance_eval do
+ define_method name do |*args|
+ scopes[name].call(self, *args)
+ end
+ end
+ end
+ end
+
+ class Scope
+ attr_reader :proxy_scope, :proxy_options
+
+ [].methods.each do |m|
+ unless m =~ /(^__|^nil\?|^send|^object_id$|class|extend|^find$|count|sum|average|maximum|minimum|paginate|first|last|empty\?|respond_to\?)/
+ delegate m, :to => :proxy_found
+ end
+ end
+
+ delegate :scopes, :with_scope, :to => :proxy_scope
+
+ def initialize(proxy_scope, options, &block)
+ [options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
+ extend Module.new(&block) if block_given?
+ @proxy_scope, @proxy_options = proxy_scope, options.except(:extend)
+ end
+
+ def reload
+ load_found; self
+ end
+
+ def first(*args)
+ if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
+ proxy_found.first(*args)
+ else
+ find(:first, *args)
+ end
+ end
+
+ def last(*args)
+ if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
+ proxy_found.last(*args)
+ else
+ find(:last, *args)
+ end
+ end
+
+ def empty?
+ @found ? @found.empty? : count.zero?
+ end
+
+ def respond_to?(method, include_private = false)
+ super || @proxy_scope.respond_to?(method, include_private)
+ end
+
+ protected
+ def proxy_found
+ @found || load_found
+ end
+
+ private
+ def method_missing(method, *args, &block)
+ if scopes.include?(method)
+ scopes[method].call(self, *args)
+ else
+ with_scope :find => proxy_options do
+ proxy_scope.send(method, *args, &block)
+ end
+ end
+ end
+
+ def load_found
+ @found = find(:all)
+ end
+ end
+ end
+end
diff --git a/test/lib/named_scope_patch_1.2.6.rb b/test/lib/named_scope/named_scope_patch_1.2.rb
old mode 100644
new mode 100755
similarity index 86%
rename from test/lib/named_scope_patch_1.2.6.rb
rename to test/lib/named_scope/named_scope_patch_1.2.rb
index a63e2b8..01fc507
--- a/test/lib/named_scope_patch_1.2.6.rb
+++ b/test/lib/named_scope/named_scope_patch_1.2.rb
@@ -8,6 +8,17 @@ def with_scope(*args, &block)
class ActiveRecord::Base
class << self
+ def find(*args)
+ options = extract_options_from_args!(args)
+ validate_find_options(options)
+ set_readonly_option!(options)
+ case args.first
+ when :first then find_initial(options)
+ when :last then find_last(options)
+ when :all then find_every(options)
+ else find_from_ids(args, options)
+ end
+ end
private
def attribute_condition_with_named_scope(argument)
case argument
diff --git a/test/lib/named_scope_patch_2.0.rb b/test/lib/named_scope/named_scope_patch_2.0.rb
old mode 100644
new mode 100755
similarity index 77%
rename from test/lib/named_scope_patch_2.0.rb
rename to test/lib/named_scope/named_scope_patch_2.0.rb
index 0751b94..ae780c2
--- a/test/lib/named_scope_patch_2.0.rb
+++ b/test/lib/named_scope/named_scope_patch_2.0.rb
@@ -8,6 +8,17 @@ def with_scope(*args, &block)
class ActiveRecord::Base
class << self
+ def find(*args)
+ options = args.extract_options!
+ validate_find_options(options)
+ set_readonly_option!(options)
+ case args.first
+ when :first then find_initial(options)
+ when :last then find_last(options)
+ when :all then find_every(options)
+ else find_from_ids(args, options)
+ end
+ end
private
def attribute_condition_with_named_scope(argument)
case argument