Skip to content

Commit

Permalink
Remove currency config
Browse files Browse the repository at this point in the history
  • Loading branch information
btolarz committed Oct 23, 2019
1 parent 3ed98b0 commit 45d00ca
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ def clear_cache
head :no_content
end

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

private

def update_currency_settings
Expand Down
27 changes: 0 additions & 27 deletions backend/app/views/spree/admin/general_settings/_form.html.erb

This file was deleted.

21 changes: 0 additions & 21 deletions backend/app/views/spree/admin/general_settings/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,6 @@
</div>

</div>
<div class="col-12 col-lg-6">

<%#-------------------------------------------------%>
<%# Currency Settings %>
<%#-------------------------------------------------%>
<div class="card mb-3" id="general-settings-currency" data-hook="general_settings_currency">
<div class="card-header">
<h1 class="card-title mb-0 h5">
<%= Spree.t(:currency_settings)%>
</h1>
</div>

<div class="card-body">
<div class="form-group">
<%= label_tag :currency, Spree.t(:choose_currency) %>
<%= select_tag :currency, currency_options %>
</div>
<%= render partial: 'form' %>
</div>
</div>
</div>
</div>

<div class="form-actions" data-hook="buttons">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@
admin_product_prices_path(@product),
class: "nav-link #{'active' if current == Spree.t(:prices)}" %>

<% end if Spree::Config.allow_currency_change == true %>
<% end if Spree::Store.all_supported_currencies.multiple? %>
</ul>
<% end %>

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions backend/spec/features/admin/products/variant_prices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
let!(:product) { create(:product) }

context 'with USD and EUR as currencies' do
before do
reset_spree_preferences do |config|
config.allow_currency_change = true
end
end

it 'allows to save a price for each currency' do
visit spree.admin_product_path(product)
click_link 'Prices'
Expand Down
4 changes: 0 additions & 4 deletions core/app/models/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ class AppConfiguration < Preferences::Configuration
preference :non_expiring_credit_types, :array, default: []
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

# searcher_class allows spree extension writers to provide their own Search class
def searcher_class
@searcher_class ||= Spree::Core::Search::Base
Expand Down
14 changes: 5 additions & 9 deletions core/app/models/spree/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,12 @@ def copy_price
end

def update_price
if Spree::Config.allow_currency_change == true
currency_price = Spree::Price.where(
currency: order.currency,
variant_id: variant_id
).first
currency_price = Spree::Price.find_by!(
currency: order.currency,
variant_id: variant_id
)

self.price = currency_price.price_including_vat_for(tax_zone: tax_zone)
else
self.price = variant.price_including_vat_for(tax_zone: tax_zone)
end
self.price = currency_price.price_including_vat_for(tax_zone: tax_zone)
end

def copy_tax_category
Expand Down
12 changes: 9 additions & 3 deletions core/app/models/spree/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ def self.default
end
end

def self.all_supported_currencies
::Spree::CurrenciesCollection.new(
pluck(:supported_currencies, :default_currency).flatten.map{ |el| el.split(',')}.flatten
)
end

def supported_currencies_list
currencies = (read_attribute(:supported_currencies).to_s.split(',') << default_currency).map(&:to_s).map do |code|
::Money::Currency.find(code.strip)
end.uniq.compact
::Spree::CurrenciesCollection.new (
supported_currencies.to_s.split(',') << default_currency
)
end

private
Expand Down
6 changes: 0 additions & 6 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,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 @@ -1344,9 +1341,6 @@ 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
Expand Down
1 change: 1 addition & 0 deletions core/lib/spree/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class DestroyWithOrdersError < StandardError; end
require 'spree/permitted_attributes'
require 'spree/service_module'
require 'spree/dependencies_helper'
require 'spree/currencies_collection'

require 'spree/core/importer'
require 'spree/core/query_filters'
Expand Down
18 changes: 18 additions & 0 deletions core/lib/spree/currencies_collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Spree
class CurrenciesCollection < Array
def initialize(array)
super(array.map(&:to_s).map do |el|
return el if el.is_a?(::Money::Currency)
::Money::Currency.find(el.strip)
end.uniq.compact)
end

def by_iso(iso)
find { |currency| currency.iso_code == iso }
end

def multiple?
size > 1
end
end
end
8 changes: 2 additions & 6 deletions core/spec/models/spree/line_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,12 @@
before do
expect(line_item).to receive(:variant_id).and_return(1001)

expect(Spree::Price).to receive(:where).with(
expect(Spree::Price).to receive(:find_by!).with(
currency: "EUR",
variant_id: 1001
).and_return([price])
).and_return(price)

expect(price).to receive(:price_including_vat_for).and_return(12)

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

it 'copies over a variants differing price for another vat zone' do
Expand Down
9 changes: 6 additions & 3 deletions frontend/app/controllers/spree/currency_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module Spree
class CurrencyController < StoreController
def set
@currency = supported_currencies.find { |currency| currency.iso_code == params[:currency] }
@currency = supported_currencies.by_iso(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]
if @currency
current_order&.update_attributes!(currency: @currency.iso_code)
session[:currency] = @currency.iso_code
end

respond_to do |format|
format.json { render json: !@currency.nil? }
format.html do
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/views/spree/shared/_main_nav_bar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</li>
<script>Spree.fetch_cart()</script>

<% if Spree::Config[:allow_currency_change] && Spree::Config[:show_currency_selector] && supported_currencies.size > 1 %>
<% if supported_currencies.multiple? %>
<li id="currency-select" data-hook>
<%= form_tag set_currency_path(format: :html), class: 'navbar-form' do %>
<div class="form-group">
Expand Down
5 changes: 0 additions & 5 deletions frontend/spec/features/order_prices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
let!(:product) { create(:product) }

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

create(:price, variant: product.master, currency: 'EUR', amount: 16.00)
create(:price, variant: product.master, currency: 'GBP', amount: 23.00)
end
Expand Down
36 changes: 12 additions & 24 deletions frontend/spec/features/product_prices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
let!(:product) { create(:product) }

before do
reset_spree_preferences do |config|
config.allow_currency_change = true
config.show_currency_selector = true
end
create(:price, variant: product.master, currency: 'EUR', amount: 16.00)
create(:price, variant: product.master, currency: 'GBP', amount: 23.00)
end
Expand All @@ -22,36 +18,28 @@
select 'GBP', from: 'currency'
expect(page).to have_text '£23.00'
end

context 'and :show_currency_selector is false' do
context 'when many supported store currency' do
before do
reset_spree_preferences do |config|
config.allow_currency_change = true
config.show_currency_selector = false
end
store.update!(supported_currencies: 'EUR,GBP')
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'
expect(page).to have_css('#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
context 'when one supported store currency' do
before do
store.update!(supported_currencies: 'EUR', default_currency: 'EUR')
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
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_css('#currency')
end
end
end
Expand Down

0 comments on commit 45d00ca

Please sign in to comment.