Skip to content

Commit

Permalink
fixing issue #69 (ActiveRecord scopes are not chainable)
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed May 29, 2013
1 parent 744689e commit 608d233
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion aasm.gemspec
Expand Up @@ -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'
Expand Down
19 changes: 15 additions & 4 deletions lib/aasm/persistence/base.rb
Expand Up @@ -89,10 +89,21 @@ def state_with_scope(name, *args)
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)
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/persistence/active_record_persistence_spec.rb
Expand Up @@ -81,7 +81,7 @@
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

Expand All @@ -99,7 +99,7 @@
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

Expand Down

0 comments on commit 608d233

Please sign in to comment.