diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index 189fc5d..6572bfb 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -3,10 +3,10 @@ module AASM def self.included(base) #:nodoc: base.extend AASM::ClassMethods - AASM::Persistence.set_persistence(base) unless AASM::StateMachine[base] AASM::StateMachine[base] = AASM::StateMachine.new('') end + AASM::Persistence.set_persistence(base) super end diff --git a/lib/aasm/persistence/active_record_persistence.rb b/lib/aasm/persistence/active_record_persistence.rb index 8f62d4c..00216f9 100644 --- a/lib/aasm/persistence/active_record_persistence.rb +++ b/lib/aasm/persistence/active_record_persistence.rb @@ -38,19 +38,6 @@ module AASM base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteState) unless base.method_defined?(:aasm_write_state) base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence) unless base.method_defined?(:aasm_write_state_without_persistence) - if base.respond_to?(:named_scope) || base.respond_to?(:scope) - base.extend(AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods) - - base.class_eval do - class << self - unless method_defined?(:aasm_state_without_scope) - alias_method :aasm_state_without_scope, :aasm_state - alias_method :aasm_state, :aasm_state_with_scope - end - end - end - end - if ActiveRecord::VERSION::MAJOR >= 3 base.before_validation(:aasm_ensure_initial_state, :on => :create) else @@ -245,18 +232,22 @@ module AASM end end - module NamedScopeMethods - def aasm_state_with_scope name, options = {} - aasm_state_without_scope name, options - - unless self.respond_to?(name) - scope_options = {:conditions => { "#{table_name}.#{self.aasm_column}" => name.to_s}} - scope_method = ActiveRecord::VERSION::MAJOR >= 3 ? :scope : :named_scope - self.send(scope_method, name, scope_options) - end - - end - end end end end + +class AASM::Base + def state_with_scope(name, *args) + state_without_scope(name, *args) + if @clazz.ancestors.map {|klass| klass.to_s}.include?("ActiveRecord::Base") && !@clazz.respond_to?(name) + # puts "setting scope #{@clazz.name}.#{name}" + scope_options = {:conditions => { "#{@clazz.table_name}.#{@clazz.aasm_column}" => name.to_s}} + scope_method = ActiveRecord::VERSION::MAJOR >= 3 ? :scope : :named_scope + @clazz.send(scope_method, name, scope_options) + # else + # puts "not setting scope #{@clazz.name}.#{name}" + end + end + alias_method :state_without_scope, :state + alias_method :state, :state_with_scope +end diff --git a/spec/schema.rb b/spec/schema.rb index c02c960..1022a10 100644 --- a/spec/schema.rb +++ b/spec/schema.rb @@ -1,6 +1,6 @@ ActiveRecord::Schema.define(:version => 0) do - %w{gates readers writers transients simples thieves localizer_test_models}.each do |table_name| + %w{gates readers writers transients simples simple_new_dsls thieves localizer_test_models}.each do |table_name| create_table table_name, :force => true do |t| t.string "aasm_state" end diff --git a/spec/unit/active_record_persistence_spec.rb b/spec/unit/active_record_persistence_spec.rb index 7e79d61..bc48430 100644 --- a/spec/unit/active_record_persistence_spec.rb +++ b/spec/unit/active_record_persistence_spec.rb @@ -46,11 +46,17 @@ end class Simple < ActiveRecord::Base include AASM aasm_column :status + aasm_state :unknown_scope + aasm_state :new end class SimpleNewDsl < ActiveRecord::Base include AASM aasm :column => :status + aasm do + state :unknown_scope + state :new + end end class Derivate < Simple @@ -220,24 +226,36 @@ describe 'Derivates' do end end -describe AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods do +describe "AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods" do - context "Does not already respond_to? the scope name" do + context "Old DSL Does not already respond_to? the scope name" do it "should add a scope" do - Simple.should_not respond_to(:unknown_scope) - Simple.aasm_state :unknown_scope Simple.should respond_to(:unknown_scope) Simple.unknown_scope.class.should == ActiveRecord::Relation end end - context "Already respond_to? the scope name" do + context "Old DSL Already respond_to? the scope name" do it "should not add a scope" do - Simple.aasm_state :new Simple.should respond_to(:new) Simple.new.class.should == Simple end end + + context "New DSL 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 + end + end + + context "New DSL Already respond_to? the scope name" do + it "should not add a scope" do + SimpleNewDsl.should respond_to(:new) + SimpleNewDsl.new.class.should == SimpleNewDsl + end + end + end describe 'Thieves' do