diff --git a/app/helpers/application_form_builder.rb b/app/helpers/application_form_builder.rb index c7b96857..16d3ffba 100644 --- a/app/helpers/application_form_builder.rb +++ b/app/helpers/application_form_builder.rb @@ -41,15 +41,31 @@ class ApplicationFormBuilder < ActionView::Helpers::FormBuilder 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) @@ -83,6 +99,17 @@ class ApplicationFormBuilder < ActionView::Helpers::FormBuilder 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 diff --git a/config/locales/views/account/en.yml b/config/locales/views/account/en.yml index acdf8cff..bf533d7d 100644 --- a/config/locales/views/account/en.yml +++ b/config/locales/views/account/en.yml @@ -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 diff --git a/lib/money/currency.rb b/lib/money/currency.rb index 71411899..2069cb65 100644 --- a/lib/money/currency.rb +++ b/lib/money/currency.rb @@ -26,6 +26,10 @@ class Money::Currency @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