state_machine 0.10.0 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,9 @@
1
1
  == master
2
2
 
3
+ == 0.10.1 / 2011-03-22
4
+
5
+ * Fix classes with multiple state machines failing to initialize in ActiveRecord / Mongoid / Sequel integrations
6
+
3
7
  == 0.10.0 / 2011-03-19
4
8
 
5
9
  * Support callback terminators in MongoMapper 0.9.0+
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'rake/gempackagetask'
6
6
 
7
7
  spec = Gem::Specification.new do |s|
8
8
  s.name = 'state_machine'
9
- s.version = '0.10.0'
9
+ s.version = '0.10.1'
10
10
  s.platform = Gem::Platform::RUBY
11
11
  s.summary = 'Adds support for creating state machines for attributes on any Ruby class'
12
12
  s.description = s.summary
@@ -373,10 +373,7 @@ module StateMachine
373
373
  # created with a set of attributes that includes this machine's
374
374
  # attribute.
375
375
  def initialize_state?(object, options)
376
- if object.new_record? && !object.instance_variable_defined?('@initialized_state_machines')
377
- object.instance_variable_set('@initialized_state_machines', true)
378
- super
379
- end
376
+ super if object.new_record?
380
377
  end
381
378
 
382
379
  # Defines an initialization hook into the owner class for setting the
@@ -392,7 +389,12 @@ module StateMachine
392
389
  # Hooks in to attribute initialization to set the states *prior*
393
390
  # to the attributes being set
394
391
  define_helper(:instance, :attributes=) do |machine, object, _super, new_attributes, *|
395
- object.class.state_machines.initialize_states(object, :attributes => new_attributes) { _super.call }
392
+ if !object.instance_variable_defined?('@initialized_state_machines')
393
+ object.class.state_machines.initialize_states(object, :attributes => new_attributes) { _super.call }
394
+ object.instance_variable_set('@initialized_state_machines', true)
395
+ else
396
+ _super.call
397
+ end
396
398
  end
397
399
  end
398
400
 
@@ -234,10 +234,7 @@ module StateMachine
234
234
  # created with a set of attributes that includes this machine's
235
235
  # attribute.
236
236
  def initialize_state?(object, options)
237
- if object.new_record? && !object.instance_variable_defined?('@initialized_state_machines')
238
- object.instance_variable_set('@initialized_state_machines', true)
239
- super
240
- end
237
+ super if object.new_record?
241
238
  end
242
239
 
243
240
  # Defines an initialization hook into the owner class for setting the
@@ -245,7 +242,12 @@ module StateMachine
245
242
  # object
246
243
  def define_state_initializer
247
244
  define_helper(:instance, :process) do |machine, object, _super, *args|
248
- object.class.state_machines.initialize_states(object, :attributes => args.first) { _super.call }
245
+ if !object.instance_variable_defined?('@initialized_state_machines')
246
+ object.class.state_machines.initialize_states(object, :attributes => args.first) { _super.call }
247
+ object.instance_variable_set('@initialized_state_machines', true)
248
+ else
249
+ _super.call
250
+ end
249
251
  end
250
252
  end
251
253
 
@@ -270,9 +270,7 @@ module StateMachine
270
270
  # created with a set of attributes that includes this machine's
271
271
  # attribute.
272
272
  def initialize_state?(object, options)
273
- if object.new? && !object.instance_variable_defined?('@initialized_state_machines')
274
- object.instance_variable_set('@initialized_state_machines', true)
275
-
273
+ if object.new?
276
274
  attributes = options[:attributes] || {}
277
275
  ignore = object.send(:setter_methods, nil, nil).map {|setter| setter.chop.to_sym} & attributes.keys.map {|key| key.to_sym}
278
276
  !ignore.map {|attribute| attribute.to_sym}.include?(attribute)
@@ -286,7 +284,12 @@ module StateMachine
286
284
  # Hooks in to attribute initialization to set the states *prior* to
287
285
  # the attributes being set
288
286
  define_helper(:instance, :set) do |machine, object, _super, *args|
289
- object.class.state_machines.initialize_states(object, :attributes => args.first) { _super.call }
287
+ if !object.instance_variable_defined?('@initialized_state_machines')
288
+ object.class.state_machines.initialize_states(object, :attributes => args.first) { _super.call }
289
+ object.instance_variable_set('@initialized_state_machines', true)
290
+ else
291
+ _super.call
292
+ end
290
293
  end
291
294
  end
292
295
 
@@ -266,6 +266,23 @@ module ActiveModelTest
266
266
  end
267
267
  end
268
268
 
269
+ class MachineMultipleTest < BaseTestCase
270
+ def setup
271
+ @model = new_model do
272
+ model_attribute :status
273
+ end
274
+
275
+ @state_machine = StateMachine::Machine.new(@model, :initial => :parked, :integration => :active_model)
276
+ @status_machine = StateMachine::Machine.new(@model, :status, :initial => :idling, :integration => :active_model)
277
+ end
278
+
279
+ def test_should_should_initialize_each_state
280
+ record = @model.new
281
+ assert_equal 'parked', record.state
282
+ assert_equal 'idling', record.status
283
+ end
284
+ end
285
+
269
286
  class MachineWithDirtyAttributesTest < BaseTestCase
270
287
  def setup
271
288
  @model = new_model do
@@ -513,6 +513,22 @@ module ActiveRecordTest
513
513
  end
514
514
  end
515
515
 
516
+ class MachineMultipleTest < BaseTestCase
517
+ def setup
518
+ @model = new_model do
519
+ connection.add_column :foo, :status, :string
520
+ end
521
+ @state_machine = StateMachine::Machine.new(@model, :initial => :parked)
522
+ @status_machine = StateMachine::Machine.new(@model, :status, :initial => :idling)
523
+ end
524
+
525
+ def test_should_should_initialize_each_state
526
+ record = @model.new
527
+ assert_equal 'parked', record.state
528
+ assert_equal 'idling', record.status
529
+ end
530
+ end
531
+
516
532
  class MachineWithLoopbackTest < BaseTestCase
517
533
  def setup
518
534
  @model = new_model do
@@ -454,6 +454,23 @@ module DataMapperTest
454
454
  end
455
455
  end
456
456
 
457
+ class MachineMultipleTest < BaseTestCase
458
+ def setup
459
+ @resource = new_resource do
460
+ property :status, String
461
+ auto_migrate!
462
+ end
463
+ @state_machine = StateMachine::Machine.new(@resource, :initial => :parked)
464
+ @status_machine = StateMachine::Machine.new(@resource, :status, :initial => :idling)
465
+ end
466
+
467
+ def test_should_should_initialize_each_state
468
+ record = @resource.new
469
+ assert_equal 'parked', record.state
470
+ assert_equal 'idling', record.status
471
+ end
472
+ end
473
+
457
474
  class MachineWithLoopbackTest < BaseTestCase
458
475
  def setup
459
476
  @resource = new_resource do
@@ -450,6 +450,22 @@ module MongoMapperTest
450
450
  end
451
451
  end
452
452
 
453
+ class MachineMultipleTest < BaseTestCase
454
+ def setup
455
+ @model = new_model do
456
+ key :status, String, :default => 'idling'
457
+ end
458
+ @state_machine = StateMachine::Machine.new(@model, :initial => :parked)
459
+ @status_machine = StateMachine::Machine.new(@model, :status, :initial => :idling)
460
+ end
461
+
462
+ def test_should_should_initialize_each_state
463
+ record = @model.new
464
+ assert_equal 'parked', record.state
465
+ assert_equal 'idling', record.status
466
+ end
467
+ end
468
+
453
469
  class MachineWithLoopbackTest < BaseTestCase
454
470
  def setup
455
471
  @model = new_model do
@@ -424,6 +424,22 @@ module MongoidTest
424
424
  end
425
425
  end
426
426
 
427
+ class MachineMultipleTest < BaseTestCase
428
+ def setup
429
+ @model = new_model do
430
+ key :status, String, :default => 'idling'
431
+ end
432
+ @state_machine = StateMachine::Machine.new(@model, :initial => :parked)
433
+ @status_machine = StateMachine::Machine.new(@model, :status, :initial => :idling)
434
+ end
435
+
436
+ def test_should_should_initialize_each_state
437
+ record = @model.new
438
+ assert_equal 'parked', record.state
439
+ assert_equal 'idling', record.status
440
+ end
441
+ end
442
+
427
443
  class MachineWithLoopbackTest < BaseTestCase
428
444
  def setup
429
445
  @model = new_model do
@@ -453,6 +453,25 @@ module SequelTest
453
453
  end
454
454
  end
455
455
 
456
+ class MachineMultipleTest < BaseTestCase
457
+ def setup
458
+ @model = new_model
459
+ DB.alter_table :foo do
460
+ add_column :status, :string, :default => 'idling'
461
+ end
462
+ @model.class_eval { get_db_schema(true) }
463
+
464
+ @state_machine = StateMachine::Machine.new(@model, :initial => :parked)
465
+ @status_machine = StateMachine::Machine.new(@model, :status, :initial => :idling)
466
+ end
467
+
468
+ def test_should_should_initialize_each_state
469
+ record = @model.new
470
+ assert_equal 'parked', record.state
471
+ assert_equal 'idling', record.status
472
+ end
473
+ end
474
+
456
475
  class MachineWithAliasedAttributeTest < BaseTestCase
457
476
  def setup
458
477
  @model = new_model do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_machine
3
3
  version: !ruby/object:Gem::Version
4
- hash: 55
5
- prerelease: false
4
+ hash: 53
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 10
9
- - 0
10
- version: 0.10.0
9
+ - 1
10
+ version: 0.10.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Aaron Pfeifer
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-19 00:00:00 -04:00
18
+ date: 2011-03-22 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -165,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
165
  requirements: []
166
166
 
167
167
  rubyforge_project: pluginaweek
168
- rubygems_version: 1.3.7
168
+ rubygems_version: 1.6.2
169
169
  signing_key:
170
170
  specification_version: 3
171
171
  summary: Adds support for creating state machines for attributes on any Ruby class