Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
## Unreleased

### Compatible changes
- Fix bug where duplicated model instances referenced shared state machine state managers.

### Breaking changes

Expand Down
5 changes: 4 additions & 1 deletion lib/rails_state_machine/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ def state_events(state_attribute = DEFAULT_STATE_ATTRIBUTE)
private

def state_machine_state_manager(state_attribute)
@state_machine_state_managers ||= {}
@state_machine_state_managers[state_attribute] ||= StateManager.new(self, state_machine(state_attribute), state_attribute)
end

def clear_state_machine_state_managers_cache
@state_machine_state_managers = {}
end

def state_machine_state_managers
self.state_machines.keys.collect do |state_attribute|
state_machine_state_manager(state_attribute)
Expand Down
1 change: 1 addition & 0 deletions lib/rails_state_machine/state_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def register_initial_state
return unless initial_state_name

@model.after_initialize do
clear_state_machine_state_managers_cache
manager = state_machine_state_manager(state_attribute)
if new_record? && !manager.state
manager.state = initial_state_name
Expand Down
16 changes: 16 additions & 0 deletions spec/rails_state_machine/state_machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,20 @@
end
end

it 'does use different state machine objects for duplicated records' do
parcel = Parcel.create!(weight: 1)
parcel_dup = parcel.dup
parcel_dup.save!

expect(parcel_dup.may_pack?).to be true
expect(parcel.may_pack?).to be true

expect { parcel.pack_and_ship! }
.to change { parcel.reload.state }.from('empty').to('shipped')
.and not_change { parcel_dup.reload.state }

expect(parcel_dup.may_pack?).to be true
expect(parcel.may_pack?).to be false
end

end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rails_state_machine'
require 'gemika'

Dir["#{File.dirname(__FILE__)}/support/*.rb"].sort.each {|f| require f}
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each {|f| require f}

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand Down
1 change: 1 addition & 0 deletions spec/support/matchers/not_change.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RSpec::Matchers.define_negated_matcher :not_change, :change