Move option value naming logic into separate lib class

This commit is contained in:
Rob H
2014-06-04 14:54:42 +10:00
committed by Will Marshall
parent c21342b82e
commit e9f2a8f2cd
4 changed files with 200 additions and 182 deletions

View File

@@ -1,3 +1,5 @@
require 'open_food_network/option_value_namer'
Spree::Variant.class_eval do
has_many :exchange_variants, dependent: :destroy
has_many :exchanges, through: :exchange_variants
@@ -54,67 +56,11 @@ Spree::Variant.class_eval do
delete_unit_option_values
option_type = self.product.variant_unit_option_type
option_value_namer = OpenFoodNetwork::OptionValueNamer.new self
if option_type
name = option_value_name
name = option_value_namer.name
ov = Spree::OptionValue.where(option_type_id: option_type, name: name, presentation: name).first || Spree::OptionValue.create!({option_type: option_type, name: name, presentation: name}, without_protection: true)
option_values << ov
end
end
def option_value_name
value, unit = option_value_value_unit
separator = value_scaled? ? '' : ' '
name_fields = []
name_fields << "#{value}#{separator}#{unit}" if value.present? && unit.present?
name_fields << unit_description if unit_description.present?
name_fields.join ' '
end
def value_scaled?
self.product.variant_unit_scale.present?
end
def option_value_value_unit
if unit_value.present?
if %w(weight volume).include? self.product.variant_unit
value, unit_name = option_value_value_unit_scaled
else
value = unit_value
unit_name = self.product.variant_unit_name
unit_name = unit_name.pluralize if value > 1
end
value = value.to_i if value == value.to_i
else
value = unit_name = nil
end
[value, unit_name]
end
def option_value_value_unit_scaled
unit_scale, unit_name = scale_for_unit_value
value = unit_value / unit_scale
[value, unit_name]
end
def scale_for_unit_value
units = {'weight' => {1.0 => 'g', 1000.0 => 'kg', 1000000.0 => 'T'},
'volume' => {0.001 => 'mL', 1.0 => 'L', 1000000.0 => 'ML'}}
# Find the largest available unit where unit_value comes to >= 1 when expressed in it.
# If there is none available where this is true, use the smallest available unit.
unit = units[self.product.variant_unit].select { |scale, unit_name|
unit_value / scale >= 1
}.to_a.last
unit = units[self.product.variant_unit].first if unit.nil?
unit
end
end

View File

@@ -0,0 +1,59 @@
module OpenFoodNetwork
class OptionValueNamer < Struct.new(:variant)
def name
value, unit = self.option_value_value_unit
separator = self.value_scaled? ? '' : ' '
name_fields = []
name_fields << "#{value}#{separator}#{unit}" if value.present? && unit.present?
name_fields << variant.unit_description if variant.unit_description.present?
name_fields.join ' '
end
def value_scaled?
variant.product.variant_unit_scale.present?
end
def option_value_value_unit
if variant.unit_value.present?
if %w(weight volume).include? variant.product.variant_unit
value, unit_name = self.option_value_value_unit_scaled
else
value = variant.unit_value
unit_name = variant.product.variant_unit_name
unit_name = unit_name.pluralize if value > 1
end
value = value.to_i if value == value.to_i
else
value = unit_name = nil
end
[value, unit_name]
end
def option_value_value_unit_scaled
unit_scale, unit_name = self.scale_for_unit_value
value = variant.unit_value / unit_scale
[value, unit_name]
end
def scale_for_unit_value
units = {'weight' => {1.0 => 'g', 1000.0 => 'kg', 1000000.0 => 'T'},
'volume' => {0.001 => 'mL', 1.0 => 'L', 1000000.0 => 'ML'}}
# Find the largest available unit where unit_value comes to >= 1 when expressed in it.
# If there is none available where this is true, use the smallest available unit.
unit = units[variant.product.variant_unit].select { |scale, unit_name|
variant.unit_value / scale >= 1
}.to_a.last
unit = units[variant.product.variant_unit].first if unit.nil?
unit
end
end
end

View File

@@ -0,0 +1,137 @@
require 'spec_helper'
module OpenFoodNetwork
describe OptionValueNamer do
describe "generating option value name" do
it "when description is blank" do
v = Spree::Variant.new unit_description: nil
subject = OptionValueNamer.new v
subject.stub(:value_scaled?) { true }
subject.stub(:option_value_value_unit) { %w(value unit) }
subject.name.should == "valueunit"
end
it "when description is present" do
v = Spree::Variant.new unit_description: 'desc'
subject = OptionValueNamer.new v
subject.stub(:option_value_value_unit) { %w(value unit) }
subject.stub(:value_scaled?) { true }
subject.name.should == "valueunit desc"
end
it "when value is blank and description is present" do
v = Spree::Variant.new unit_description: 'desc'
subject = OptionValueNamer.new v
subject.stub(:option_value_value_unit) { [nil, nil] }
subject.stub(:value_scaled?) { true }
subject.name.should == "desc"
end
it "spaces value and unit when value is unscaled" do
v = Spree::Variant.new unit_description: nil
subject = OptionValueNamer.new v
subject.stub(:option_value_value_unit) { %w(value unit) }
subject.stub(:value_scaled?) { false }
subject.name.should == "value unit"
end
end
describe "determining if a variant's value is scaled" do
it "returns true when the product has a scale" do
p = Spree::Product.new variant_unit_scale: 1000
v = Spree::Variant.new
v.stub(:product) { p }
subject = OptionValueNamer.new v
subject.value_scaled?.should be_true
end
it "returns false otherwise" do
p = Spree::Product.new
v = Spree::Variant.new
v.stub(:product) { p }
subject = OptionValueNamer.new v
subject.value_scaled?.should be_false
end
end
describe "generating option value's value and unit" do
let(:v) { Spree::Variant.new }
let(:subject) { OptionValueNamer.new v }
it "generates simple values" do
p = double(:product, variant_unit: 'weight', variant_unit_scale: 1.0)
v.stub(:product) { p }
v.stub(:unit_value) { 100 }
subject.option_value_value_unit.should == [100, 'g']
end
it "generates values when unit value is non-integer" do
p = double(:product, variant_unit: 'weight', variant_unit_scale: 1.0)
v.stub(:product) { p }
v.stub(:unit_value) { 123.45 }
subject.option_value_value_unit.should == [123.45, 'g']
end
it "returns a value of 1 when unit value equals the scale" do
p = double(:product, variant_unit: 'weight', variant_unit_scale: 1000.0)
v.stub(:product) { p }
v.stub(:unit_value) { 1000.0 }
subject.option_value_value_unit.should == [1, 'kg']
end
it "generates values for all weight scales" do
[[1.0, 'g'], [1000.0, 'kg'], [1000000.0, 'T']].each do |scale, unit|
p = double(:product, variant_unit: 'weight', variant_unit_scale: scale)
v.stub(:product) { p }
v.stub(:unit_value) { 100 * scale }
subject.option_value_value_unit.should == [100, unit]
end
end
it "generates values for all volume scales" do
[[0.001, 'mL'], [1.0, 'L'], [1000000.0, 'ML']].each do |scale, unit|
p = double(:product, variant_unit: 'volume', variant_unit_scale: scale)
v.stub(:product) { p }
v.stub(:unit_value) { 100 * scale }
subject.option_value_value_unit.should == [100, unit]
end
end
it "chooses the correct scale when value is very small" do
p = double(:product, variant_unit: 'volume', variant_unit_scale: 0.001)
v.stub(:product) { p }
v.stub(:unit_value) { 0.0001 }
subject.option_value_value_unit.should == [0.1, 'mL']
end
it "generates values for item units" do
%w(packet box).each do |unit|
p = double(:product, variant_unit: 'items', variant_unit_scale: nil, variant_unit_name: unit)
v.stub(:product) { p }
v.stub(:unit_value) { 100 }
subject.option_value_value_unit.should == [100, unit.pluralize]
end
end
it "generates singular values for item units when value is 1" do
p = double(:product, variant_unit: 'items', variant_unit_scale: nil, variant_unit_name: 'packet')
v.stub(:product) { p }
v.stub(:unit_value) { 1 }
subject.option_value_value_unit.should == [1, 'packet']
end
it "returns [nil, nil] when unit value is not set" do
p = double(:product, variant_unit: 'items', variant_unit_scale: nil, variant_unit_name: 'foo')
v.stub(:product) { p }
v.stub(:unit_value) { nil }
subject.option_value_value_unit.should == [nil, nil]
end
end
end
end

View File

@@ -228,130 +228,6 @@ module Spree
end
end
end
describe "generating option value name" do
it "when description is blank" do
v = Spree::Variant.new unit_description: nil
v.stub(:option_value_value_unit) { %w(value unit) }
v.stub(:value_scaled?) { true }
v.send(:option_value_name).should == "valueunit"
end
it "when description is present" do
v = Spree::Variant.new unit_description: 'desc'
v.stub(:option_value_value_unit) { %w(value unit) }
v.stub(:value_scaled?) { true }
v.send(:option_value_name).should == "valueunit desc"
end
it "when value is blank and description is present" do
v = Spree::Variant.new unit_description: 'desc'
v.stub(:option_value_value_unit) { [nil, nil] }
v.stub(:value_scaled?) { true }
v.send(:option_value_name).should == "desc"
end
it "spaces value and unit when value is unscaled" do
v = Spree::Variant.new unit_description: nil
v.stub(:option_value_value_unit) { %w(value unit) }
v.stub(:value_scaled?) { false }
v.send(:option_value_name).should == "value unit"
end
end
describe "determining if a variant's value is scaled" do
it "returns true when the product has a scale" do
p = Spree::Product.new variant_unit_scale: 1000
v = Spree::Variant.new
v.stub(:product) { p }
v.send(:value_scaled?).should be_true
end
it "returns false otherwise" do
p = Spree::Product.new
v = Spree::Variant.new
v.stub(:product) { p }
v.send(:value_scaled?).should be_false
end
end
describe "generating option value's value and unit" do
let(:v) { Spree::Variant.new }
it "generates simple values" do
p = double(:product, variant_unit: 'weight', variant_unit_scale: 1.0)
v.stub(:product) { p }
v.stub(:unit_value) { 100 }
v.send(:option_value_value_unit).should == [100, 'g']
end
it "generates values when unit value is non-integer" do
p = double(:product, variant_unit: 'weight', variant_unit_scale: 1.0)
v.stub(:product) { p }
v.stub(:unit_value) { 123.45 }
v.send(:option_value_value_unit).should == [123.45, 'g']
end
it "returns a value of 1 when unit value equals the scale" do
p = double(:product, variant_unit: 'weight', variant_unit_scale: 1000.0)
v.stub(:product) { p }
v.stub(:unit_value) { 1000.0 }
v.send(:option_value_value_unit).should == [1, 'kg']
end
it "generates values for all weight scales" do
[[1.0, 'g'], [1000.0, 'kg'], [1000000.0, 'T']].each do |scale, unit|
p = double(:product, variant_unit: 'weight', variant_unit_scale: scale)
v.stub(:product) { p }
v.stub(:unit_value) { 100 * scale }
v.send(:option_value_value_unit).should == [100, unit]
end
end
it "generates values for all volume scales" do
[[0.001, 'mL'], [1.0, 'L'], [1000000.0, 'ML']].each do |scale, unit|
p = double(:product, variant_unit: 'volume', variant_unit_scale: scale)
v.stub(:product) { p }
v.stub(:unit_value) { 100 * scale }
v.send(:option_value_value_unit).should == [100, unit]
end
end
it "chooses the correct scale when value is very small" do
p = double(:product, variant_unit: 'volume', variant_unit_scale: 0.001)
v.stub(:product) { p }
v.stub(:unit_value) { 0.0001 }
v.send(:option_value_value_unit).should == [0.1, 'mL']
end
it "generates values for item units" do
%w(packet box).each do |unit|
p = double(:product, variant_unit: 'items', variant_unit_scale: nil, variant_unit_name: unit)
v.stub(:product) { p }
v.stub(:unit_value) { 100 }
v.send(:option_value_value_unit).should == [100, unit.pluralize]
end
end
it "generates singular values for item units when value is 1" do
p = double(:product, variant_unit: 'items', variant_unit_scale: nil, variant_unit_name: 'packet')
v.stub(:product) { p }
v.stub(:unit_value) { 1 }
v.send(:option_value_value_unit).should == [1, 'packet']
end
it "returns [nil, nil] when unit value is not set" do
p = double(:product, variant_unit: 'items', variant_unit_scale: nil, variant_unit_name: 'foo')
v.stub(:product) { p }
v.stub(:unit_value) { nil }
v.send(:option_value_value_unit).should == [nil, nil]
end
end
end
describe "deleting unit option values" do