-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Implement Spree.formatMoney using accounting.js #1745
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d660c29
Implement Spree.formatMoney using accounting.js
jhawthorn 13de739
Rename _translations partial to _js_locale_data
jhawthorn ca48229
Convert to 1.9 hash syntax
jhawthorn 77f9760
Move JS Spree.currencyInfo into erb view
jhawthorn b1e4990
Add CHANGELOG entries for Spree.formatMoney
jhawthorn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
backend/app/assets/javascripts/spree/backend/format_money.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//= require spree/backend/translation | ||
//= require solidus_admin/accounting | ||
|
||
Spree.formatMoney = function(amount, currency) { | ||
var currencyInfo = Spree.currencyInfo[currency]; | ||
|
||
var thousand = Spree.t('currency_delimiter'); | ||
var decimal = Spree.t('currency_separator'); | ||
|
||
return accounting.formatMoney(amount, currencyInfo[0], currencyInfo[1], thousand, decimal, currencyInfo[2]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
backend/app/views/spree/admin/shared/_js_locale_data.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<script> | ||
if (Spree === undefined) { | ||
var Spree = {} | ||
} | ||
Spree.translations = <%== | ||
I18n.t("spree").merge({ | ||
date_picker: Spree.t(:js_format, | ||
scope: 'date_picker', | ||
default: 'yy/mm/dd'), | ||
abbr_day_names: I18n.t(:abbr_day_names, scope: :date), | ||
destroy: Spree.t(:destroy, scope: :actions), | ||
edit: Spree.t(:edit, scope: :actions), | ||
save: Spree.t(:save, scope: :actions), | ||
cancel: Spree.t(:cancel, scope: :actions), | ||
first_day: Spree.t(:first_day, | ||
scope: 'date_picker', | ||
default: 0).to_i, | ||
item_stock_placeholder: Spree.t(:choose_location), | ||
month_names: I18n.t(:month_names, scope: :date).reject(&:blank?), | ||
currency_separator: I18n.t('number.currency.format.separator'), | ||
currency_delimiter: I18n.t('number.currency.format.delimiter'), | ||
activerecord: I18n.t('activerecord') | ||
}).to_json | ||
%>; | ||
|
||
Spree.currencyInfo = <%== | ||
Money::Currency.all.map { |c| | ||
format = | ||
if c.symbol == "" || c.symbol_first | ||
"%s%v" | ||
else | ||
"%v %s" | ||
end | ||
[c.id.to_s.upcase, [ | ||
c.symbol || "¤", | ||
c.exponent, | ||
format | ||
]] | ||
}.to_h.to_json | ||
%>; | ||
</script> | ||
<script data-hook='admin-custom-translations'> | ||
</script> | ||
|
||
<% if I18n.locale != :en %> | ||
<%= javascript_include_tag "select2_locale_#{I18n.locale}" %> | ||
<% end %> |
30 changes: 0 additions & 30 deletions
30
backend/app/views/spree/admin/shared/_translations.html.erb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
backend/spec/features/admin/javascript_format_money_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe 'JS Spree.formatMoney', js: true do | ||
stub_authorization! | ||
|
||
# This is a slightly hacky spec to ensure that our JS will format money in | ||
# the same was as our ruby code. | ||
# This should probably replaced with a pure JS test in the future. | ||
it 'should behave identically to Spree::Money#to_s' do | ||
visit '/admin' | ||
|
||
Money::Currency.all.map(&:id).map(&:to_s).map(&:upcase).uniq.each do |currency| | ||
money = Spree::Money.new(1234, currency: currency) | ||
|
||
js_result = page.evaluate_script("Spree.formatMoney(#{money.to_d}, '#{currency}')") | ||
|
||
expect(js_result).to eq money.to_s | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually find this a very good way to test this. 👍 |
||
end | ||
end |
4 changes: 4 additions & 0 deletions
4
backend/vendor/assets/javascripts/solidus_admin/accounting.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Good idea 👌🏻 Much more convenient.
This deserves a CHANGELOG entry though, as people may have modified one of these files. Even deface overrides will not work anymore as the file name changed.