From 3ad575cb5fb498c4fe951da04c641fb47a8c8acf Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 26 Jun 2012 09:54:00 +1000 Subject: [PATCH] Display variant price differences as absolute, not relative values --- .../spree/products_helper_decorator.rb | 9 +++++ spec/helpers/products_helper_spec.rb | 35 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 app/helpers/spree/products_helper_decorator.rb create mode 100644 spec/helpers/products_helper_spec.rb diff --git a/app/helpers/spree/products_helper_decorator.rb b/app/helpers/spree/products_helper_decorator.rb new file mode 100644 index 0000000000..7c17b71a36 --- /dev/null +++ b/app/helpers/spree/products_helper_decorator.rb @@ -0,0 +1,9 @@ +module Spree + ProductsHelper.class_eval do + # Return the price of the variant, or nil if it is identical to the master price + def variant_price_diff(variant) + return nil if variant.price == variant.product.price + "(#{number_to_currency variant.price})" + end + end +end diff --git a/spec/helpers/products_helper_spec.rb b/spec/helpers/products_helper_spec.rb new file mode 100644 index 0000000000..bc55cc208d --- /dev/null +++ b/spec/helpers/products_helper_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +module Spree + describe ProductsHelper do + subject do + obj = Object.new + obj.extend(ProductsHelper) + obj.extend(ActionView::Helpers::NumberHelper) + end + + + it "displays variant price differences as absolute, not relative values" do + variant = make_variant_stub(10.00, 10.00) + subject.variant_price_diff(variant).should be_nil + + variant = make_variant_stub(10.00, 15.55) + subject.variant_price_diff(variant).should == "($15.55)" + + variant = make_variant_stub(10.00, 5.55) + subject.variant_price_diff(variant).should == "($5.55)" + end + + private + def make_variant_stub(product_price, variant_price) + product = stub(:product) + product.stub(:price).and_return(product_price) + + variant = stub(:variant) + variant.stub(:product).and_return(product) + variant.stub(:price).and_return(variant_price) + + variant + end + end +end