-
Notifications
You must be signed in to change notification settings - Fork 39
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
have to_json and as_json return amount and currency json #138
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just move some lines around to conform to the ActiveSupport interface as described in the issue.
lib/money/money.rb
Outdated
end | ||
|
||
def as_json(*args) | ||
to_s | ||
to_json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have the actual implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was always wrong too, as_json
needs to return a hash, not a string... fixed now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
DO NOT MERGE... until background jobs can handle the new format |
this is ready to merge now since https://github.com/Shopify/shopify/pull/151350 |
def as_json(*args) | ||
to_s | ||
def as_json(*_args) | ||
{ value: to_s(:amount), currency: currency.to_s } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we prefer currency.iso_code
here instead to be a bit more clear and future proof if we want to_s to be more human readable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the main difference is with a NullCurrency, to_s
will return an empty string, vs iso_code
will return XXX
. I'd rather keep it the way it is for now since the generated JSON might be sent to a third party which does not understand the valid "no currency" iso code.
😮 |
this is the new default behaviour in v1.0, with an option to fallback to the legacy behaviour |
Why
Fixes #76
What
as_json
should return a hash of attributes (amount, currency) to be serialized, not a stringfrom_json
is missing to assign attributes from a JSON object