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

Improve pricing options flexibility #2504

Merged
merged 3 commits into from
Feb 8, 2019
Merged
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
4 changes: 2 additions & 2 deletions api/app/helpers/spree/api/api_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def required_fields_for(model)
end

@@product_attributes = [
:id, :name, :description, :price, :display_price, :available_on,
:id, :name, :description, :available_on,
:slug, :meta_description, :meta_keywords, :shipping_category_id,
:taxon_ids, :total_on_hand, :meta_title
]
Expand All @@ -61,7 +61,7 @@ def required_fields_for(model)
]

@@variant_attributes = [
:id, :name, :sku, :price, :weight, :height, :width, :depth, :is_master,
:id, :name, :sku, :weight, :height, :width, :depth, :is_master,
:slug, :description, :track_inventory
]

Expand Down
3 changes: 2 additions & 1 deletion api/app/views/spree/api/products/_product.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
@product_attributes ||= product_attributes
json.cache! [I18n.locale, @current_user_roles.include?('admin'), current_pricing_options, @product_attributes, @exclude_data, product] do
json.(product, *@product_attributes)
json.display_price(product.display_price.to_s)
json.price(product.price_for(current_pricing_options).try(:to_d))
json.display_price(product.price_for(current_pricing_options).to_s)

@exclude_data ||= {}
unless @exclude_data[:variants]
Expand Down
5 changes: 3 additions & 2 deletions api/app/views/spree/api/variants/_small.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

json.cache! [I18n.locale, variant] do
json.cache! [I18n.locale, current_pricing_options, variant] do
json.(variant, *variant_attributes)
json.display_price(variant.display_price.to_s)
json.price(variant.price_for(current_pricing_options).try(:to_d))
json.display_price(variant.price_for(current_pricing_options).to_s)
json.options_text(variant.options_text)
json.track_inventory(variant.should_track_inventory?)
json.in_stock(variant.in_stock?)
Expand Down
2 changes: 1 addition & 1 deletion backend/app/views/spree/admin/variants/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<div class="col-3">
<div class="field" data-hook="price">
<%= f.label :price %>
<%= render "spree/admin/shared/number_with_currency", f: f, amount_attr: :price, currency: @variant.default_price.currency %>
<%= render "spree/admin/shared/number_with_currency", f: f, amount_attr: :price, currency: @variant.find_or_build_default_price.currency %>
</div>
</div>

Expand Down
10 changes: 10 additions & 0 deletions core/app/models/spree/variant/pricing_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ def self.from_price(price)
new(currency: price.currency, country_iso: price.country_iso)
end

# This creates the correct pricing options for a price, so the store owners can easily customize how to
# find the pricing based on the view context, having available current_store, current_spree_user, request.host_name, etc.
# @return [Spree::Variant::PricingOptions] pricing options for pricing a line item
def self.from_context(context)
new(
currency: context.current_store.try!(:default_currency).presence || Spree::Config[:currency],
country_iso: context.current_store.try!(:cart_tax_country_iso).presence
)
end

# @return [Hash] The hash of exact desired attributes
attr_reader :desired_attributes

Expand Down
5 changes: 1 addition & 4 deletions core/lib/spree/core/controller_helpers/pricing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ module Pricing
end

def current_pricing_options
Spree::Config.pricing_options_class.new(
currency: current_store.try!(:default_currency).presence || Spree::Config[:currency],
country_iso: current_store.try!(:cart_tax_country_iso).presence
)
Spree::Config.pricing_options_class.from_context(self)
end

def current_currency
Expand Down
13 changes: 13 additions & 0 deletions core/spec/lib/spree/core/controller_helpers/pricing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,18 @@ class FakesController < ApplicationController
it { is_expected.to be_nil }
end
end

context "from context" do
subject { controller.current_pricing_options }

let(:store) { FactoryBot.create :store, default_currency: 'USD' }

context "when the whole context is passed" do
it "receives the right object " do
expect(Spree::Config.pricing_options_class).to receive(:from_context).with(controller)
is_expected.to be_nil
end
end
end
end
end
23 changes: 23 additions & 0 deletions core/spec/models/spree/variant/pricing_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,29 @@
end
end

context ".from_context" do
let(:view_context) { double(ApplicationController, current_store: store) }
subject { described_class.from_context(view_context) }

context "if the store has not defined default_currency" do
let(:store) { FactoryBot.create :store, default_currency: nil, cart_tax_country_iso: nil }

it "fallbacks to Spree::Config.currency" do
expect(Spree::Variant::PricingOptions).to receive(:new).with(currency: Spree::Config.currency, country_iso: nil)
expect(subject).to be_nil
end
end

context 'if the store has default_currency and cart_tax_country_iso' do
let(:store) { FactoryBot.create :store, default_currency: 'MXN' }

it "uses current_store information" do
expect(Spree::Variant::PricingOptions).to receive(:new).with(currency: store.default_currency, country_iso: store.cart_tax_country_iso)
expect(subject).to be_nil
end
end
end

describe '#desired_attributes' do
context "when called with no arguments" do
it "returns the default pricing options" do
Expand Down