diff --git a/CHANGELOG.md b/CHANGELOG.md index fb44b8b..14b252e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/aasm.gemspec b/aasm.gemspec index 11a1b56..02ea996 100644 --- a/aasm.gemspec +++ b/aasm.gemspec @@ -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' diff --git a/lib/aasm/persistence/base.rb b/lib/aasm/persistence/base.rb index 453a0ef..a771f8d 100644 --- a/lib/aasm/persistence/base.rb +++ b/lib/aasm/persistence/base.rb @@ -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) diff --git a/spec/unit/persistence/active_record_persistence_spec.rb b/spec/unit/persistence/active_record_persistence_spec.rb index 6707e86..78b63df 100644 --- a/spec/unit/persistence/active_record_persistence_spec.rb +++ b/spec/unit/persistence/active_record_persistence_spec.rb @@ -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