Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions lib/rails_state_machine/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def state_machine_state_manager(state_attribute)
@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 = nil
end

Comment thread
LisaHader marked this conversation as resolved.
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
15 changes: 15 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,19 @@
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 }
Comment thread
LisaHader marked this conversation as resolved.
Outdated

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

end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

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

RSpec::Matchers.define_negated_matcher :not_change, :change
Comment thread
LisaHader marked this conversation as resolved.
Outdated

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'
Expand Down