Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert changes made to machine find_or_create method in #32 #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion Contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@
- @gmitrev
- @nblumoe
- @reiner
- @sanemat
- @sanemat
- @jonathan-wheeler
6 changes: 2 additions & 4 deletions lib/state_machines/machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,8 @@ def find_or_create(owner_class, *args, &block)
name = args.first || :state

# Find an existing machine
machine = owner_class.respond_to?(:state_machines) &&
(args.first && owner_class.state_machines[name] || !args.first &&
owner_class.state_machines.values.first) || nil

machine = owner_class.respond_to?(:state_machines) && owner_class.state_machines[name]

if machine
# Only create a new copy if changes are being made to the machine in
# a subclass
Expand Down
2 changes: 1 addition & 1 deletion test/functional/driver_default_nonstandard_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class DriverNonstandardTest < MiniTest::Test
def setup
@driver = Driver.new
@events = Driver.state_machine.events
@events = Driver.state_machine(:status).events
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to pass the name now?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without it this call would create a new state machine on state.

Any call to state_machine when not using the default name (:state) needs to also supply the namespace. I could update to always return a state machine if there is one? But though it would be better to keep it simple and consistent.

Can also amend the read me to depending on which way you decide to go.

end

def test_should_have
Expand Down
25 changes: 25 additions & 0 deletions test/functional/vehicle_insurance_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require_relative '../test_helper'
require_relative '../files/models/vehicle'

class VehicleInsuranceTest < MiniTest::Test
def setup
@vehicle = Vehicle.new
end

def test_insurance_state_should_be_in_inactive_state
assert_equal 'inactive', @vehicle.insurance_state
end

def test_insurance_should_be_inactive
assert @vehicle.insurance_inactive?
end

def test_should_allow_buy_insurance
assert @vehicle.buy_insurance
end

def test_insurance_should_be_active_after_buy_event
@vehicle.buy_insurance
assert @vehicle.insurance_active?
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require_relative '../../test_helper'

class MachineFinderWithOtherNamedExistingOnSameClassTest < StateMachinesTest
def setup
@klass = Class.new
@existing_machine = StateMachines::Machine.new(@klass, :status)
@machine = StateMachines::Machine.find_or_create(@klass)
end

def test_should_create_a_new_machine
refute_same @machine, @existing_machine
end
end