Skip to content
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
6 changes: 5 additions & 1 deletion lib/simple_enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

require 'simple_enum/version'
require 'simple_enum/attribute'
require 'simple_enum/translation'
begin
require 'simple_enum/translation'
rescue SyntaxError
Copy link

@softbrada softbrada Aug 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am not a maintainer, or contributor to this project, probably should also be ArgumentError here. On Ruby 2.6 i get ArgumentError rather than SyntaxError. Just sharing my thoughts

require 'simple_enum/translation_ruby19'
end
require 'simple_enum/view_helpers'

# Base module which gets included in <tt>ActiveRecord::Base</tt>. See documentation
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_enum/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def human_enum_name(enum, key, options = {})
defaults << key.to_s.humanize

options.reverse_merge! count: 1, default: defaults
I18n.translate(defaults.shift, options)
I18n.translate(defaults.shift, **options)
Copy link

@softbrada softbrada Aug 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am not a maintainer, or contributor to this project, but seems brittle to me to have this whole file duplicated to fix a single line.

We could rescue here the exception so we avoid duplicating unnecessary code. Just my thoughts

end
end
end
21 changes: 21 additions & 0 deletions lib/simple_enum/translation_ruby19.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'i18n'

module SimpleEnum
module Translation
def human_enum_name(enum, key, options = {})
return '' unless key.present?

defaults = lookup_ancestors.map do |klass|
:"#{self.i18n_scope}.enums.#{klass.model_name.i18n_key}.#{enum}.#{key}"
end

defaults << :"enums.#{self.model_name.i18n_key}.#{enum}.#{key}"
defaults << :"enums.#{enum}.#{key}"
defaults << options.delete(:default) if options[:default]
defaults << key.to_s.humanize

options.reverse_merge! count: 1, default: defaults
I18n.translate(defaults.shift, options)
end
end
end