Skip to content

Commit

Permalink
Allow subclasses to override the initial state.
Browse files Browse the repository at this point in the history
  • Loading branch information
obrie committed Jul 26, 2007
1 parent ea86551 commit 982f2f8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Allow subclasses to override the initial state

* Fix problem with migrations using default null when column cannot be null

* Moved deadline support into a separate plugin (has_state_deadlines).
Expand Down
5 changes: 3 additions & 2 deletions lib/has_states.rb
Expand Up @@ -99,11 +99,12 @@ def has_states(options)
# Save options for referencing later
write_inheritable_attribute :active_states, {}
write_inheritable_attribute :active_events, {}
write_inheritable_attribute :initial_state_name, options[:initial]
write_inheritable_attribute :initial_state, options[:initial]
write_inheritable_attribute :record_state_changes, options[:record_changes]

class_inheritable_reader :active_states
class_inheritable_reader :active_events
class_inheritable_writer :initial_state
class_inheritable_reader :record_state_changes

before_create :set_initial_state_id
Expand Down Expand Up @@ -376,7 +377,7 @@ def self.included(base) #:nodoc:

# Gets the name of the initial state that records will be placed in.
def initial_state_name
name = self.class.read_inheritable_attribute(:initial_state_name)
name = self.class.read_inheritable_attribute(:initial_state)
name = name.call(self) if name.is_a?(Proc)

name.to_sym
Expand Down
1 change: 1 addition & 0 deletions test/app_root/app/models/motorcycle.rb
@@ -1,2 +1,3 @@
class Motorcycle < Vehicle
self.initial_state = :idling
end
5 changes: 5 additions & 0 deletions test/unit/has_states_test.rb
Expand Up @@ -21,6 +21,11 @@ def test_should_raise_exception_if_no_initial_state_given
assert_raise(PluginAWeek::Has::States::NoInitialState) {Message.has_states({})}
end

def test_should_allow_subclasses_to_override_initial_state
assert_equal :idling, Motorcycle.read_inheritable_attribute(:initial_state)
assert_not_equal Motorcycle.read_inheritable_attribute(:initial_state), Vehicle.read_inheritable_attribute(:initial_state)
end

def test_active_states_should_be_initially_empty
expected = {}
assert_equal expected, Message.active_states
Expand Down

0 comments on commit 982f2f8

Please sign in to comment.