Skip to content
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

[RFC] Add per-store configurable locales #2627

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/app/helpers/spree/api/api_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def required_fields_for(model)

@@store_attributes = [
:id, :name, :url, :meta_description, :meta_keywords, :seo_title,
:mail_from_address, :default_currency, :code, :default
:mail_from_address, :default_currency, :code, :default, :available_locales
]

@@store_credit_history_attributes = [
Expand Down
9 changes: 6 additions & 3 deletions api/spec/requests/spree/api/stores_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ module Spree
"mail_from_address" => "[email protected]",
"default_currency" => nil,
"code" => store.code,
"default" => true
"default" => true,
"available_locales" => ["en"]
},
{
"id" => non_default_store.id,
Expand All @@ -47,7 +48,8 @@ module Spree
"mail_from_address" => "[email protected]",
"default_currency" => nil,
"code" => non_default_store.code,
"default" => false
"default" => false,
"available_locales" => ["en"]
}
])
end
Expand All @@ -64,7 +66,8 @@ module Spree
"mail_from_address" => "[email protected]",
"default_currency" => nil,
"code" => store.code,
"default" => true
"default" => true,
"available_locales" => ["en"]
)
end

Expand Down
11 changes: 11 additions & 0 deletions backend/app/views/spree/admin/stores/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,14 @@
{ class: "custom-select fullwidth" } %>
<%= f.error_message_on :cart_tax_country_iso %>
<% end %>

<%= f.field_container :available_locales do %>
<%= f.label :available_locales %>
<%= f.select :available_locales,
Spree.i18n_available_locales.map { |locale|
[I18n.t('spree.i18n.this_file_language', locale: locale, default: locale.to_s, fallback: false), locale]
}.sort,
{ },
{ class: 'custom-select fullwidth', multiple: true } %>
<%= f.error_message_on :default_currency %>
<% end %>
18 changes: 18 additions & 0 deletions core/app/models/spree/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ class << self
deprecate :by_url, "Spree::Store.by_url is DEPRECATED", deprecator: Spree::Deprecation
end

def available_locales
locales = super()
if locales
super().split(",").map(&:to_sym)
else
Spree.i18n_available_locales
end
end

def available_locales=(locales)
locales = locales.reject(&:blank?)
if locales.empty?
super(nil)
else
super(locales.map(&:to_s).join(","))
end
end

def self.current(store_key)
Spree::Deprecation.warn "Spree::Store.current is DEPRECATED"
current_store = Store.find_by(code: store_key) || Store.by_url(store_key).first if store_key
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class AddAvailableLocalesToStores < ActiveRecord::Migration[5.1]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing magic comment # frozen_string_literal: true.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing magic comment # frozen_string_literal: true.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing magic comment # frozen_string_literal: true.

def change
change_table :spree_stores do |t|
t.column :available_locales, :string
end
end
end
57 changes: 57 additions & 0 deletions core/spec/models/spree/store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,61 @@
end
end
end

describe '#available_locales' do
let(:store) { described_class.new(available_locales: locales) }
subject { store.available_locales }

context 'with available_locales: []' do
let(:locales) { [] }

it "returns all available locales" do
expect(subject).to eq([:en])
end

it "serializes as nil" do
expect(store[:available_locales]).to be nil
end
end

context 'with available_locales: [:en]' do
let(:locales) { [:en] }

it "returns [:en]" do
expect(subject).to eq([:en])
end

it "serializes correctly" do
expect(store[:available_locales]).to eq("en")
end
end

context 'with available_locales: [:en, :fr]' do
let(:locales) { [:en, :fr] }

it "returns [:fr]" do
expect(subject).to eq([:en, :fr])
end

it "serializes correctly" do
expect(store[:available_locales]).to eq("en,fr")
end
end

context 'with available_locales: [:fr]' do
let(:locales) { [:fr] }

it "returns [:fr]" do
expect(subject).to eq([:fr])
end
end

context 'with available_locales: ["fr"]' do
let(:locales) { ["fr"] }

it "returns symbols [:fr]" do
expect(subject).to eq([:fr])
end
end
end
end