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

Ensure the value of default option must be a string when calling I18n.t #1737

Conversation

mlzhuyi
Copy link

@mlzhuyi mlzhuyi commented Jan 31, 2018

@mlzhuyi mlzhuyi force-pushed the ensure_default_option_a_string_when_call_i18.t branch from 863655c to e9393c5 Compare January 31, 2018 03:37
Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

Just some minor changes please in language. Thanks!

end.join(', ')
end

def translate_attribute(key, **options)
translate("#{BASE_ATTRIBUTES_KEY}.#{key}", default: key, **options)
translate("#{BASE_ATTRIBUTES_KEY}.#{key}", default: key.to_s, **options)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe I am nitpicking, but feels like we should do this inside translate, no?

Copy link
Author

@mlzhuyi mlzhuyi Feb 1, 2018

Choose a reason for hiding this comment

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

It is generally better. But I'm not sure if there is any need to pass an alternative symbol key as value of :default in other cases, though, there isn't any example now.

CHANGELOG.md Outdated
@@ -7,6 +7,7 @@
#### Fixes

* Your contribution here.
* [#1737](https://github.com/ruby-grape/grape/pull/1737): Ensure the value of default option must be a string when calling i18n.t - [@mlzhuyi](https://github.com/mlzhuyi).
Copy link
Member

Choose a reason for hiding this comment

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

This fixes a bug. What's the error that you get without this fix? That's what should be in the changelog, please.

@mlzhuyi mlzhuyi force-pushed the ensure_default_option_a_string_when_call_i18.t branch from e9393c5 to bad7bf3 Compare February 1, 2018 02:41
CHANGELOG.md Outdated
@@ -7,6 +7,7 @@
#### Fixes

* Your contribution here.
* [#1737](https://github.com/ruby-grape/grape/pull/1737): Fix translating error whe pass symbols as params in custom validations - [@mlzhuyi](https://github.com/mlzhuyi).
Copy link
Member

Choose a reason for hiding this comment

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

Typo: when passing

@mlzhuyi mlzhuyi force-pushed the ensure_default_option_a_string_when_call_i18.t branch 3 times, most recently from 0924c22 to b7fc5bf Compare February 1, 2018 05:13
@mlzhuyi
Copy link
Author

mlzhuyi commented Feb 8, 2018

It has been changed @dblock

@@ -71,6 +71,7 @@ def translate_message(key, **options)
end

def translate(key, **options)
options[:default] &&= options[:default].to_s
Copy link
Member

Choose a reason for hiding this comment

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

This modifies the caller's options, which can easily be a frozen hash or whatever.

Copy link
Author

Choose a reason for hiding this comment

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

* and ** in arguments act a different way with common argument.

def common_argument_test(args)
  p args.object_id
end

def keyword_argument_test(**args)
  p args.object_id
end

args = { a: 1 }
p args.object_id # => 70338004137000
common_argument_test args # => 70338004137000
keyword_argument_test args # => 70338004136780

In there code, args in keyword_argument_test is a new hash, create by the method itself.I think we can treat it as a most basic hash.

Copy link
Member

Choose a reason for hiding this comment

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

I guess we always declared it the old fashioned way as options = {}. I worry that this is different version from version, especially reading this SO. Can we just do this the old fashioned way and avoid surprises?

Copy link
Author

Choose a reason for hiding this comment

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

Do you mean refactoring out the code, for there here are many ** now?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry if I wasn't clear. The double splat is fine. I mean this:

        options = options.dup
        options[:default] &&= options[:default].to_s

We dup arguments everywhere as far as I can see.

Copy link
Author

Choose a reason for hiding this comment

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

It's a really good habit in case of unknown changes.
I have added it :)

@mlzhuyi mlzhuyi force-pushed the ensure_default_option_a_string_when_call_i18.t branch from b7fc5bf to a8058ba Compare February 11, 2018 01:33
@mlzhuyi mlzhuyi force-pushed the ensure_default_option_a_string_when_call_i18.t branch from a8058ba to a23ffad Compare February 11, 2018 02:06
@dblock dblock merged commit 18c9d4b into ruby-grape:master Feb 14, 2018
@dblock
Copy link
Member

dblock commented Feb 14, 2018

Thanks, merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants