From 114e050ebe27840ae0f5023cab5c31ef10cd3f7a Mon Sep 17 00:00:00 2001 From: kastakhov <16296930+kastakhov@users.noreply.github.com> Date: Wed, 25 Sep 2024 21:04:54 +0300 Subject: [PATCH 1/2] fix positional and keyword arguments in ruby 2.7 and above --- .../action_view/helpers/translation_helper.rb | 22 +++++++++++++---- activerecord/lib/active_record/base.rb | 24 +++++++++++++++++-- activerecord/lib/active_record/validations.rb | 12 ++++++++-- .../lib/active_support/option_merger.rb | 19 +++++++++++++-- 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/actionpack/lib/action_view/helpers/translation_helper.rb b/actionpack/lib/action_view/helpers/translation_helper.rb index 2772aa90861fb..945c56fa95de0 100644 --- a/actionpack/lib/action_view/helpers/translation_helper.rb +++ b/actionpack/lib/action_view/helpers/translation_helper.rb @@ -40,7 +40,11 @@ def translate(keys, options = {}) html_safe_options[:default] = MISSING_TRANSLATION unless html_safe_options[:default].blank? - translation = I18n.translate(qualified_key, html_safe_options) + translation = if RUBY_VERSION >= "2.7" + I18n.translate(qualified_key, **html_safe_options) + else + I18n.translate(qualified_key, html_safe_options) + end if translation.equal?(MISSING_TRANSLATION) options[:default].first @@ -48,7 +52,11 @@ def translate(keys, options = {}) translation.respond_to?(:html_safe) ? translation.html_safe : translation end else - I18n.translate(qualified_key, options) + if RUBY_VERSION >= "2.7" + I18n.translate(qualified_key, **options) + else + I18n.translate(qualified_key, options) + end end end @@ -71,8 +79,14 @@ def translate(keys, options = {}) alias :t :translate # Delegates to I18n.localize with no additional functionality. - def localize(*args) - I18n.localize(*args) + if RUBY_VERSION >= "2.7" + def localize(...) + I18n.localize(...) + end + else + def localize(*args) + I18n.localize(*args) + end end alias :l :localize diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 5e176c1fe96e9..93e242a8b7bee 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1435,7 +1435,17 @@ def human_attribute_name(attribute_key_name, options = {}) defaults.flatten! defaults << attribute_key_name.to_s.humanize options[:count] ||= 1 - I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => [:activerecord, :attributes])) + if RUBY_VERSION >= '2.7' + I18n.translate( + defaults.shift, + **options.merge(:default => defaults, :scope => [:activerecord, :attributes]) + ) + else + I18n.translate( + defaults.shift, + options.merge(:default => defaults, :scope => [:activerecord, :attributes]) + ) + end end # Transform the modelname into a more humane format, using I18n. @@ -1447,7 +1457,17 @@ def human_name(options = {}) :"#{klass.name.underscore}" if klass.name.present? end.compact defaults << self.name.humanize if self.name.present? - I18n.translate(defaults.shift, {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options)) + if RUBY_VERSION >= '2.7' + I18n.translate( + defaults.shift, + **{:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options) + ) + else + I18n.translate( + defaults.shift, + {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options) + ) + end end # True if this isn't a concrete subclass needing a STI type condition. diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 26d82d4ea153d..e3d2150ba0bcd 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -79,7 +79,11 @@ def generate_message(options = {}) keys.compact! options.merge!(:default => keys) - I18n.translate(keys.shift, options) + if RUBY_VERSION >= '2.7' + I18n.translate(keys.shift, **options) + else + I18n.translate(keys.shift, options) + end end # Wraps an error message into a full_message format. @@ -117,7 +121,11 @@ def generate_full_message(options = {}) ] options.merge!(:default => keys, :message => self.message) - I18n.translate(keys.shift, options) + if RUBY_VERSION >= '2.7' + I18n.translate(keys.shift, **options) + else + I18n.translate(keys.shift, options) + end end # Return user options with default options. diff --git a/activesupport/lib/active_support/option_merger.rb b/activesupport/lib/active_support/option_merger.rb index 63662b75c7496..b0257e616efc2 100644 --- a/activesupport/lib/active_support/option_merger.rb +++ b/activesupport/lib/active_support/option_merger.rb @@ -10,14 +10,29 @@ def initialize(context, options) private def method_missing(method, *arguments, &block) + options = nil if arguments.last.is_a?(Proc) proc = arguments.pop arguments << lambda { |*args| @options.deep_merge(proc.call(*args)) } + elsif arguments.last.respond_to?(:to_hash) + options = @options.deep_merge(arguments.pop) else - arguments << (arguments.last.respond_to?(:to_hash) ? @options.deep_merge(arguments.pop) : @options.dup) + options = @options.dup end - @context.__send__(method, *arguments, &block) + invoke_method(method, arguments, options, &block) + end + def invoke_method(method, arguments, options, &block) + if RUBY_VERSION >= '2.7' + if options + @context.__send__(method, *arguments, **options, &block) + else + @context.__send__(method, *arguments, &block) + end + else + arguments << options if options + @context.__send__(method, *arguments, &block) + end end end end From cccd53b66acb88cc5184a5782c4e7b3ae0d40936 Mon Sep 17 00:00:00 2001 From: kastakhov <16296930+kastakhov@users.noreply.github.com> Date: Wed, 25 Sep 2024 21:11:31 +0300 Subject: [PATCH 2/2] backport original implementation from rails v6.1.0 --- activesupport/lib/active_support/option_merger.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/activesupport/lib/active_support/option_merger.rb b/activesupport/lib/active_support/option_merger.rb index b0257e616efc2..cefb003c502d6 100644 --- a/activesupport/lib/active_support/option_merger.rb +++ b/activesupport/lib/active_support/option_merger.rb @@ -17,20 +17,23 @@ def method_missing(method, *arguments, &block) elsif arguments.last.respond_to?(:to_hash) options = @options.deep_merge(arguments.pop) else - options = @options.dup + options = @options end invoke_method(method, arguments, options, &block) end - def invoke_method(method, arguments, options, &block) - if RUBY_VERSION >= '2.7' + + if RUBY_VERSION >= "2.7" + def invoke_method(method, arguments, options, &block) if options @context.__send__(method, *arguments, **options, &block) else @context.__send__(method, *arguments, &block) end - else - arguments << options if options + end + else + def invoke_method(method, arguments, options, &block) + arguments << options.dup if options @context.__send__(method, *arguments, &block) end end