mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Don't add named_scope if the ActiveRecord object responds to the method name already (such as 'new')
Signed-off-by: Scott Barron <scott@elitists.net>
This commit is contained in:
parent
656984f8b2
commit
f1748118ba
2 changed files with 55 additions and 36 deletions
|
@ -37,10 +37,10 @@ module AASM
|
||||||
base.send(:include, AASM::Persistence::ActiveRecordPersistence::ReadState) unless base.method_defined?(:aasm_read_state)
|
base.send(:include, AASM::Persistence::ActiveRecordPersistence::ReadState) unless base.method_defined?(:aasm_read_state)
|
||||||
base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteState) unless base.method_defined?(:aasm_write_state)
|
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)
|
base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence) unless base.method_defined?(:aasm_write_state_without_persistence)
|
||||||
|
|
||||||
if base.respond_to?(:named_scope)
|
if base.respond_to?(:named_scope)
|
||||||
base.extend(AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods)
|
base.extend(AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods)
|
||||||
|
|
||||||
base.class_eval do
|
base.class_eval do
|
||||||
class << self
|
class << self
|
||||||
alias_method :aasm_state_without_named_scope, :aasm_state
|
alias_method :aasm_state_without_named_scope, :aasm_state
|
||||||
|
@ -48,7 +48,7 @@ module AASM
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
base.before_validation_on_create :aasm_ensure_initial_state
|
base.before_validation_on_create :aasm_ensure_initial_state
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -119,7 +119,7 @@ module AASM
|
||||||
|
|
||||||
# Returns the current aasm_state of the object. Respects reload and
|
# Returns the current aasm_state of the object. Respects reload and
|
||||||
# any changes made to the aasm_state field directly
|
# any changes made to the aasm_state field directly
|
||||||
#
|
#
|
||||||
# Internally just calls <tt>aasm_read_state</tt>
|
# Internally just calls <tt>aasm_read_state</tt>
|
||||||
#
|
#
|
||||||
# foo = Foo.find(1)
|
# foo = Foo.find(1)
|
||||||
|
@ -136,7 +136,7 @@ module AASM
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# Ensures that if the aasm_state column is nil and the record is new
|
# Ensures that if the aasm_state column is nil and the record is new
|
||||||
# that the initial state gets populated before validation on create
|
# that the initial state gets populated before validation on create
|
||||||
#
|
#
|
||||||
|
@ -160,7 +160,7 @@ module AASM
|
||||||
|
|
||||||
module WriteStateWithoutPersistence
|
module WriteStateWithoutPersistence
|
||||||
# Writes <tt>state</tt> to the state column, but does not persist it to the database
|
# Writes <tt>state</tt> to the state column, but does not persist it to the database
|
||||||
#
|
#
|
||||||
# foo = Foo.find(1)
|
# foo = Foo.find(1)
|
||||||
# foo.aasm_current_state # => :opened
|
# foo.aasm_current_state # => :opened
|
||||||
# foo.close
|
# foo.close
|
||||||
|
@ -179,7 +179,7 @@ module AASM
|
||||||
module WriteState
|
module WriteState
|
||||||
# Writes <tt>state</tt> to the state column and persists it to the database
|
# Writes <tt>state</tt> to the state column and persists it to the database
|
||||||
# using update_attribute (which bypasses validation)
|
# using update_attribute (which bypasses validation)
|
||||||
#
|
#
|
||||||
# foo = Foo.find(1)
|
# foo = Foo.find(1)
|
||||||
# foo.aasm_current_state # => :opened
|
# foo.aasm_current_state # => :opened
|
||||||
# foo.close!
|
# foo.close!
|
||||||
|
@ -190,12 +190,12 @@ module AASM
|
||||||
def aasm_write_state(state)
|
def aasm_write_state(state)
|
||||||
old_value = read_attribute(self.class.aasm_column)
|
old_value = read_attribute(self.class.aasm_column)
|
||||||
write_attribute(self.class.aasm_column, state.to_s)
|
write_attribute(self.class.aasm_column, state.to_s)
|
||||||
|
|
||||||
unless self.save
|
unless self.save
|
||||||
write_attribute(self.class.aasm_column, old_value)
|
write_attribute(self.class.aasm_column, old_value)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -212,17 +212,17 @@ module AASM
|
||||||
# aasm_state :opened
|
# aasm_state :opened
|
||||||
# aasm_state :closed
|
# aasm_state :closed
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# foo = Foo.new
|
# foo = Foo.new
|
||||||
# foo.current_state # => :opened
|
# foo.current_state # => :opened
|
||||||
# foo.close
|
# foo.close
|
||||||
# foo.current_state # => :closed
|
# foo.current_state # => :closed
|
||||||
#
|
#
|
||||||
# foo = Foo.find(1)
|
# foo = Foo.find(1)
|
||||||
# foo.current_state # => :opened
|
# foo.current_state # => :opened
|
||||||
# foo.aasm_state = nil
|
# foo.aasm_state = nil
|
||||||
# foo.current_state # => nil
|
# foo.current_state # => nil
|
||||||
#
|
#
|
||||||
# NOTE: intended to be called from an event
|
# NOTE: intended to be called from an event
|
||||||
#
|
#
|
||||||
# This allows for nil aasm states - be sure to add validation to your model
|
# This allows for nil aasm states - be sure to add validation to your model
|
||||||
|
@ -238,8 +238,8 @@ module AASM
|
||||||
module NamedScopeMethods
|
module NamedScopeMethods
|
||||||
def aasm_state_with_named_scope name, options = {}
|
def aasm_state_with_named_scope name, options = {}
|
||||||
aasm_state_without_named_scope name, options
|
aasm_state_without_named_scope name, options
|
||||||
self.named_scope name, :conditions => {self.aasm_column => name.to_s} unless self.scopes.include?(name)
|
self.named_scope name, :conditions => {self.aasm_column => name.to_s} unless self.respond_to?(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,21 +25,21 @@ begin
|
||||||
class Fi < ActiveRecord::Base
|
class Fi < ActiveRecord::Base
|
||||||
def aasm_read_state
|
def aasm_read_state
|
||||||
"fi"
|
"fi"
|
||||||
end
|
end
|
||||||
include AASM
|
include AASM
|
||||||
end
|
end
|
||||||
|
|
||||||
class Fo < ActiveRecord::Base
|
class Fo < ActiveRecord::Base
|
||||||
def aasm_write_state(state)
|
def aasm_write_state(state)
|
||||||
"fo"
|
"fo"
|
||||||
end
|
end
|
||||||
include AASM
|
include AASM
|
||||||
end
|
end
|
||||||
|
|
||||||
class Fum < ActiveRecord::Base
|
class Fum < ActiveRecord::Base
|
||||||
def aasm_write_state_without_persistence(state)
|
def aasm_write_state_without_persistence(state)
|
||||||
"fum"
|
"fum"
|
||||||
end
|
end
|
||||||
include AASM
|
include AASM
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -47,17 +47,17 @@ begin
|
||||||
include AASM
|
include AASM
|
||||||
aasm_column :status
|
aasm_column :status
|
||||||
end
|
end
|
||||||
|
|
||||||
class Beaver < June
|
class Beaver < June
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "aasm model", :shared => true do
|
describe "aasm model", :shared => true do
|
||||||
it "should include AASM::Persistence::ActiveRecordPersistence" do
|
it "should include AASM::Persistence::ActiveRecordPersistence" do
|
||||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence)
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence)
|
||||||
end
|
end
|
||||||
it "should include AASM::Persistence::ActiveRecordPersistence::InstanceMethods" do
|
it "should include AASM::Persistence::ActiveRecordPersistence::InstanceMethods" do
|
||||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe FooBar, "class methods" do
|
describe FooBar, "class methods" do
|
||||||
|
@ -67,13 +67,13 @@ begin
|
||||||
it_should_behave_like "aasm model"
|
it_should_behave_like "aasm model"
|
||||||
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
||||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
||||||
end
|
end
|
||||||
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
||||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
||||||
end
|
end
|
||||||
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
||||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe Fi, "class methods" do
|
describe Fi, "class methods" do
|
||||||
|
@ -83,13 +83,13 @@ begin
|
||||||
it_should_behave_like "aasm model"
|
it_should_behave_like "aasm model"
|
||||||
it "should not include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
it "should not include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
||||||
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
||||||
end
|
end
|
||||||
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
||||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
||||||
end
|
end
|
||||||
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
||||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe Fo, "class methods" do
|
describe Fo, "class methods" do
|
||||||
|
@ -99,13 +99,13 @@ begin
|
||||||
it_should_behave_like "aasm model"
|
it_should_behave_like "aasm model"
|
||||||
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
||||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
||||||
end
|
end
|
||||||
it "should not include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
it "should not include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
||||||
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
||||||
end
|
end
|
||||||
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
||||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe Fum, "class methods" do
|
describe Fum, "class methods" do
|
||||||
|
@ -115,13 +115,13 @@ begin
|
||||||
it_should_behave_like "aasm model"
|
it_should_behave_like "aasm model"
|
||||||
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
||||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
||||||
end
|
end
|
||||||
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
||||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
||||||
end
|
end
|
||||||
it "should not include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
it "should not include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
||||||
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe FooBar, "instance methods" do
|
describe FooBar, "instance methods" do
|
||||||
|
@ -183,23 +183,42 @@ begin
|
||||||
foo.should_not_receive(:aasm_ensure_initial_state)
|
foo.should_not_receive(:aasm_ensure_initial_state)
|
||||||
foo.valid?
|
foo.valid?
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'Beavers' do
|
describe 'Beavers' do
|
||||||
it "should have the same states as it's parent" do
|
it "should have the same states as it's parent" do
|
||||||
Beaver.aasm_states.should == June.aasm_states
|
Beaver.aasm_states.should == June.aasm_states
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should have the same events as it's parent" do
|
it "should have the same events as it's parent" do
|
||||||
Beaver.aasm_events.should == June.aasm_events
|
Beaver.aasm_events.should == June.aasm_events
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should have the same column as it's parent" do
|
it "should have the same column as it's parent" do
|
||||||
Beaver.aasm_column.should == :status
|
Beaver.aasm_column.should == :status
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods do
|
||||||
|
class NamedScopeExample < ActiveRecord::Base
|
||||||
|
include AASM
|
||||||
|
end
|
||||||
|
|
||||||
|
context "Does not already respond_to? the scope name" do
|
||||||
|
it "should add a named_scope" do
|
||||||
|
NamedScopeExample.should_receive(:named_scope)
|
||||||
|
NamedScopeExample.aasm_state :unknown_scope
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "Already respond_to? the scope name" do
|
||||||
|
it "should not add a named_scope" do
|
||||||
|
NamedScopeExample.should_not_receive(:named_scope)
|
||||||
|
NamedScopeExample.aasm_state :new
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# TODO: figure out how to test ActiveRecord reload! without a database
|
# TODO: figure out how to test ActiveRecord reload! without a database
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue