pluginaweek / state_machine

Adds support for creating state machines for attributes on any Ruby class
http://www.pluginaweek.org
MIT License
3.74k stars 510 forks source link

repeated "validate"-style validations across different states do not validate at all #265

Open zampino opened 11 years ago

zampino commented 11 years ago

Here a minimal example to be run in irb.

If I validate with the same method across (at least) two different states the validation isn't called.

require 'active_record'
require 'state_machine'

ActiveRecord::Base.
  establish_connection(:adapter  => 'sqlite3', :database => ':memory:')

ActiveRecord::Base.connection.create_table( :models ) do |t|
  t.string :property
  t.string :state
end

class Model < ActiveRecord::Base

  def property_is_nice
    fail
  end

  state_machine :state, :initial => :foo do

    state :a do
      validate :property_is_nice
    end

    state :b do
      validate :property_is_nice
    end

    event :to_a do
      transition :foo => :a
    end

    event :to_b do
      transition :a => :b
    end
  end
end

@foo = Model.create property: "nice"

@foo.to_a #=> true (doesn't fail!)

@foo.state # =>  "a"

Is this a known bug? This does not happen if I use validates style validations like

    state :a do
      validates :property => :nice
    end

    state :b do
      validates :property => :nice
    end
the8472 commented 11 years ago

That's documented in the API: http://rdoc.info/github/pluginaweek/state_machine/master/StateMachine/Integrations/ActiveModel