Skip to content

Commit

Permalink
Enhance currency selector to list 'All Others' after 'Popular' (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekosulin authored Apr 10, 2024
1 parent b5c56f7 commit b812b6d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
29 changes: 28 additions & 1 deletion app/helpers/application_form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,31 @@ def money_field(method, options = {})

merged_options = default_options.merge(options)

grouped_options = currency_options_for_select
selected_currency = money&.currency&.iso_code

@template.form_field_tag do
(label(method, *label_args(options)).to_s if options[:label]) +
@template.tag.div(class: "flex items-center") do
number_field(money_amount_method, merged_options.except(:label)) +
select(money_currency_method, Money::Currency.popular.map(&:iso_code), { selected: money&.currency&.iso_code }, { disabled: readonly_currency, class: "ml-auto form-field__input w-fit pr-8" })
grouped_select(money_currency_method, grouped_options, { selected: selected_currency, disabled: readonly_currency }, class: "ml-auto form-field__input w-fit pr-8")
end
end
end


def grouped_select(method, grouped_choices, options = {}, html_options = {})
default_options = { class: "form-field__input" }
merged_html_options = default_options.merge(html_options)

label_html = label(method, *label_args(options)).to_s if options[:label]
select_html = @template.grouped_collection_select(@object_name, method, grouped_choices, :last, :first, :last, :first, options, merged_html_options)

@template.content_tag(:div, class: "flex items-center") do
label_html.to_s.html_safe + select_html
end
end

def select(method, choices, options = {}, html_options = {})
default_options = { class: "form-field__input" }
merged_options = default_options.merge(html_options)
Expand Down Expand Up @@ -83,6 +99,17 @@ def submit(value = nil, options = {})

private

def currency_options_for_select
popular_currencies = Money::Currency.popular.map { |currency| [ currency.iso_code, currency.iso_code ] }
all_currencies = Money::Currency.all_instances.map { |currency| [ currency.iso_code, currency.iso_code ] }
all_other_currencies = all_currencies.reject { |c| popular_currencies.map(&:last).include?(c.last) }.sort_by(&:last)

{
I18n.t("accounts.new.currency.popular") => popular_currencies,
I18n.t("accounts.new.currency.all_others") => all_other_currencies
}
end

def label_args(options)
case options[:label]
when Array
Expand Down
3 changes: 3 additions & 0 deletions config/locales/views/account/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ en:
index:
new_account: New account
new:
currency:
all_others: All Others
popular: Popular
name:
label: Account name
placeholder: Example account name
Expand Down
4 changes: 4 additions & 0 deletions lib/money/currency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def all
@all ||= YAML.load_file(CURRENCIES_FILE_PATH)
end

def all_instances
all.values.map { |currency_data| new(currency_data["iso_code"]) }
end

def popular
all.values.sort_by { |currency| currency["priority"] }.first(12).map { |currency_data| new(currency_data["iso_code"]) }
end
Expand Down

0 comments on commit b812b6d

Please sign in to comment.