-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathcomponent.rb
More file actions
62 lines (49 loc) · 1.73 KB
/
component.rb
File metadata and controls
62 lines (49 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
class SolidusAdmin::Stores::Edit::Component < SolidusAdmin::BaseComponent
include SolidusAdmin::Layout::PageHelpers
# Define the necessary attributes for the component
attr_reader :store, :available_countries
# Initialize the component with required data
def initialize(store:)
@store = store
@available_countries = fetch_available_countries
end
def form_id
@form_id ||= "#{stimulus_id}--form-#{@store.id}"
end
def currency_options
Spree::Config.available_currencies.map(&:iso_code)
end
# Generates options for cart tax countries
def cart_tax_country_options
fetch_available_countries(restrict_to_zone: Spree::Config[:checkout_zone]).map do |country|
[country.name, country.iso]
end
end
# Generates available locales
def localization_options
Spree.i18n_available_locales.map do |locale|
[
I18n.t('spree.i18n.this_file_language', locale: locale, default: locale.to_s),
locale
]
end
end
# Fetch countries for the address form
def available_country_options
Spree::Country.order(:name).map { |country| [country.name, country.id] }
end
private
# Fetch the available countries for the localization section
def fetch_available_countries(restrict_to_zone: Spree::Config[:checkout_zone])
countries = Spree::Country.available(restrict_to_zone:)
country_names = Carmen::Country.all.map do |country|
[country.code, country.name]
end.to_h
country_names.update I18n.t('spree.country_names', default: {}).stringify_keys
countries.collect do |country|
country.name = country_names.fetch(country.iso, country.name)
country
end.sort_by { |country| country.name.parameterize }
end
end