diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-11-22 18:37:08 -0800 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-11-22 19:19:13 -0800 |
commit | 0e2d18e415118afed2df148e5d7302ef0361b569 (patch) | |
tree | badfc2005f1b57f37b64efb6be51affab0cf04a6 /activemodel/lib/active_model | |
parent | 0492759db338a01623672674408a0bed62951ac6 (diff) | |
download | rails-0e2d18e415118afed2df148e5d7302ef0361b569.tar.gz rails-0e2d18e415118afed2df148e5d7302ef0361b569.tar.bz2 rails-0e2d18e415118afed2df148e5d7302ef0361b569.zip |
Extract state query method definition and quiet method redefinition warning.
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r-- | activemodel/lib/active_model/state_machine.rb | 6 | ||||
-rw-r--r-- | activemodel/lib/active_model/state_machine/state.rb | 7 |
2 files changed, 8 insertions, 5 deletions
diff --git a/activemodel/lib/active_model/state_machine.rb b/activemodel/lib/active_model/state_machine.rb index 89afc4abb6..bce90fd743 100644 --- a/activemodel/lib/active_model/state_machine.rb +++ b/activemodel/lib/active_model/state_machine.rb @@ -31,6 +31,12 @@ module ActiveModel state_machines[name] ||= Machine.new(self, name) block ? state_machines[name].update(options, &block) : state_machines[name] end + + def define_state_query_method(state_name) + name = "#{state_name}?" + undef_method(name) if method_defined?(name) + class_eval "def #{name}; current_state.to_s == %(#{state_name}) end" + end end def current_state(name = nil, new_state = nil, persist = false) diff --git a/activemodel/lib/active_model/state_machine/state.rb b/activemodel/lib/active_model/state_machine/state.rb index 68eb2aa34a..76916b1d86 100644 --- a/activemodel/lib/active_model/state_machine/state.rb +++ b/activemodel/lib/active_model/state_machine/state.rb @@ -5,11 +5,8 @@ module ActiveModel def initialize(name, options = {}) @name = name - machine = options.delete(:machine) - if machine - machine.klass.send(:define_method, "#{name}?") do - current_state.to_s == name.to_s - end + if machine = options.delete(:machine) + machine.klass.define_state_query_method(name) end update(options) end |