Skip to content

Commit

Permalink
Merge pull request #10533 from spark-solutions/SD-959-show-or-hide-cu…
Browse files Browse the repository at this point in the history
…rrency-selector-as-admin

Sd 959 show or hide currency selector as admin
  • Loading branch information
damianlegawiec committed Oct 29, 2020
2 parents d002bbb + 028dcd4 commit c315a82
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 69 deletions.
Expand Up @@ -27,7 +27,7 @@ def clear_cache
end

def render(*args)
@preferences_currency |= [:allow_currency_change, :show_currency_selector]
@preferences_currency |= [:show_store_currency_selector]
super
end

Expand Down
16 changes: 3 additions & 13 deletions backend/app/views/spree/admin/general_settings/_form.html.erb
Expand Up @@ -7,20 +7,10 @@
<div class="form-group">
<div class="checkbox">
<label>
<%= check_box('', :allow_currency_change, { checked: Spree::Config.allow_currency_change }) %>
<%= Spree.t('allow_currency_change.short') %>
<%= check_box('', :show_store_currency_selector, { checked: Spree::Config.show_store_currency_selector }) %>
<%= Spree.t('show_store_currency_selector.short') %>
</label>
<p class="help-block"><%= Spree.t('allow_currency_change.long') %></p>
</div>
</div>

<div class="form-group">
<div class="checkbox">
<label>
<%= check_box('', :show_currency_selector, { checked: Spree::Config.show_currency_selector}) %>
<%= Spree.t('show_currency_selector.short') %>
</label>
<p class="help-block"><%= Spree.t('show_currency_selector.long_html') %></p>
<p class="help-block"><%= Spree.t('show_store_currency_selector.long') %></p>
</div>
</div>
</div>
Expand Down
Expand Up @@ -7,8 +7,7 @@
it 'Multi Currency settings are present' do
visit spree.edit_admin_general_settings_path

expect(page).to have_content 'Allow Currency Change'
expect(page).to have_content 'Show Currency Selector'
expect(page).to have_content 'Show Store selector'
end
end
end
Expand Up @@ -7,51 +7,43 @@
describe 'enabling currency settings' do
before do
reset_spree_preferences do |config|
config.allow_currency_change = false
config.show_currency_selector = false
config.show_store_currency_selector = false
end
end

it 'allows to enable currency settings' do
visit spree.edit_admin_general_settings_path

# Test initial state
expect(page).to have_unchecked_field('allow_currency_change')
expect(page).to have_unchecked_field('show_currency_selector')
expect(page).to have_unchecked_field('show_store_currency_selector')

# Interact with the form
check('allow_currency_change')
check('show_currency_selector')
check('show_store_currency_selector')
click_button 'Update'

# Test final state
expect(page).to have_content 'General Settings has been successfully updated!'
expect(page).to have_checked_field('allow_currency_change')
expect(page).to have_checked_field('show_currency_selector')
expect(page).to have_checked_field('show_store_currency_selector')
end
end

describe 'disabling currency settings' do
before do
reset_spree_preferences do |config|
config.allow_currency_change = true
config.show_currency_selector = true
config.show_store_currency_selector = true
end
end

it 'allows to disable currency settings' do
visit spree.edit_admin_general_settings_path

expect(page).to have_checked_field('allow_currency_change')
expect(page).to have_checked_field('show_currency_selector')
expect(page).to have_checked_field('show_store_currency_selector')

uncheck('allow_currency_change')
uncheck('show_currency_selector')
uncheck('show_store_currency_selector')
click_button 'Update'

expect(page).to have_content 'General Settings has been successfully updated!'
expect(page).to have_unchecked_field('allow_currency_change')
expect(page).to have_unchecked_field('show_currency_selector')
expect(page).to have_unchecked_field('show_store_currency_selector')
end
end
end
3 changes: 1 addition & 2 deletions core/app/models/spree/app_configuration.rb
Expand Up @@ -69,8 +69,7 @@ class AppConfiguration < Preferences::Configuration
preference :credit_to_new_allocation, :boolean, default: false

# Multi currency configurations
preference :allow_currency_change, :boolean, default: false
preference :show_currency_selector, :boolean, default: false
preference :show_store_currency_selector, :boolean, default: false

# searcher_class allows spree extension writers to provide their own Search class
def searcher_class
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/line_item.rb
Expand Up @@ -57,7 +57,7 @@ def copy_price
end

def update_price
if Spree::Config.allow_currency_change == true
if Spree::Config.show_store_currency_selector == true
currency_price = Spree::Price.where(
currency: order.currency,
variant_id: variant_id
Expand Down
9 changes: 3 additions & 6 deletions core/config/locales/en.yml
Expand Up @@ -575,9 +575,6 @@ en:
agree_to_privacy_policy: Agree to Privacy Policy
agree_to_terms_of_service: Agree to Terms of Service
all: All
allow_currency_change:
short: Allow Currency Change
long: Allow users to change their currency via the currency picker.
all_adjustments_closed: All adjustments successfully closed!
all_adjustments_opened: All adjustments successfully opened!
all_departments: All departments
Expand Down Expand Up @@ -1572,15 +1569,15 @@ en:
shopping_cart: Shopping Cart
show: Show
show_active: Show Active
show_currency_selector:
short: Show Currency Selector
long_html: Display the currency picker in the main nav bar. This will only display if there are multiple supported currencies, and <strong>Allow currency change</strong> option is enabled.
show_deleted: Show Deleted
show_discontinued: Show Discontinued
show_only_complete_orders: Only show complete orders
show_only_considered_risky: Only show risky orders
show_property: Show Property
show_rate_in_label: Show rate in label
show_store_currency_selector:
short: Show Store selector
long: Display the Store selector in the main nav bar of Storefront and allow users to change Store and Currency
sign_up: Sign Up
sku: SKU
skus: SKUs
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/line_item_spec.rb
Expand Up @@ -187,7 +187,7 @@
expect(price).to receive(:price_including_vat_for).and_return(12)

reset_spree_preferences do |config|
config.allow_currency_change = true
config.show_store_currency_selector = true
end
end

Expand Down
2 changes: 1 addition & 1 deletion frontend/app/controllers/spree/currency_controller.rb
Expand Up @@ -4,7 +4,7 @@ def set
@currency = supported_currencies.find { |currency| currency.iso_code == params[:currency] }
# Make sure that we update the current order, so the currency change is reflected.
current_order&.update_attributes!(currency: @currency.iso_code)
session[:currency] = params[:currency] if Spree::Config[:allow_currency_change]
session[:currency] = params[:currency] if Spree::Config[:show_store_currency_selector]
respond_to do |format|
format.json { render json: !@currency.nil? }
format.html do
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/views/spree/shared/_change_store.html.erb
@@ -1,4 +1,4 @@
<% if stores.size > 1 %>
<% if Spree::Config.show_store_currency_selector && stores.size > 1 %>
<li class="change-store">
<div class="dropdown navbar-right-dropdown">
<button id="stores-button" type="button" data-toggle="dropdown" class="navbar-right-dropdown-toggle" aria-label="<%= Spree.t('nav_bar.change_country')%>">
Expand All @@ -12,7 +12,7 @@
height: 15) %>
</button>

<div class="dropdown-menu dropdown-menu-right text-right">
<div id="stores_list" class="dropdown-menu dropdown-menu-right text-right">
<% stores.each do |store| %>
<div class="pl-3 d-flex justify-content-between align-items-center <%= 'active' if store.id == current_store.id %>">
<span class="flag-icon flag-icon-<%= store_country_iso(store) %>"></span>
Expand Down
78 changes: 78 additions & 0 deletions frontend/spec/features/main_nav_bar_spec.rb
@@ -0,0 +1,78 @@
require 'spec_helper'

describe 'Main navigation bar', type: :feature do
describe 'change store' do
shared_examples 'change store not available' do
it 'does not show currency selector button' do
expect(page).not_to have_button(id: 'stores-button')
end

it 'does not render stores list' do
expect(page).not_to have_selector('div#stores_list')
end
end

context 'when show_store_currency_selector preference is set to true' do
let!(:stores) { create_list(:store, stores_number, default_country: create(:country)) }

before do
reset_spree_preferences do |config|
config.show_store_currency_selector = true
end

visit spree.root_path
end

context 'when there is one supported currency' do
let(:stores_number) { 0 }

it_behaves_like 'change store not available'
end

context 'when there are more than one supported currencies' do
let(:stores_number) { 2 }
let(:first_store) { stores.first }
let(:first_store_currency_symbol) { ::Money::Currency.find(first_store.default_currency).symbol }
let(:first_link_name) { "#{first_store.default_country&.name} (#{first_store_currency_symbol})" }
let(:first_url) { "//#{first_store.url}" }
let(:second_store) { stores.second }
let(:second_store_currency_symbol) { ::Money::Currency.find(second_store.default_currency).symbol }
let(:second_link_name) { "#{second_store.default_country&.name} (#{first_store_currency_symbol})" }
let(:second_url) { "//#{second_store.url}" }

it 'shows currency selector button' do
within('.change-store') { expect(page).to have_button(id: 'stores-button') }
end

it 'currency selector button shows a links list to currencies' do
within('.change-store') { expect(page).to have_link(first_link_name, href: first_url) }
within('.change-store') { expect(page).to have_link(second_link_name, href: second_url) }
end
end
end

context 'when show_store_currency_selector preference is set to false' do
let!(:stores) { create_list(:store, stores_number) }

before do
reset_spree_preferences do |config|
config.show_store_currency_selector = false
end

visit spree.root_path
end

context 'when there is one supported currency' do
let(:stores_number) { 0 }

it_behaves_like 'change store not available'
end

context 'when there are more than one supported currencies' do
let(:stores_number) { 2 }

it_behaves_like 'change store not available'
end
end
end
end
3 changes: 1 addition & 2 deletions frontend/spec/features/order_prices_spec.rb
Expand Up @@ -6,8 +6,7 @@

before do
reset_spree_preferences do |config|
config.allow_currency_change = true
config.show_currency_selector = true
config.show_store_currency_selector = true
end

create(:price, variant: product.master, currency: 'EUR', amount: 16.00)
Expand Down
25 changes: 3 additions & 22 deletions frontend/spec/features/product_prices_spec.rb
Expand Up @@ -8,8 +8,7 @@

before do
reset_spree_preferences do |config|
config.allow_currency_change = true
config.show_currency_selector = true
config.show_store_currency_selector = true
end
create(:price, variant: product.master, currency: 'EUR', amount: 16.00)
create(:price, variant: product.master, currency: 'GBP', amount: 23.00)
Expand All @@ -24,11 +23,10 @@
expect(page).to have_text '£23.00'
end

context 'and :show_currency_selector is false' do
context 'and :show_store_currency_selector is false' do
before do
reset_spree_preferences do |config|
config.allow_currency_change = true
config.show_currency_selector = false
config.show_store_currency_selector = false
end
end

Expand All @@ -38,23 +36,6 @@
expect(page).to_not have_text 'Currency'
end
end

context 'and :allow_currency_change is false' do
context 'and show_currency_selector is true' do
before do
reset_spree_preferences do |config|
config.allow_currency_change = false
config.show_currency_selector = true
end
end

it 'will not render the currency selector' do
visit spree.product_path(product)
expect(page).to have_current_path(spree.product_path(product))
expect(page).to_not have_text 'Currency'
end
end
end
end
end

Expand Down
4 changes: 4 additions & 0 deletions guides/src/content/developer/core/preferences.md
Expand Up @@ -426,6 +426,10 @@ Determines if, on the admin listing screen, only completed orders should be show

Determines if the variant's full price or price difference from a product should be displayed on the product's show page. Defaults to `false`.

`show_store_currency_selector`

Display the Store selector in the main nav bar of Storefront and allow users to change Store and Currency. Defaults to `false`.

`tax_using_ship_address`

Determines if tax information should be based on shipping address, rather than the billing address. Defaults to `true`.
Expand Down

0 comments on commit c315a82

Please sign in to comment.