state_machine 0.10.2 → 0.10.3

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.3 / 2011-04-07
4
+
5
+ * Fix state initialization failing in ActiveRecord 3.0.2+ when using with_state scopes for the default scope
6
+
3
7
  == 0.10.2 / 2011-03-31
4
8
 
5
9
  * Use more integrated state initialization hooks for ActiveRecord, Mongoid, and Sequel
data/README.rdoc CHANGED
@@ -459,7 +459,7 @@ machines, see StateMachine::Integrations::DataMapper.
459
459
  === Mongoid
460
460
 
461
461
  The Mongoid integration adds support for automatically saving the record,
462
- basic scopes, validation errors and callbacks. For example,
462
+ basic scopes, validation errors, and observers. For example,
463
463
 
464
464
  class Vehicle
465
465
  include Mongoid::Document
@@ -490,6 +490,18 @@ basic scopes, validation errors and callbacks. For example,
490
490
  ...
491
491
  end
492
492
  end
493
+
494
+ class VehicleObserver < Mongoid::Observer
495
+ # Callback for :ignite event *before* the transition is performed
496
+ def before_ignite(vehicle, transition)
497
+ # log message
498
+ end
499
+
500
+ # Generic transition callback *after* the transition is performed
501
+ def after_transition(vehicle, transition)
502
+ Audit.log(vehicle, transition)
503
+ end
504
+ end
493
505
 
494
506
  For more information about the various behaviors added for Mongoid state
495
507
  machines, see StateMachine::Integrations::Mongoid.
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.2'
9
+ s.version = '0.10.3'
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
@@ -400,16 +400,19 @@ module StateMachine
400
400
  # Creates a scope for finding records *with* a particular state or
401
401
  # states for the attribute
402
402
  def create_with_scope(name)
403
- define_scope(name, lambda {|values| {attribute => values}})
403
+ create_scope(name, lambda {|values| ["#{attribute_column} IN (?)", values]})
404
404
  end
405
405
 
406
406
  # Creates a scope for finding records *without* a particular state or
407
407
  # states for the attribute
408
408
  def create_without_scope(name)
409
- define_scope(name, lambda {|values|
410
- connection = owner_class.connection
411
- ["#{connection.quote_table_name(owner_class.table_name)}.#{connection.quote_column_name(attribute)} NOT IN (?)", values]
412
- })
409
+ create_scope(name, lambda {|values| ["#{attribute_column} NOT IN (?)", values]})
410
+ end
411
+
412
+ # Generates the fully-qualifed column name for this machine's attribute
413
+ def attribute_column
414
+ connection = owner_class.connection
415
+ "#{connection.quote_table_name(owner_class.table_name)}.#{connection.quote_column_name(attribute)}"
413
416
  end
414
417
 
415
418
  # Runs a new database transaction, rolling back any changes by raising
@@ -420,7 +423,7 @@ module StateMachine
420
423
  end
421
424
 
422
425
  # Defines a new named scope with the given name
423
- def define_scope(name, scope)
426
+ def create_scope(name, scope)
424
427
  lambda {|model, values| model.where(scope.call(values))}
425
428
  end
426
429
 
@@ -10,7 +10,7 @@ module StateMachine
10
10
  super if defined?(I18n)
11
11
  end
12
12
 
13
- def define_scope(name, scope)
13
+ def create_scope(name, scope)
14
14
  if owner_class.respond_to?(:named_scope)
15
15
  name = name.to_sym
16
16
  machine_name = self.name
@@ -1819,6 +1819,27 @@ module ActiveRecordTest
1819
1819
  $stderr.puts 'Skipping ActiveRecord Scope tests. `gem install active_record` >= v2.1.0 and try again.'
1820
1820
  end
1821
1821
 
1822
+ if ActiveRecord.const_defined?(:Relation)
1823
+ class MachineWithDefaultScope < BaseTestCase
1824
+ def setup
1825
+ @model = new_model
1826
+ @machine = StateMachine::Machine.new(@model, :initial => :parked)
1827
+ @machine.state :idling
1828
+
1829
+ @model.class_eval do
1830
+ default_scope with_state(:parked, :idling)
1831
+ end
1832
+ end
1833
+
1834
+ def test_should_set_initial_state_on_created_object
1835
+ object = @model.new
1836
+ assert_equal 'parked', object.state
1837
+ end
1838
+ end
1839
+ else
1840
+ $stderr.puts 'Skipping ActiveRecord Default Scope tests. `gem install active_record` >= v3.0.0 and try again.'
1841
+ end
1842
+
1822
1843
  if Object.const_defined?(:I18n)
1823
1844
  class MachineWithInternationalizationTest < BaseTestCase
1824
1845
  def setup
@@ -1402,6 +1402,23 @@ module MongoidTest
1402
1402
  end
1403
1403
  end
1404
1404
 
1405
+ class MachineWithDefaultScope < BaseTestCase
1406
+ def setup
1407
+ @model = new_model
1408
+ @machine = StateMachine::Machine.new(@model, :initial => :parked)
1409
+ @machine.state :idling
1410
+
1411
+ @model.class_eval do
1412
+ default_scope with_state(:parked, :idling)
1413
+ end
1414
+ end
1415
+
1416
+ def test_should_set_initial_state_on_created_object
1417
+ object = @model.new
1418
+ assert_equal 'parked', object.state
1419
+ end
1420
+ end
1421
+
1405
1422
  class MachineWithInternationalizationTest < BaseTestCase
1406
1423
  def setup
1407
1424
  I18n.backend = I18n::Backend::Simple.new
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: 51
4
+ hash: 49
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 10
9
- - 2
10
- version: 0.10.2
9
+ - 3
10
+ version: 0.10.3
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-31 00:00:00 -04:00
18
+ date: 2011-04-07 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21