Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix positional and keyword arguments in ruby 2.7 and above #13

Open
wants to merge 2 commits into
base: 2-3-lts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions actionpack/lib/action_view/helpers/translation_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,23 @@ 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
else
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

Expand All @@ -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

Expand Down
24 changes: 22 additions & 2 deletions activerecord/lib/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
12 changes: 10 additions & 2 deletions activerecord/lib/active_record/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
22 changes: 20 additions & 2 deletions activesupport/lib/active_support/option_merger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,32 @@ 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
end

@context.__send__(method, *arguments, &block)
invoke_method(method, arguments, options, &block)
end

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
end
else
def invoke_method(method, arguments, options, &block)
arguments << options.dup if options
@context.__send__(method, *arguments, &block)
end
end
end
end