lib/aasm/base.rb in aasm-5.0.5 vs lib/aasm/base.rb in aasm-5.0.6

- old
+ new

@@ -57,10 +57,12 @@ if @state_machine.config.no_direct_assignment @klass.send(:define_method, "#{@state_machine.config.column}=") do |state_name| if self.class.aasm(:"#{aasm_name}").state_machine.config.no_direct_assignment raise AASM::NoDirectAssignmentError.new('direct assignment of AASM column has been disabled (see AASM configuration for this class)') + else + super(state_name) end end end end @@ -131,10 +133,12 @@ safely_define_method klass, name, ->(*args, &block) do aasm(aasm_name).current_event = event aasm_fire_event(aasm_name, event, {:persist => false}, *args, &block) end + skip_instance_level_validation(event, name, aasm_name, klass) + # Create aliases for the event methods. Keep the old names to maintain backwards compatibility. if namespace? klass.send(:alias_method, "may_#{name}_#{namespace}?", "may_#{name}?") klass.send(:alias_method, "#{name}_#{namespace}!", "#{name}!") klass.send(:alias_method, "#{name}_#{namespace}", name) @@ -243,9 +247,24 @@ [[args.first], args.last] elsif args.size > 0 [args, {}] else raise "count not parse states: #{args}" + end + end + + def skip_instance_level_validation(event, name, aasm_name, klass) + # Overrides the skip_validation config for an instance (If skip validation is set to false in original config) and + # restores it back to the original value after the event is fired. + safely_define_method klass, "#{name}_without_validation!", ->(*args, &block) do + original_config = AASM::StateMachineStore.fetch(self.class, true).machine(aasm_name).config.skip_validation_on_save + begin + AASM::StateMachineStore.fetch(self.class, true).machine(aasm_name).config.skip_validation_on_save = true unless original_config + aasm(aasm_name).current_event = :"#{name}!" + aasm_fire_event(aasm_name, event, {:persist => true}, *args, &block) + ensure + AASM::StateMachineStore.fetch(self.class, true).machine(aasm_name).config.skip_validation_on_save = original_config + end end end end end