fixing issue #69 (ActiveRecord scopes are not chainable)

This commit is contained in:
Thorsten Böttger 2013-05-29 23:19:04 +02:00
parent 744689e2cf
commit 608d23332c
4 changed files with 23 additions and 8 deletions

View File

@ -2,9 +2,11 @@
## Unreleased
* fixing issue #69 (ActiveRecord scopes are not chainable)
## 3.0.18
* fixing issue #66
* fixing issue #66 (state methods not reflecting the current state)
## 3.0.17

View File

@ -13,7 +13,9 @@ Gem::Specification.new do |s|
s.date = Time.now
s.licenses = ["MIT"]
s.add_development_dependency 'activerecord'
s.add_development_dependency 'activerecord', '3.2.12'
# s.add_development_dependency 'activerecord', '4.0.0.rc1'
s.add_development_dependency 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
s.add_development_dependency 'rake'
s.add_development_dependency 'sdoc'

View File

@ -89,10 +89,21 @@ module AASM
state_without_scope(name, *args)
unless @clazz.respond_to?(name)
if @clazz.ancestors.map {|klass| klass.to_s}.include?("ActiveRecord::Base")
scope_options_hash = {:conditions => { "#{@clazz.table_name}.#{@clazz.aasm_column}" => name.to_s}}
scope_method = ActiveRecord::VERSION::MAJOR >= 3 ? :scope : :named_scope
scope_options = ActiveRecord::VERSION::MAJOR >= 4 ? lambda { where(scope_options_hash[:conditions])} : scope_options_hash
@clazz.send(scope_method, name, scope_options)
conditions = {"#{@clazz.table_name}.#{@clazz.aasm_column}" => name.to_s}
if ActiveRecord::VERSION::MAJOR >= 4
@clazz.class_eval do
scope name, lambda { where(conditions) }
end
elsif ActiveRecord::VERSION::MAJOR >= 3
@clazz.class_eval do
scope name, where(conditions)
end
else
@clazz.class_eval do
named_scope name, :conditions => conditions
end
end
elsif @clazz.ancestors.map {|klass| klass.to_s}.include?("Mongoid::Document")
scope_options = lambda { @clazz.send(:where, {@clazz.aasm_column.to_sym => name.to_s}) }
@clazz.send(:scope, name, scope_options)

View File

@ -81,7 +81,7 @@ describe "named scopes with the old DSL" do
context "Does not already respond_to? the scope name" do
it "should add a scope" do
Simple.should respond_to(:unknown_scope)
Simple.unknown_scope.class.should == ActiveRecord::Relation
SimpleNewDsl.unknown_scope.is_a?(ActiveRecord::Relation).should be_true
end
end
@ -99,7 +99,7 @@ describe "named scopes with the new DSL" do
context "Does not already respond_to? the scope name" do
it "should add a scope" do
SimpleNewDsl.should respond_to(:unknown_scope)
SimpleNewDsl.unknown_scope.class.should == ActiveRecord::Relation
SimpleNewDsl.unknown_scope.is_a?(ActiveRecord::Relation).should be_true
end
end