Skip to content

Commit

Permalink
bugfix: initialize the aasm state column after initialization of the …
Browse files Browse the repository at this point in the history
…_ActiveRecord_ instance #191
  • Loading branch information
alto committed Dec 4, 2014
1 parent ec82d2c commit 13c8e96
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 32 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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

Expand Down
13 changes: 4 additions & 9 deletions lib/aasm/persistence/active_record_persistence.rb
Expand Up @@ -33,15 +33,10 @@ def self.included(base)
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
Expand Down Expand Up @@ -197,5 +192,5 @@ def aasm_validate_states
end # InstanceMethods

end
end
end
end # Persistence
end # AASM
4 changes: 2 additions & 2 deletions lib/aasm/persistence/mongoid_persistence.rb
Expand Up @@ -133,5 +133,5 @@ def aasm_state_with_named_scope name, options = {}
end
end
end
end
end
end # Persistence
end # AASM
6 changes: 5 additions & 1 deletion spec/database.rb
@@ -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"
Expand Down
14 changes: 13 additions & 1 deletion spec/models/persistence.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
21 changes: 3 additions & 18 deletions spec/unit/persistence/active_record_persistence_spec.rb
Expand Up @@ -76,12 +76,10 @@
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

Expand All @@ -101,12 +99,10 @@
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

Expand Down Expand Up @@ -256,20 +252,9 @@
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
Expand Down

0 comments on commit 13c8e96

Please sign in to comment.