Skip to content

Commit 8873a21

Browse files
committed
chore: update validations and add setup scripts
1 parent 3ae1274 commit 8873a21

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

README.md

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Or install it yourself as:
2323

2424
## Dependencies
2525

26-
Active Model 5.1+
26+
Active Model 6.0+
2727

2828
## Usage
2929

@@ -36,19 +36,19 @@ class Vehicle
3636
attr_accessor :state
3737
define_attribute_methods [:state]
3838

39-
state_machine :initial => :parked do
40-
before_transition :parked => any - :parked, :do => :put_on_seatbelt
41-
after_transition any => :parked do |vehicle, transition|
39+
state_machine initial: :parked do
40+
before_transition parked: any - :parked, do: :put_on_seatbelt
41+
after_transition any: :parked do |vehicle, transition|
4242
vehicle.seatbelt = 'off'
4343
end
4444
around_transition :benchmark
4545

46-
event :ignite do
47-
transition :parked => :idling
46+
event ignite: do
47+
transition parked: :idling
4848
end
4949

5050
state :first_gear, :second_gear do
51-
validates_presence_of :seatbelt_on
51+
validates :seatbelt_on, presence: true
5252
end
5353
end
5454

@@ -62,24 +62,6 @@ class Vehicle
6262
...
6363
end
6464
end
65-
66-
class VehicleObserver < ActiveModel::Observer
67-
# Callback for :ignite event *before* the transition is performed
68-
def before_ignite(vehicle, transition)
69-
# log message
70-
end
71-
72-
# Generic transition callback *after* the transition is performed
73-
def after_transition(vehicle, transition)
74-
Audit.log(vehicle, transition)
75-
end
76-
77-
# Generic callback after the transition fails to perform
78-
def after_failure_to_transition(vehicle, transition)
79-
Audit.error(vehicle, transition)
80-
end
81-
end
82-
8365
```
8466

8567
## Contributing

bin/console

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "bundler/setup"
5+
require "state_machines-activemodel"
6+
7+
# You can add fixtures and/or initialization code here to make experimenting
8+
# with your gem easier. You can also use a different console, if you like.
9+
10+
# (If you use this, don't forget to add pry to your Gemfile!)
11+
# require "pry"
12+
# Pry.start
13+
14+
require "irb"
15+
IRB.start(__FILE__)

bin/setup

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
set -vx
5+
6+
bundle install
7+
8+
# Do any other automated setup that you need to do here

test/machine_with_state_driven_validations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def setup
99

1010
@machine = StateMachines::Machine.new(@model)
1111
@machine.state :first_gear, :second_gear do
12-
validates_presence_of :seatbelt
12+
validates :seatbelt, presence: true
1313
end
1414
@machine.other_states :parked
1515
end

0 commit comments

Comments
 (0)