diff --git a/lib/aasm/base.rb b/lib/aasm/base.rb index 5e870cfc..03b6fc43 100644 --- a/lib/aasm/base.rb +++ b/lib/aasm/base.rb @@ -223,7 +223,20 @@ def safely_define_method(klass, method_name, method_definition) end end - klass.send(:define_method, method_name, method_definition) + klass.send(:define_method, method_name, method_definition).tap do |sym| + apply_ruby2_keyword(klass, sym) + end + end + + def apply_ruby2_keyword(klass, sym) + if RUBY_VERSION >= '2.7.1' + if klass.instance_method(sym).parameters.find { |type, _| type.to_s.start_with?('rest') } + # If there is a place where you are receiving in *args, do ruby2_keywords. + klass.module_eval do + ruby2_keywords sym + end + end + end end def namespace? diff --git a/spec/models/event_with_keyword_arguments.rb b/spec/models/event_with_keyword_arguments.rb new file mode 100644 index 00000000..f87c4b58 --- /dev/null +++ b/spec/models/event_with_keyword_arguments.rb @@ -0,0 +1,16 @@ +class EventWithKeywordArguments + include AASM + + aasm do + state :open, :initial => true, :column => :status + state :closed + + event :close do + before :_before_close + transitions from: :open, to: :closed + end + end + + def _before_close(key:) + end +end diff --git a/spec/unit/event_with_keyword_arguments_spec.rb b/spec/unit/event_with_keyword_arguments_spec.rb new file mode 100644 index 00000000..d9a2ace5 --- /dev/null +++ b/spec/unit/event_with_keyword_arguments_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' + +describe EventWithKeywordArguments do + let(:example) { EventWithKeywordArguments.new } + describe 'enable keyword arguments' do + it 'should be executed correctly that method registered by "before hooks" for events with keyword arguments.' do + expect(example.close(key: 1)).to be_truthy + end + end +end