lib/aasm/aasm.rb in aasm-2.3.0 vs lib/aasm/aasm.rb in aasm-2.3.1
- old
+ new
@@ -48,10 +48,17 @@
unless sm.events.has_key?(name)
sm.events[name] = AASM::SupportingClasses::Event.new(name, options, &block)
end
+ # an addition over standard aasm so that, before firing an event, you can ask
+ # may_event? and get back a boolean that tells you whether the guard method
+ # on the transition will let this happen.
+ define_method("may_#{name.to_s}?") do |*args|
+ aasm_test_event(name, *args)
+ end
+
define_method("#{name.to_s}!") do |*args|
aasm_fire_event(name, true, *args)
end
define_method("#{name.to_s}") do |*args|
@@ -70,11 +77,11 @@
def aasm_states_for_select
AASM::StateMachine[self].states.map { |state| state.for_select }
end
def human_event_name(event)
- AASM::I18n.new.human_event_name(self, event)
+ AASM::Localizer.new.human_event_name(self, event)
end
end
# Instance methods
def aasm_current_state
@@ -102,17 +109,23 @@
def aasm_events_for_current_state
aasm_events_for_state(aasm_current_state)
end
+ # filters the results of events_for_current_state so that only those that
+ # are really currently possible (given transition guards) are shown.
+ def aasm_permissible_events_for_current_state
+ aasm_events_for_current_state.select{ |e| self.send(("may_" + e.to_s + "?").to_sym) }
+ end
+
def aasm_events_for_state(state)
events = self.class.aasm_events.values.select {|event| event.transitions_from_state?(state) }
events.map {|event| event.name}
end
def human_state
- AASM::I18n.new.human_state(self)
+ AASM::Localizer.new.human_state(self)
end
private
def set_aasm_current_state_with_persistence(state)
@@ -147,9 +160,14 @@
obj = self.class.aasm_states.find {|s| s == name}
raise AASM::UndefinedState, "State :#{name} doesn't exist" if obj.nil?
obj
end
+ def aasm_test_event(name, *args)
+ event = self.class.aasm_events[name]
+ event.may_fire?(self, *args)
+ end
+
def aasm_fire_event(name, persist, *args)
event = self.class.aasm_events[name]
begin
old_state = aasm_state_object_for_state(aasm_current_state)