forked from spree/spree
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Product helpers needed in backend, and remove auth devise templates.
- Loading branch information
Jeff Dutil
authored and
Jeff Dutil
committed
Dec 2, 2014
1 parent
f4b0a48
commit 18fbd66
Showing
10 changed files
with
280 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module Spree | ||
module ProductsHelper | ||
# returns the formatted price for the specified variant as a full price or a difference depending on configuration | ||
def variant_price(variant) | ||
if Spree::Config[:show_variant_full_price] | ||
variant_full_price(variant) | ||
else | ||
variant_price_diff(variant) | ||
end | ||
end | ||
|
||
# returns the formatted price for the specified variant as a difference from product price | ||
def variant_price_diff(variant) | ||
variant_amount = variant.amount_in(current_currency) | ||
product_amount = variant.product.amount_in(current_currency) | ||
return if variant_amount == product_amount || product_amount.nil? | ||
diff = variant.amount_in(current_currency) - product_amount | ||
amount = Spree::Money.new(diff.abs, currency: current_currency).to_html | ||
label = diff > 0 ? :add : :subtract | ||
"(#{Spree.t(label)}: #{amount})".html_safe | ||
end | ||
|
||
# returns the formatted full price for the variant, if at least one variant price differs from product price | ||
def variant_full_price(variant) | ||
product = variant.product | ||
unless product.variants.active(current_currency).all? { |v| v.price == product.price } | ||
Spree::Money.new(variant.price, { currency: current_currency }).to_html | ||
end | ||
end | ||
|
||
# converts line breaks in product description into <p> tags (for html display purposes) | ||
def product_description(product) | ||
if Spree::Config[:show_raw_product_description] | ||
raw(product.description) | ||
else | ||
raw(product.description.gsub(/(.*?)\r?\n\r?\n/m, '<p>\1</p>')) | ||
end | ||
end | ||
|
||
def line_item_description(variant) | ||
ActiveSupport::Deprecation.warn "line_item_description(variant) is deprecated and may be removed from future releases, use line_item_description_text(line_item.description) instead.", caller | ||
|
||
line_item_description_text(variant.product.description) | ||
end | ||
|
||
def line_item_description_text description_text | ||
if description_text.present? | ||
truncate(strip_tags(description_text.gsub(' ', ' ')), length: 100) | ||
else | ||
Spree.t(:product_has_no_description) | ||
end | ||
end | ||
|
||
def cache_key_for_products | ||
count = @products.count | ||
max_updated_at = (@products.maximum(:updated_at) || Date.today).to_s(:number) | ||
"#{I18n.locale}/#{current_currency}/spree/products/all-#{params[:page]}-#{max_updated_at}-#{count}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
module Spree | ||
describe ProductsHelper, :type => :helper do | ||
include ProductsHelper | ||
|
||
let(:product) { create(:product) } | ||
let(:currency) { 'USD' } | ||
|
||
before do | ||
allow(helper).to receive(:current_currency) { currency } | ||
end | ||
|
||
context "#variant_price_diff" do | ||
let(:product_price) { 10 } | ||
let(:variant_price) { 10 } | ||
|
||
before do | ||
@variant = create(:variant, :product => product) | ||
product.price = 15 | ||
@variant.price = 10 | ||
allow(product).to receive(:amount_in) { product_price } | ||
allow(@variant).to receive(:amount_in) { variant_price } | ||
end | ||
|
||
subject { helper.variant_price(@variant) } | ||
|
||
context "when variant is same as master" do | ||
it { is_expected.to be_nil } | ||
end | ||
|
||
context "when the master has no price" do | ||
let(:product_price) { nil } | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
|
||
context "when currency is default" do | ||
context "when variant is more than master" do | ||
let(:variant_price) { 15 } | ||
|
||
it { is_expected.to eq("(Add: $5.00)") } | ||
# Regression test for #2737 | ||
it { is_expected.to be_html_safe } | ||
end | ||
|
||
context "when variant is less than master" do | ||
let(:product_price) { 15 } | ||
|
||
it { is_expected.to eq("(Subtract: $5.00)") } | ||
end | ||
end | ||
|
||
context "when currency is JPY" do | ||
let(:variant_price) { 100 } | ||
let(:product_price) { 100 } | ||
let(:currency) { 'JPY' } | ||
|
||
context "when variant is more than master" do | ||
let(:variant_price) { 150 } | ||
|
||
it { is_expected.to eq("(Add: ¥50)") } | ||
end | ||
|
||
context "when variant is less than master" do | ||
let(:product_price) { 150 } | ||
|
||
it { is_expected.to eq("(Subtract: ¥50)") } | ||
end | ||
end | ||
end | ||
|
||
context "#variant_price_full" do | ||
before do | ||
Spree::Config[:show_variant_full_price] = true | ||
@variant1 = create(:variant, :product => product) | ||
@variant2 = create(:variant, :product => product) | ||
end | ||
|
||
context "when currency is default" do | ||
it "should return the variant price if the price is different than master" do | ||
product.price = 10 | ||
@variant1.price = 15 | ||
@variant2.price = 20 | ||
expect(helper.variant_price(@variant1)).to eq("$15.00") | ||
expect(helper.variant_price(@variant2)).to eq("$20.00") | ||
end | ||
end | ||
|
||
context "when currency is JPY" do | ||
let(:currency) { 'JPY' } | ||
|
||
before do | ||
product.variants.active.each do |variant| | ||
variant.prices.each do |price| | ||
price.currency = currency | ||
price.save! | ||
end | ||
end | ||
end | ||
|
||
it "should return the variant price if the price is different than master" do | ||
product.price = 100 | ||
@variant1.price = 150 | ||
expect(helper.variant_price(@variant1)).to eq("¥150") | ||
end | ||
end | ||
|
||
it "should be nil when all variant prices are equal" do | ||
product.price = 10 | ||
@variant1.default_price.update_column(:amount, 10) | ||
@variant2.default_price.update_column(:amount, 10) | ||
expect(helper.variant_price(@variant1)).to be_nil | ||
expect(helper.variant_price(@variant2)).to be_nil | ||
end | ||
end | ||
|
||
|
||
context "#product_description" do | ||
# Regression test for #1607 | ||
it "renders a product description without excessive paragraph breaks" do | ||
product.description = %Q{ | ||
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus a ligula leo. Proin eu arcu at ipsum dapibus ullamcorper. Pellentesque egestas orci nec magna condimentum luctus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Ut ac ante et mauris bibendum ultricies non sed massa. Fusce facilisis dui eget lacus scelerisque eget aliquam urna ultricies. Duis et rhoncus quam. Praesent tellus nisi, ultrices sed iaculis quis, euismod interdum ipsum.</p> | ||
<ul> | ||
<li>Lorem ipsum dolor sit amet</li> | ||
<li>Lorem ipsum dolor sit amet</li> | ||
</ul> | ||
} | ||
description = product_description(product) | ||
expect(description.strip).to eq(product.description.strip) | ||
end | ||
|
||
it "renders a product description with automatic paragraph breaks" do | ||
product.description = %Q{ | ||
THIS IS THE BEST PRODUCT EVER! | ||
"IT CHANGED MY LIFE" - Sue, MD} | ||
|
||
description = product_description(product) | ||
expect(description.strip).to eq(%Q{<p>\nTHIS IS THE BEST PRODUCT EVER!</p>"IT CHANGED MY LIFE" - Sue, MD}) | ||
end | ||
|
||
it "renders a product description without any formatting based on configuration" do | ||
initialDescription = %Q{ | ||
<p>hello world</p> | ||
<p>tihs is completely awesome and it works</p> | ||
<p>why so many spaces in the code. and why some more formatting afterwards?</p> | ||
} | ||
|
||
product.description = initialDescription | ||
|
||
Spree::Config[:show_raw_product_description] = true | ||
description = product_description(product) | ||
expect(description).to eq(initialDescription) | ||
end | ||
|
||
end | ||
|
||
shared_examples_for "line item descriptions" do | ||
context 'variant has a blank description' do | ||
let(:description) { nil } | ||
it { is_expected.to eq(Spree.t(:product_has_no_description)) } | ||
end | ||
context 'variant has a description' do | ||
let(:description) { 'test_desc' } | ||
it { is_expected.to eq(description) } | ||
end | ||
context 'description has nonbreaking spaces' do | ||
let(:description) { 'test desc' } | ||
it { is_expected.to eq('test desc') } | ||
end | ||
end | ||
|
||
context "#line_item_description" do | ||
let(:variant) { create(:variant, :product => product, description: description) } | ||
subject { line_item_description_text(variant.product.description) } | ||
|
||
it_should_behave_like "line item descriptions" | ||
end | ||
|
||
context '#line_item_description_text' do | ||
subject { line_item_description_text description } | ||
|
||
it_should_behave_like "line item descriptions" | ||
end | ||
|
||
context '#cache_key_for_products' do | ||
subject { helper.cache_key_for_products } | ||
before(:each) do | ||
@products = double('products collection') | ||
allow(helper).to receive(:params) { {:page => 10} } | ||
end | ||
|
||
context 'when there is a maximum updated date' do | ||
let(:updated_at) { Date.new(2011, 12, 13) } | ||
before :each do | ||
allow(@products).to receive(:count) { 5 } | ||
allow(@products).to receive(:maximum).with(:updated_at) { updated_at } | ||
end | ||
|
||
it { is_expected.to eq('en/USD/spree/products/all-10-20111213-5') } | ||
end | ||
|
||
context 'when there is no considered maximum updated date' do | ||
let(:today) { Date.new(2013, 12, 11) } | ||
before :each do | ||
allow(@products).to receive(:count) { 1234567 } | ||
allow(@products).to receive(:maximum).with(:updated_at) { nil } | ||
allow(Date).to receive(:today) { today } | ||
end | ||
|
||
it { is_expected.to eq('en/USD/spree/products/all-10-20131211-1234567') } | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.