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::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.extend(AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods)
|
||||
|
||||
|
||||
base.class_eval do
|
||||
class << self
|
||||
alias_method :aasm_state_without_named_scope, :aasm_state
|
||||
|
@ -48,7 +48,7 @@ module AASM
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
base.before_validation_on_create :aasm_ensure_initial_state
|
||||
end
|
||||
|
||||
|
@ -119,7 +119,7 @@ module AASM
|
|||
|
||||
# Returns the current aasm_state of the object. Respects reload and
|
||||
# any changes made to the aasm_state field directly
|
||||
#
|
||||
#
|
||||
# Internally just calls <tt>aasm_read_state</tt>
|
||||
#
|
||||
# foo = Foo.find(1)
|
||||
|
@ -136,7 +136,7 @@ module AASM
|
|||
end
|
||||
|
||||
private
|
||||
|
||||
|
||||
# Ensures that if the aasm_state column is nil and the record is new
|
||||
# that the initial state gets populated before validation on create
|
||||
#
|
||||
|
@ -160,7 +160,7 @@ module AASM
|
|||
|
||||
module WriteStateWithoutPersistence
|
||||
# Writes <tt>state</tt> to the state column, but does not persist it to the database
|
||||
#
|
||||
#
|
||||
# foo = Foo.find(1)
|
||||
# foo.aasm_current_state # => :opened
|
||||
# foo.close
|
||||
|
@ -179,7 +179,7 @@ module AASM
|
|||
module WriteState
|
||||
# Writes <tt>state</tt> to the state column and persists it to the database
|
||||
# using update_attribute (which bypasses validation)
|
||||
#
|
||||
#
|
||||
# foo = Foo.find(1)
|
||||
# foo.aasm_current_state # => :opened
|
||||
# foo.close!
|
||||
|
@ -190,12 +190,12 @@ module AASM
|
|||
def aasm_write_state(state)
|
||||
old_value = read_attribute(self.class.aasm_column)
|
||||
write_attribute(self.class.aasm_column, state.to_s)
|
||||
|
||||
|
||||
unless self.save
|
||||
write_attribute(self.class.aasm_column, old_value)
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
true
|
||||
end
|
||||
end
|
||||
|
@ -212,17 +212,17 @@ module AASM
|
|||
# aasm_state :opened
|
||||
# aasm_state :closed
|
||||
# end
|
||||
#
|
||||
#
|
||||
# foo = Foo.new
|
||||
# foo.current_state # => :opened
|
||||
# foo.close
|
||||
# foo.current_state # => :closed
|
||||
#
|
||||
#
|
||||
# foo = Foo.find(1)
|
||||
# foo.current_state # => :opened
|
||||
# foo.aasm_state = nil
|
||||
# foo.current_state # => nil
|
||||
#
|
||||
#
|
||||
# NOTE: intended to be called from an event
|
||||
#
|
||||
# This allows for nil aasm states - be sure to add validation to your model
|
||||
|
@ -238,8 +238,8 @@ module AASM
|
|||
module NamedScopeMethods
|
||||
def aasm_state_with_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)
|
||||
end
|
||||
self.named_scope name, :conditions => {self.aasm_column => name.to_s} unless self.respond_to?(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -25,21 +25,21 @@ begin
|
|||
class Fi < ActiveRecord::Base
|
||||
def aasm_read_state
|
||||
"fi"
|
||||
end
|
||||
end
|
||||
include AASM
|
||||
end
|
||||
|
||||
class Fo < ActiveRecord::Base
|
||||
def aasm_write_state(state)
|
||||
"fo"
|
||||
end
|
||||
end
|
||||
include AASM
|
||||
end
|
||||
|
||||
class Fum < ActiveRecord::Base
|
||||
def aasm_write_state_without_persistence(state)
|
||||
"fum"
|
||||
end
|
||||
end
|
||||
include AASM
|
||||
end
|
||||
|
||||
|
@ -47,17 +47,17 @@ begin
|
|||
include AASM
|
||||
aasm_column :status
|
||||
end
|
||||
|
||||
|
||||
class Beaver < June
|
||||
end
|
||||
|
||||
describe "aasm model", :shared => true do
|
||||
it "should include AASM::Persistence::ActiveRecordPersistence" do
|
||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence)
|
||||
end
|
||||
end
|
||||
it "should include AASM::Persistence::ActiveRecordPersistence::InstanceMethods" do
|
||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe FooBar, "class methods" do
|
||||
|
@ -67,13 +67,13 @@ begin
|
|||
it_should_behave_like "aasm model"
|
||||
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
||||
end
|
||||
end
|
||||
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
||||
end
|
||||
end
|
||||
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Fi, "class methods" do
|
||||
|
@ -83,13 +83,13 @@ begin
|
|||
it_should_behave_like "aasm model"
|
||||
it "should not include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
||||
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
||||
end
|
||||
end
|
||||
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
||||
end
|
||||
end
|
||||
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Fo, "class methods" do
|
||||
|
@ -99,13 +99,13 @@ begin
|
|||
it_should_behave_like "aasm model"
|
||||
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
||||
end
|
||||
end
|
||||
it "should not include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
||||
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
||||
end
|
||||
end
|
||||
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Fum, "class methods" do
|
||||
|
@ -115,13 +115,13 @@ begin
|
|||
it_should_behave_like "aasm model"
|
||||
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
||||
end
|
||||
end
|
||||
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
||||
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
||||
end
|
||||
end
|
||||
it "should not include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
||||
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe FooBar, "instance methods" do
|
||||
|
@ -183,23 +183,42 @@ begin
|
|||
foo.should_not_receive(:aasm_ensure_initial_state)
|
||||
foo.valid?
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
describe 'Beavers' do
|
||||
it "should have the same states as it's parent" do
|
||||
Beaver.aasm_states.should == June.aasm_states
|
||||
end
|
||||
|
||||
|
||||
it "should have the same events as it's parent" do
|
||||
Beaver.aasm_events.should == June.aasm_events
|
||||
end
|
||||
|
||||
|
||||
it "should have the same column as it's parent" do
|
||||
Beaver.aasm_column.should == :status
|
||||
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
|
||||
|
||||
|
|
Loading…
Reference in a new issue