bugfix: initialize the aasm state column after initialization of the _ActiveRecord_ instance #191

This commit is contained in:
Thorsten Böttger 2014-12-05 00:07:38 +01:00
parent ec82d2c4c3
commit 13c8e96fe7
6 changed files with 29 additions and 32 deletions

View File

@ -7,7 +7,8 @@
## 4.0.4 (not yet released)
* bugfix: avoid Rails autoloading conflicts (see [issue #137](https://github.com/aasm/aasm/issues/137) and see [issue #139](https://github.com/aasm/aasm/issues/139) for details)
* bugfix: initialize the aasm state column after initialization of the _ActiveRecord_ instance (see [issue #191](https://github.com/aasm/aasm/issues/191) for details)
* bugfix: avoid Rails autoloading conflicts (see [issue #137](https://github.com/aasm/aasm/issues/137) and [issue #139](https://github.com/aasm/aasm/issues/139) for details)
## 4.0.3

View File

@ -33,15 +33,10 @@ module AASM
base.extend AASM::Persistence::ActiveRecordPersistence::ClassMethods
base.send(:include, AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
if ActiveRecord::VERSION::MAJOR >= 3
base.before_validation(:aasm_ensure_initial_state, :on => :create)
else
base.before_validation_on_create(:aasm_ensure_initial_state)
base.after_initialize do
aasm_ensure_initial_state
end
# ensure initial aasm state even when validations are skipped
base.before_create(:aasm_ensure_initial_state)
# ensure state is in the list of states
base.validate :aasm_validate_states
end
@ -197,5 +192,5 @@ module AASM
end # InstanceMethods
end
end
end
end # Persistence
end # AASM

View File

@ -133,5 +133,5 @@ module AASM
end
end
end
end
end
end # Persistence
end # AASM

View File

@ -1,10 +1,14 @@
ActiveRecord::Migration.suppress_messages do
%w{gates readers writers transients simples simple_new_dsls no_scopes no_direct_assignments thieves localizer_test_models persisted_states provided_and_persisted_states with_enums with_true_enums with_false_enums}.each do |table_name|
%w{gates readers writers transients simples no_scopes no_direct_assignments thieves localizer_test_models persisted_states provided_and_persisted_states with_enums with_true_enums with_false_enums}.each do |table_name|
ActiveRecord::Migration.create_table table_name, :force => true do |t|
t.string "aasm_state"
end
end
ActiveRecord::Migration.create_table "simple_new_dsls", :force => true do |t|
t.string "status"
end
ActiveRecord::Migration.create_table "validators", :force => true do |t|
t.string "name"
t.string "status"

View File

@ -2,7 +2,11 @@ class Gate < ActiveRecord::Base
include AASM
# Fake this column for testing purposes
attr_accessor :aasm_state
# attr_accessor :aasm_state
def value
'value'
end
aasm do
state :opened
@ -20,6 +24,10 @@ class WithEnum < ActiveRecord::Base
# Fake this column for testing purposes
attr_accessor :aasm_state
def self.test
{}
end
aasm :enum => :test do
state :opened
state :closed
@ -36,6 +44,10 @@ class WithTrueEnum < ActiveRecord::Base
# Fake this column for testing purposes
attr_accessor :aasm_state
def value
'value'
end
aasm :enum => true do
state :opened
state :closed

View File

@ -76,12 +76,10 @@ describe "instance methods" do
let(:with_true_enum) { WithTrueEnum.new }
before :each do
WithTrueEnum.aasm.stub(:attribute_name).and_return(:value)
with_true_enum.stub(:aasm_guess_enum_method).and_return(:values)
end
it "infers enum method name from pluralized column name" do
expect(with_true_enum.send(:aasm_enum)).to eq :values
expect(with_true_enum).to have_received :aasm_guess_enum_method
end
end
@ -101,12 +99,10 @@ describe "instance methods" do
context "when AASM column looks like enum" do
before :each do
gate.stub(:aasm_column_looks_like_enum).and_return(true)
gate.stub(:aasm_guess_enum_method).and_return(:values)
end
it "infers enum method name from pluralized column name" do
expect(gate.send(:aasm_enum)).to eq :values
expect(gate).to have_received :aasm_guess_enum_method
end
end
@ -256,20 +252,9 @@ describe "instance methods" do
expect(gate.aasm.current_state).to be_nil
end
it "should call aasm_ensure_initial_state on validation before create" do
expect(gate).to receive(:aasm_ensure_initial_state).and_return(true)
gate.valid?
end
it "should call aasm_ensure_initial_state before create, even if skipping validations" do
expect(gate).to receive(:aasm_ensure_initial_state).and_return(true)
gate.save(:validate => false)
end
it "should not call aasm_ensure_initial_state on validation before update" do
allow(gate).to receive(:new_record?).and_return(false)
expect(gate).not_to receive(:aasm_ensure_initial_state)
gate.valid?
it "should initialize the aasm state on instantiation" do
expect(Gate.new.aasm_state).to eql 'opened'
expect(Gate.new.aasm.current_state).to eql :opened
end
end