Skip to content

Add transition for initial state #2

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

Merged
merged 4 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions lib/statesman/machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,18 @@ def array_to_s_or_nil(input)
def initialize(object,
options = {
transition_class: Statesman::Adapters::MemoryTransition,
initial_transition: false,
})
@object = object
@transition_class = options[:transition_class]
@storage_adapter = adapter_class(@transition_class).new(
@transition_class, object, self, options
)

if options[:initial_transition]
@storage_adapter.create(nil, self.class.initial_state)
end

send(:after_initialize) if respond_to? :after_initialize
end

Expand Down
2 changes: 1 addition & 1 deletion spec/statesman/adapters/active_record_queries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def self.initial_state; end
end

it "does not raise an error" do
expect { check_missing_methods! }.to_not raise_exception(NotImplementedError)
expect { check_missing_methods! }.to_not raise_exception
Copy link
Owner Author

Choose a reason for hiding this comment

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

WARNING: Using `expect { }.not_to raise_error(SpecificErrorClass)` risks false positives, since literally any other error would cause the expectation to pass, including those raised by Ruby (e.g. NoMethodError, NameError and ArgumentError), meaning the code you are intending to test may not even get reached. Instead consider using `expect { }.not_to raise_error` or `expect { }.to raise_error(DifferentSpecificErrorClass)`.

end
end

Expand Down
57 changes: 57 additions & 0 deletions spec/statesman/machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,63 @@
expect(machine_instance.object).to be(my_model)
end

context "initial state is configured" do
before { machine.state(:x, initial: true) }

context "initial_transition is not provided" do
let(:options) { {} }

it "doesn't call .create on storage adapter" do
expect_any_instance_of(Statesman.storage_adapter).to_not receive(:create)
machine.new(my_model, options)
end

it "doesn't create a new transition object" do
instance = machine.new(my_model, options)

expect(instance.history.count).to eq(0)
end
end

context "initial_transition is provided" do
context "initial_transition is true" do
let(:options) do
{ initial_transition: true,
transition_class: Statesman::Adapters::MemoryTransition }
end

it "calls .create on storage adapter" do
expect_any_instance_of(Statesman.storage_adapter).to receive(:create).with(nil,
"x")
machine.new(my_model, options)
end

it "creates a new transition object" do
instance = machine.new(my_model, options)

expect(instance.history.count).to eq(1)
expect(instance.history.first).to be_a(Statesman::Adapters::MemoryTransition)
expect(instance.history.first.to_state).to eq("x")
end
end

context "initial_transition is false" do
let(:options) { { initial_transition: false } }

it "doesn't call .create on storage adapter" do
expect_any_instance_of(Statesman.storage_adapter).to_not receive(:create)
machine.new(my_model, options)
end

it "doesn't create a new transition object" do
instance = machine.new(my_model, options)

expect(instance.history.count).to eq(0)
end
end
end
end

context "transition class" do
it "sets a default" do
expect(Statesman.storage_adapter).to receive(:new).once.
Expand Down