Skip to content

Commit

Permalink
Moved supported currencies from preferencies to store
Browse files Browse the repository at this point in the history
  • Loading branch information
btolarz authored and damianlegawiec committed Apr 21, 2020
1 parent 6729a5f commit c88795b
Show file tree
Hide file tree
Showing 22 changed files with 62 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ def find_spree_current_order
end

def supported_currencies
Spree::Config[:supported_currencies].split(',').map do |code|
::Money::Currency.find(code.strip)
end
spree_current_store.supported_currencies_list
end

def serialize_order(order)
Expand Down
3 changes: 2 additions & 1 deletion api/spec/controllers/spree/api/v1/stores_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ module Spree
url: 'spree123.example.com',
mail_from_address: '[email protected]',
customer_support_email: '[email protected]',
default_currency: 'USD'
default_currency: 'USD',
supported_currencies: 'USD'
}
api_post :create, store: store_hash
expect(response.status).to eq(201)
Expand Down
6 changes: 1 addition & 5 deletions api/spec/requests/spree/api/v2/storefront/cart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe 'API V2 Storefront Cart Spec', type: :request do
let(:default_currency) { 'USD' }
let(:store) { create(:store, default_currency: default_currency) }
let!(:store) { create(:store, default_currency: default_currency) }
let(:currency) { store.default_currency }
let(:user) { create(:user) }
let(:order) { create(:order, user: user, store: store, currency: currency) }
Expand Down Expand Up @@ -359,10 +359,6 @@
end

context 'for specified currency' do
before do
Spree::Config[:supported_currencies] = 'USD,EUR'
end

context 'store default' do
before do
store.update!(default_currency: 'EUR')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def clear_cache
end

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

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

<div class="panel-body">
<div class="form-group">
<label><%= Spree.t('supported_currencies.short') %></label>
<input type="text" class="form-control" value="<%= Spree::Config.supported_currencies %>" name="supported_currencies"/>
<p class="help-block"><%= Spree.t('supported_currencies.long') %></p>
</div>

<div class="form-group">
<div class="checkbox">
<label>
Expand Down
1 change: 0 additions & 1 deletion backend/app/views/spree/admin/stores/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<div data-hook="admin_store_form_fields">

<div class="card">
<div class="card-header">
<h1 class="card-title mb-0 h5">
Expand Down
4 changes: 3 additions & 1 deletion backend/spec/features/admin/configuration/stores_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
store_table = page.find('table')
expect(store_table).to have_content(updated_name)
expect(store_table).to have_content(new_currency)
expect(store.reload.name).to eq updated_name
store.reload
expect(store.name).to eq updated_name
expect(store.supported_currencies).to eq new_currency
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
it 'Multi Currency settings are present' do
visit spree.edit_admin_general_settings_path

expect(page).to have_content 'Supported Currencies'
expect(page).to have_content 'Allow Currency Change'
expect(page).to have_content 'Show Currency Selector'
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require 'spec_helper'

describe 'Updating currencies settings', type: :feature, js: true do
let!(:store) { create(:store, default: true) }
stub_authorization!

before do
reset_spree_preferences do |config|
config.supported_currencies = 'USD'
config.allow_currency_change = false
config.show_currency_selector = false
end
Expand All @@ -15,19 +15,16 @@
visit spree.edit_admin_general_settings_path

# Test initial state
expect(page).to have_field('supported_currencies', with: 'USD')
expect(page).to have_unchecked_field('allow_currency_change')
expect(page).to have_unchecked_field('show_currency_selector')

# Interact with the form
fill_in 'supported_currencies', with: 'USD,PLN'
check('allow_currency_change')
check('show_currency_selector')
click_button 'Update'

# Test final state
expect(page).to have_content 'General Settings has been successfully updated!'
expect(page).to have_field('supported_currencies', with: 'USD,PLN')
expect(page).to have_checked_field('allow_currency_change')
expect(page).to have_checked_field('show_currency_selector')
end
Expand Down
3 changes: 1 addition & 2 deletions backend/spec/features/admin/products/variant_prices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

describe 'Variant Prices', type: :feature, js: true do
stub_authorization!

let!(:store) { create(:store, default: true) }
let!(:product) { create(:product) }

context 'with USD and EUR as currencies' do
before do
reset_spree_preferences do |config|
config.supported_currencies = 'USD,EUR'
config.allow_currency_change = true
end
end
Expand Down
1 change: 0 additions & 1 deletion core/app/models/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class AppConfiguration < Preferences::Configuration
# Multi currency configurations
preference :allow_currency_change, :boolean, default: false
preference :show_currency_selector, :boolean, default: false
preference :supported_currencies, :string, default: 'USD'

# searcher_class allows spree extension writers to provide their own Search class
def searcher_class
Expand Down
10 changes: 10 additions & 0 deletions core/app/models/spree/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class Store < Spree::Base
validates :default_currency
end

if connection.column_exists?(:spree_stores, :supported_currencies)
validates :supported_currencies, presence: true
end

before_save :ensure_default_exists_and_is_unique
before_destroy :validate_not_default

Expand All @@ -26,6 +30,12 @@ def self.default
end
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
end

private

def ensure_default_exists_and_is_unique
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddSupportedCurrenciesToStore < ActiveRecord::Migration[6.0]
def change
add_column :spree_stores, :supported_currencies, :string
Spree::Store.all.each do |store|
store.update_attribute(:supported_currencies, store.default_currency)
end
end
end
2 changes: 1 addition & 1 deletion core/lib/spree/core/controller_helpers/currency_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.included(receiver)
end

def supported_currencies
Spree::Config[:supported_currencies].split(',').map { |code| ::Money::Currency.find(code.strip) }
current_store.supported_currencies_list
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions core/lib/spree/core/controller_helpers/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ def current_currency
end

def supported_currencies
Spree::Config[:supported_currencies].split(',').map do |code|
::Money::Currency.find(code.strip)
end
current_store.supported_currencies_list
end

def current_store
Expand Down
3 changes: 2 additions & 1 deletion core/lib/spree/permitted_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ module PermittedAttributes

@@store_attributes = [:name, :url, :seo_title, :code, :meta_keywords,
:meta_description, :default_currency, :mail_from_address,
:customer_support_email, :facebook, :twitter, :instagram, :default_locale]
:customer_support_email, :facebook, :twitter, :instagram,
:default_locale, :supported_currencies]

@@store_credit_attributes = %i[amount currency category_id memo]

Expand Down
1 change: 1 addition & 0 deletions core/lib/spree/testing_support/factories/store_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
mail_from_address { '[email protected]' }
customer_support_email { '[email protected]' }
default_currency { 'USD' }
supported_currencies { 'USD,EUR,GBP' }
facebook { 'spreecommerce' }
twitter { 'spreecommerce' }
instagram { 'spreecommerce' }
Expand Down
9 changes: 9 additions & 0 deletions core/spec/lib/spree/core/controller_helpers/store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ class FakesController < ApplicationController
end
end

describe '#supported_currencies' do
let(:currency) { 'EUR' }
let!(:store) { create :store, default: true, supported_currencies: currency }

it 'returns supported currencies' do
expect(controller.supported_currencies).to include(::Money::Currency.find(currency))
end
end

describe '#current_price_options' do
subject(:current_price_options) { controller.current_price_options }

Expand Down
10 changes: 10 additions & 0 deletions core/spec/models/spree/store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,15 @@
expect(Spree::Store.default.default).to be(true)
end
end

describe '.supported_currencies_list' do
let(:currencies) { 'USD, EUR, dummy' }
let!(:store) { create(:store, supported_currencies: currencies) }
it 'returns supported currencies list' do
expect(store.supported_currencies_list).to contain_exactly(
::Money::Currency.find('USD'), ::Money::Currency.find('EUR')
)
end
end
end
end
3 changes: 2 additions & 1 deletion frontend/spec/features/order_prices_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
require 'spec_helper'

describe 'Order', type: :feature, js: true do
let!(:store) { create(:store, default: true) }
let!(:product) { create(:product) }

before do
reset_spree_preferences do |config|
config.supported_currencies = 'USD,EUR,GBP'
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
4 changes: 1 addition & 3 deletions frontend/spec/features/product_prices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

describe 'Product with prices in multiple currencies', type: :feature, js: true do
context 'with USD, EUR and GBP as currencies' do
let!(:store) { create(:store, default: true) }
let!(:product) { create(:product) }

before do
reset_spree_preferences do |config|
config.supported_currencies = 'USD,EUR,GBP'
config.allow_currency_change = true
config.show_currency_selector = true
end
Expand All @@ -26,7 +26,6 @@
context 'and :show_currency_selector is false' do
before do
reset_spree_preferences do |config|
config.supported_currencies = 'USD,EUR,GBP'
config.allow_currency_change = true
config.show_currency_selector = false
end
Expand All @@ -43,7 +42,6 @@
context 'and show_currency_selector is true' do
before do
reset_spree_preferences do |config|
config.supported_currencies = 'USD,EUR,GBP'
config.allow_currency_change = false
config.show_currency_selector = true
end
Expand Down
8 changes: 7 additions & 1 deletion frontend/spec/features/template_rendering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

describe 'Template rendering', type: :feature do
it 'layout should have canonical tag referencing site url' do
Spree::Store.default.update(url: 'spreestore.example.com')
Spree::Store.default.update(
name: 'My Spree Store',
url: 'spreestore.example.com',
mail_from_address: '[email protected]',
default_currency: 'USD',
supported_currencies: 'USD'
)

visit spree.root_path
expect(find('link[rel=canonical]', visible: false)[:href]).to eql('http://spreestore.example.com/')
Expand Down

0 comments on commit c88795b

Please sign in to comment.