Skip to content

Commit

Permalink
#101 docs(models): examples show models inheriting ApplicationRecord (
Browse files Browse the repository at this point in the history
  • Loading branch information
casaper authored Jul 25, 2022
1 parent 94662c6 commit 8b34ac5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ For the complete usage guide, see http://www.rubydoc.info/github/state-machines/
### Example

```ruby
class Vehicle < ActiveRecord::Base
class Vehicle < ApplicationRecord
state_machine :initial => :parked do
before_transition :parked => any - :parked, :do => :put_on_seatbelt
after_transition any => :parked do |vehicle, transition|
Expand All @@ -40,7 +40,7 @@ class Vehicle < ActiveRecord::Base
end

state :first_gear, :second_gear do
validates_presence_of :seatbelt_on
validates :seatbelt_on, presence: true
end
end

Expand Down Expand Up @@ -73,7 +73,7 @@ framework, custom validators will not work as expected when defined to run
in multiple states. For example:

```ruby
class Vehicle < ActiveRecord::Base
class Vehicle < ApplicationRecord
state_machine do
state :first_gear, :second_gear do
validate :speed_is_legal
Expand All @@ -87,7 +87,7 @@ for the <tt>:second_gear</tt> state. To avoid this, you can define your
custom validation like so:

```ruby
class Vehicle < ActiveRecord::Base
class Vehicle < ApplicationRecord
state_machine do
state :first_gear, :second_gear do
validate {|vehicle| vehicle.speed_is_legal}
Expand Down
23 changes: 11 additions & 12 deletions lib/state_machines/integrations/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Integrations #:nodoc:
# Below is an example of a simple state machine defined within an
# ActiveRecord model:
#
# class Vehicle < ActiveRecord::Base
# class Vehicle < ApplicationRecord
# state_machine :initial => :parked do
# event :ignite do
# transition :parked => :idling
Expand Down Expand Up @@ -82,7 +82,7 @@ module Integrations #:nodoc:
# users from tampering with events through URLs / forms, the attribute
# should be protected like so:
#
# class Vehicle < ActiveRecord::Base
# class Vehicle < ApplicationRecord
# attr_protected :state_event
# # attr_accessible ... # Alternative technique
#
Expand All @@ -94,7 +94,7 @@ module Integrations #:nodoc:
# If you want to only have *some* events be able to fire via mass-assignment,
# you can build two state machines (one public and one protected) like so:
#
# class Vehicle < ActiveRecord::Base
# class Vehicle < ApplicationRecord
# attr_protected :state_event # Prevent access to events in the first machine
#
# state_machine do
Expand All @@ -115,7 +115,7 @@ module Integrations #:nodoc:
#
# For example,
#
# class Message < ActiveRecord::Base
# class Message < ApplicationRecord
# end
#
# Vehicle.state_machine do
Expand All @@ -136,7 +136,7 @@ module Integrations #:nodoc:
#
# To turn off transactions:
#
# class Vehicle < ActiveRecord::Base
# class Vehicle < ApplicationRecord
# state_machine :initial => :parked, :use_transactions => false do
# ...
# end
Expand All @@ -150,7 +150,7 @@ module Integrations #:nodoc:
# framework, custom validators will not work as expected when defined to run
# in multiple states. For example:
#
# class Vehicle < ActiveRecord::Base
# class Vehicle < ApplicationRecord
# state_machine do
# ...
# state :first_gear, :second_gear do
Expand All @@ -163,7 +163,7 @@ module Integrations #:nodoc:
# for the <tt>:second_gear</tt> state. To avoid this, you can define your
# custom validation like so:
#
# class Vehicle < ActiveRecord::Base
# class Vehicle < ApplicationRecord
# state_machine do
# ...
# state :first_gear, :second_gear do
Expand Down Expand Up @@ -206,8 +206,7 @@ module Integrations #:nodoc:
# These named scopes are essentially the functional equivalent of the
# following definitions:
#
# class Vehicle < ActiveRecord::Base
# named_scope :with_states, lambda {|*states| {:conditions => {:state => states}}}
# class Vehicle < ApplicationRecord
# # with_states also aliased to with_state
#
# named_scope :without_states, lambda {|*states| {:conditions => ['state NOT IN (?)', states]}}
Expand Down Expand Up @@ -235,7 +234,7 @@ module Integrations #:nodoc:
#
# For example,
#
# class Vehicle < ActiveRecord::Base
# class Vehicle < ApplicationRecord
# state_machine :initial => :parked do
# before_transition any => :idling do |vehicle|
# vehicle.put_on_seatbelt
Expand Down Expand Up @@ -266,11 +265,11 @@ module Integrations #:nodoc:
# ActiveRecord, a save failure will cause any records that get created in
# your callback to roll back. You can work around this issue like so:
#
# class TransitionLog < ActiveRecord::Base
# class TransitionLog < ApplicationRecord
# establish_connection Rails.env.to_sym
# end
#
# class Vehicle < ActiveRecord::Base
# class Vehicle < ApplicationRecord
# state_machine do
# after_failure do |vehicle, transition|
# TransitionLog.create(:vehicle => vehicle, :transition => transition)
Expand Down
6 changes: 3 additions & 3 deletions test/machine_with_event_attributes_on_validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_should_not_run_after_callbacks
def test_should_not_run_after_callbacks_with_failures_disabled_if_validation_fails
@model.class_eval do
attr_accessor :seatbelt
validates_presence_of :seatbelt
validates :seatbelt, presence: true
end

ran_callback = false
Expand All @@ -78,7 +78,7 @@ def test_should_not_run_after_callbacks_with_failures_disabled_if_validation_fai
def test_should_run_after_callbacks_if_validation_fails
@model.class_eval do
attr_accessor :seatbelt
validates_presence_of :seatbelt
validates :seatbelt, presence: true
end

ran_callback = false
Expand All @@ -103,7 +103,7 @@ def test_should_not_run_around_callbacks_after_yield
def test_should_not_run_around_callbacks_after_yield_with_failures_disabled_if_validation_fails
@model.class_eval do
attr_accessor :seatbelt
validates_presence_of :seatbelt
validates :seatbelt, presence: true
end

ran_callback = false
Expand Down
2 changes: 1 addition & 1 deletion test/machine_with_state_driven_validations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def setup

@machine = StateMachines::Machine.new(@model)
@machine.state :first_gear, :second_gear do
validates_presence_of :seatbelt
validates :seatbelt, presence: true
end
@machine.other_states :parked
end
Expand Down

0 comments on commit 8b34ac5

Please sign in to comment.