aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
Diffstat (limited to 'activemodel/lib')
-rw-r--r--activemodel/lib/active_model/state_machine.rb6
-rw-r--r--activemodel/lib/active_model/state_machine/state.rb7
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