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

Remove currency config #26

Draft
wants to merge 19 commits into
base: feature/multi-currency
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def find_spree_current_order
)
end

def supported_currencies
spree_current_store.supported_currencies_list
end

def serialize_order(order)
resource_serializer.new(order.reload, include: resource_includes, fields: sparse_fields).serializable_hash
end
Expand Down
10 changes: 9 additions & 1 deletion api/app/controllers/spree/api/v2/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def spree_current_store
@spree_current_store ||= Spree::Store.current(request.env['SERVER_NAME'])
end

def spree_supported_currencies
spree_current_store.supported_currencies_list
end

def spree_current_user
@spree_current_user ||= Spree.user_class.find_by(id: doorkeeper_token.resource_owner_id) if doorkeeper_token
end
Expand Down Expand Up @@ -105,7 +109,11 @@ def sparse_fields
end

def current_currency
spree_current_store.default_currency || Spree::Config[:currency]
if params[:currency].present? && spree_supported_currencies.map(&:iso_code).include?(params[:currency])
params[:currency]
else
spree_current_store.default_currency || Spree::Config[:currency]
end
end

def record_not_found
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ def resource_serializer
end

def scope
Spree::Product.accessible_by(current_ability, :show).includes(scope_includes)
Spree::Product.accessible_by(current_ability, :show).
joins(master: :prices).where(spree_prices: {currency: current_currency}).
select('*, spree_prices.currency as currency, spree_prices.amount as price').
includes(scope_includes)
end

def scope_includes
{
master: :default_price,
variants: [],
variant_images: [],
taxons: [],
Expand Down
10 changes: 9 additions & 1 deletion api/app/serializers/spree/v2/storefront/product_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ module Storefront
class ProductSerializer < BaseSerializer
set_type :product

attributes :name, :description, :price, :currency, :display_price,
attributes :name, :description, :display_price,
:available_on, :slug, :meta_description, :meta_keywords,
:updated_at

attribute :price do |object|
object[:price]
end

attribute :currency do |object|
object[:currency]
end

attribute :purchasable, &:purchasable?
attribute :in_stock, &:in_stock?
attribute :backorderable, &:backorderable?
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 @@ -76,7 +76,8 @@ module Spree
name: 'Hack0rz',
url: 'spree123.example.com',
mail_from_address: '[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
78 changes: 55 additions & 23 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 All @@ -28,7 +28,8 @@

describe 'cart#create' do
let(:order) { Spree::Order.last }
let(:execute) { post '/api/v2/storefront/cart', headers: headers }
let(:params) { {} }
let(:execute) { post '/api/v2/storefront/cart', headers: headers, params: params }

shared_examples 'creates an order' do
before { execute }
Expand All @@ -38,17 +39,32 @@
end

shared_examples 'creates an order with different currency' do
before do
store.default_currency = 'EUR'
store.save!
execute
context 'store default' do
before do
store.default_currency = 'EUR'
store.save!
execute
end

it_behaves_like 'returns valid cart JSON'
it_behaves_like 'returns 201 HTTP status'

it 'sets requested currency' do
expect(json_response['data']).to have_attribute(:currency).with_value('EUR')
end
end

it_behaves_like 'returns valid cart JSON'
it_behaves_like 'returns 201 HTTP status'
context 'currency passed as a param' do
let(:params) { { currency: 'EUR' } }

before { execute }

it_behaves_like 'returns valid cart JSON'
it_behaves_like 'returns 201 HTTP status'

it 'sets proper currency' do
expect(json_response['data']).to have_attribute(:currency).with_value('EUR')
it 'sets requested currency' do
expect(json_response['data']).to have_attribute(:currency).with_value('EUR')
end
end
end

Expand Down Expand Up @@ -305,9 +321,11 @@
end

describe 'cart#show' do
let(:params) { {} }

shared_examples 'showing the cart' do
before do
get '/api/v2/storefront/cart', headers: headers
get '/api/v2/storefront/cart', headers: headers, params: params
end

it_behaves_like 'returns 200 HTTP status'
Expand Down Expand Up @@ -341,11 +359,11 @@
end

context 'for specified currency' do
before do
store.update!(default_currency: 'EUR')
end
context 'store default' do
before do
store.update!(default_currency: 'EUR')
end

context 'with matching currency' do
include_context 'creates guest order with guest token'

it_behaves_like 'showing the cart'
Expand All @@ -355,6 +373,20 @@
expect(json_response['data']).to have_attribute(:currency).with_value('EUR')
end
end

context 'passed as a param' do
let(:currency) { 'EUR' }
let(:params) { { currency: currency } }

include_context 'creates guest order with guest token'

it_behaves_like 'showing the cart'

it 'includes the requested currency' do
get '/api/v2/storefront/cart', headers: headers, params: params
expect(json_response['data']).to have_attribute(:currency).with_value('EUR')
end
end
end

context 'with option: include' do
Expand Down Expand Up @@ -509,9 +541,9 @@

context 'tries to remove an empty string' do
let!(:coupon_code) { '' }

before { execute }

it 'changes the adjustment total to 0.0' do
expect(json_response['data']).to have_attribute(:adjustment_total).with_value(0.0.to_s)
end
Expand All @@ -520,12 +552,12 @@
expect(json_response['included']).not_to include(have_type('promotion'))
end
end

context 'tries to remove nil' do
let(:coupon_code) { nil }

before { execute }

it 'changes the adjustment total to 0.0' do
expect(json_response['data']).to have_attribute(:adjustment_total).with_value(0.0.to_s)
end
Expand Down Expand Up @@ -582,7 +614,7 @@
context 'tries to remove an empty string' do
let!(:coupon_code) { '' }
before { execute }

it 'changes the adjustment total to 0.0' do
expect(json_response['data']).to have_attribute(:adjustment_total).with_value(0.0.to_s)
end
Expand All @@ -591,11 +623,11 @@
expect(json_response['included']).not_to include(have_type('promotion'))
end
end

context 'tries to remove nil' do
let(:coupon_code) { nil }
before { execute }

it 'changes the adjustment total to 0.0' do
expect(json_response['data']).to have_attribute(:adjustment_total).with_value(0.0.to_s)
end
Expand Down
50 changes: 50 additions & 0 deletions api/spec/requests/spree/api/v2/storefront/products_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'spec_helper'

describe 'API V2 Storefront Products Spec', type: :request do
let!(:store) { create(:store, default: true, supported_currencies: 'USD,PLN') }
let!(:products) { create_list(:product, 5) }
let(:taxon) { create(:taxon) }
let(:product_with_taxon) { create(:product, taxons: [taxon]) }
Expand All @@ -24,6 +25,32 @@
expect(json_response['data'].count).to eq Spree::Product.not_discontinued.count
expect(json_response['data'].first).to have_type('product')
end


it 'returns all products with ' do
expect(json_response['data'].count).to eq Spree::Product.not_discontinued.count
expect(json_response['data'].first).to have_type('product')
end
end

context 'with currency' do
let(:currency) { 'PLN' }
before { get "/api/v2/storefront/products?currency=#{currency}" }

let!(:products_collection) {
create_list(
:product, 5, master: create(
:variant, is_master: true, prices: [
create(:price, currency: store.default_currency),
create(:price, currency: currency),
]
)
)
}

it 'returns products with provided currency' do
expect(json_response['data'].map{ |el| el['attributes']['currency']}.all?{ |el| el == currency} ).to be true
end
end

context 'with specified ids' do
Expand Down Expand Up @@ -269,5 +296,28 @@
)
end
end

context 'with currency' do

let!(:currency) { 'PLN' }
let!(:product_in_currency) {
create(
:product, master: create(
:variant, is_master: true, prices: [
create(:price, currency: store.default_currency),
create(:price, currency: currency)
]
)
)
}

it_behaves_like 'returns 200 HTTP status'

before { get "/api/v2/storefront/products/#{product_in_currency.slug}?currency=#{currency}" }

it 'returns product price in provided currency' do
expect(json_response['data']).to have_attribute(:currency).with_value(currency)
end
end
end
end
1 change: 1 addition & 0 deletions backend/app/assets/javascripts/spree/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//= require spree/backend/handlebar_extensions
//= require spree/backend/line_items
//= require spree/backend/line_items_on_order_edit
//= require spree/backend/multi_currency
//= require spree/backend/option_type_autocomplete
//= require spree/backend/option_value_picker
//= require spree/backend/orders/edit
Expand Down
2 changes: 1 addition & 1 deletion backend/app/assets/javascripts/spree/backend/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function handle_date_picker_fields () {
})
}

$(document).ready(function(){
$(document).ready(function() {
handle_date_picker_fields()
$('.observe_field').on('change', function() {
target = $(this).data('update')
Expand Down
48 changes: 48 additions & 0 deletions backend/app/assets/javascripts/spree/backend/multi_currency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//= require spree/backend

$(document).ready(function () {
$.expr[':'].Contains = function (a, i, m) {
return (
(a.textContent || a.innerText || '')
.toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0
)
}

function listFilter (list) {
var input = $('#variant-price-search')

$(input)
.change(function () {
var filter = $(this).val()
if (filter) {
$(list)
.find('.panel-title:not(:Contains(' + filter + '))')
.parent()
.parent()
.parent()
.hide()
$(list)
.find('.panel-title:Contains(' + filter + ')')
.parent()
.parent()
.parent()
.show()
} else {
$(list)
.find('.panel')
.parent()
.show()
}
return false
})
.keyup(function () {
$(this).change()
})
}

// ondomready
$(function () {
listFilter($('#variant-prices'))
})
})
Loading