diff --git a/lib/aasm/persistence/active_record_persistence.rb b/lib/aasm/persistence/active_record_persistence.rb index d2a536e..137998b 100644 --- a/lib/aasm/persistence/active_record_persistence.rb +++ b/lib/aasm/persistence/active_record_persistence.rb @@ -31,6 +31,7 @@ module AASM def self.included(base) base.send(:include, AASM::Persistence::Base) base.send(:include, AASM::Persistence::ActiveRecordPersistence::InstanceMethods) + base.extend AASM::Persistence::ActiveRecordPersistence::ClassMethods base.after_initialize :aasm_ensure_initial_state @@ -38,6 +39,23 @@ module AASM base.validate :aasm_validate_states end + module ClassMethods + def aasm_create_scope(state_machine_name, scope_name) + conditions = { + table_name => { aasm(state_machine_name).attribute_name => scope_name.to_s } + } + if ActiveRecord::VERSION::MAJOR >= 3 + class_eval do + scope scope_name, lambda { where(conditions) } + end + else + class_eval do + named_scope scope_name, :conditions => conditions + end + end + end + end + module InstanceMethods # Writes state to the state column and persists it to the database diff --git a/lib/aasm/persistence/base.rb b/lib/aasm/persistence/base.rb index 3f8dd25..1f5b64f 100644 --- a/lib/aasm/persistence/base.rb +++ b/lib/aasm/persistence/base.rb @@ -63,53 +63,11 @@ module AASM private def create_scope?(name) - @state_machine.config.create_scopes && !@klass.respond_to?(name) + @state_machine.config.create_scopes && !@klass.respond_to?(name) && @klass.respond_to?(:aasm_create_scope) end def create_scope(name) - if ancestors_include?("ActiveRecord::Base") - create_for_active_record(name) - elsif ancestors_include?("Mongoid::Document") - create_for_mongoid(name) - elsif ancestors_include?("MongoMapper::Document") - create_for_mongomapper(name) - end - end - - def ancestors_include?(class_name) - @klass.ancestors.map { |klass| klass.to_s }.include?(class_name) - end - - def create_for_active_record(name) - conditions = { - @klass.table_name => { @klass.aasm(@name).attribute_name => name.to_s } - } - if ActiveRecord::VERSION::MAJOR >= 3 - @klass.class_eval do - scope name, lambda { where(conditions) } - end - else - @klass.class_eval do - named_scope name, :conditions => conditions - end - end - end - - def create_for_mongoid(name) - klass = @klass - state_machine_name = @name - scope_options = lambda { - klass.send( - :where, - { klass.aasm(state_machine_name).attribute_name.to_sym => name.to_s } - ) - } - @klass.send(:scope, name, scope_options) - end - - def create_for_mongomapper(name) - conditions = { @klass.aasm(@name).attribute_name.to_sym => name.to_s } - @klass.scope(name, lambda { @klass.where(conditions) }) + @klass.aasm_create_scope(@name, name) end end # Base diff --git a/lib/aasm/persistence/mongo_mapper_persistence.rb b/lib/aasm/persistence/mongo_mapper_persistence.rb index 9199b1f..9dadbd3 100644 --- a/lib/aasm/persistence/mongo_mapper_persistence.rb +++ b/lib/aasm/persistence/mongo_mapper_persistence.rb @@ -33,6 +33,7 @@ module AASM def self.included(base) base.send(:include, AASM::Persistence::Base) base.send(:include, AASM::Persistence::MongoMapperPersistence::InstanceMethods) + base.extend AASM::Persistence::MongoMapperPersistence::ClassMethods base.before_create :aasm_ensure_initial_state @@ -40,6 +41,13 @@ module AASM base.validate :aasm_validate_states end + module ClassMethods + def aasm_create_scope(state_machine_name, scope_name) + conditions = { aasm(state_machine_name).attribute_name.to_sym => scope_name.to_s } + scope(scope_name, lambda { where(conditions) }) + end + end + module InstanceMethods # Writes state to the state column and persists it to the database diff --git a/lib/aasm/persistence/mongoid_persistence.rb b/lib/aasm/persistence/mongoid_persistence.rb index 7566078..ea37bd5 100644 --- a/lib/aasm/persistence/mongoid_persistence.rb +++ b/lib/aasm/persistence/mongoid_persistence.rb @@ -33,10 +33,23 @@ module AASM def self.included(base) base.send(:include, AASM::Persistence::Base) base.send(:include, AASM::Persistence::MongoidPersistence::InstanceMethods) + base.extend AASM::Persistence::MongoidPersistence::ClassMethods base.after_initialize :aasm_ensure_initial_state end + module ClassMethods + def aasm_create_scope(state_machine_name, scope_name) + scope_options = lambda { + send( + :where, + { aasm(state_machine_name).attribute_name.to_sym => scope_name.to_s } + ) + } + send(:scope, scope_name, scope_options) + end + end + module InstanceMethods # Writes state to the state column and persists it to the database