Module: Spree::Stores::Markets
- Extended by:
- ActiveSupport::Concern
- Included in:
- Spree::Store
- Defined in:
- app/models/concerns/spree/stores/markets.rb
Instance Method Summary collapse
-
#countries_available_for_checkout ⇒ Array<Spree::Country>
Returns the countries available for checkout, derived from markets.
-
#countries_from_markets ⇒ ActiveRecord::Relation<Spree::Country>
Returns countries from all markets as an ActiveRecord relation.
-
#default_country ⇒ Spree::Country?
Returns the default country, derived from the default market.
-
#default_country_id ⇒ Integer?
Returns the default country ID, derived from the default market.
-
#default_currency ⇒ String?
Returns the default currency, delegating to the default market when markets exist Falls back to the store’s own default_currency column.
-
#default_locale ⇒ String?
Returns the default locale, delegating to the default market when markets exist Falls back to the store’s own default_locale column.
-
#default_market ⇒ Spree::Market?
Returns the default market for this store.
-
#has_markets? ⇒ Boolean
Returns true if the store has markets, false otherwise.
-
#market_for_country(country) ⇒ Spree::Market?
Returns the market that contains the given country.
-
#supported_currencies_list ⇒ Array<Money::Currency>
Returns supported currencies derived from markets, falling back to store attributes.
-
#supported_locales_list ⇒ Array<String>
Returns supported locales derived from markets, falling back to store attributes.
Instance Method Details
#countries_available_for_checkout ⇒ Array<Spree::Country>
Returns the countries available for checkout, derived from markets
75 76 77 78 79 80 81 82 83 |
# File 'app/models/concerns/spree/stores/markets.rb', line 75 def countries_available_for_checkout @countries_available_for_checkout = begin if has_markets? markets.flat_map(&:countries).uniq.sort_by(&:name) else Spree::Country.all.to_a end end end |
#countries_from_markets ⇒ ActiveRecord::Relation<Spree::Country>
Returns countries from all markets as an ActiveRecord relation
67 68 69 70 71 |
# File 'app/models/concerns/spree/stores/markets.rb', line 67 def countries_from_markets Spree::Country .where(id: Spree::Country.joins(market_countries: :market).where(Spree::Market.table_name => { store_id: id, deleted_at: nil }).select(:id)) .order(:name) end |
#default_country ⇒ Spree::Country?
Returns the default country, derived from the default market
18 19 20 21 22 23 24 |
# File 'app/models/concerns/spree/stores/markets.rb', line 18 def default_country if has_markets? default_market&.default_country else super end end |
#default_country_id ⇒ Integer?
Returns the default country ID, derived from the default market
28 29 30 31 32 33 34 |
# File 'app/models/concerns/spree/stores/markets.rb', line 28 def default_country_id if has_markets? default_country&.id else read_attribute(:default_country_id) end end |
#default_currency ⇒ String?
Returns the default currency, delegating to the default market when markets exist Falls back to the store’s own default_currency column
50 51 52 53 54 55 56 |
# File 'app/models/concerns/spree/stores/markets.rb', line 50 def default_currency if has_markets? default_market&.currency || read_attribute(:default_currency) else read_attribute(:default_currency) end end |
#default_locale ⇒ String?
Returns the default locale, delegating to the default market when markets exist Falls back to the store’s own default_locale column
39 40 41 42 43 44 45 |
# File 'app/models/concerns/spree/stores/markets.rb', line 39 def default_locale if has_markets? default_market&.default_locale || read_attribute(:default_locale) else read_attribute(:default_locale) end end |
#default_market ⇒ Spree::Market?
Returns the default market for this store
12 13 14 |
# File 'app/models/concerns/spree/stores/markets.rb', line 12 def default_market @default_market ||= Spree::Market.default_for_store(self) end |
#has_markets? ⇒ Boolean
Returns true if the store has markets, false otherwise
109 110 111 |
# File 'app/models/concerns/spree/stores/markets.rb', line 109 def has_markets? @has_markets ||= persisted? && (markets.loaded? ? markets.any? : markets.exists?) end |
#market_for_country(country) ⇒ Spree::Market?
Returns the market that contains the given country
61 62 63 |
# File 'app/models/concerns/spree/stores/markets.rb', line 61 def market_for_country(country) Spree::Market.for_country(country, store: self) end |
#supported_currencies_list ⇒ Array<Money::Currency>
Returns supported currencies derived from markets, falling back to store attributes
87 88 89 90 91 92 93 94 95 |
# File 'app/models/concerns/spree/stores/markets.rb', line 87 def supported_currencies_list @supported_currencies_list ||= if has_markets? markets.pluck(:currency).uniq.map do |code| ::Money::Currency.find(code) end.compact.sort_by { |c| c.iso_code == default_currency ? 0 : 1 } else legacy_supported_currencies_list end end |
#supported_locales_list ⇒ Array<String>
Returns supported locales derived from markets, falling back to store attributes
99 100 101 102 103 104 105 |
# File 'app/models/concerns/spree/stores/markets.rb', line 99 def supported_locales_list @supported_locales_list ||= if has_markets? (markets.flat_map(&:supported_locales_list) << default_locale).compact.uniq.sort else legacy_supported_locales_list end end |