Permalink
Browse files

fix when creating variant option_values

  • Loading branch information...
1 parent a149438 commit ac7aeea5e6dcca468efdc1c0cec3f6b85f9a8db9 @sbounmy committed Mar 24, 2012
Showing with 65 additions and 15 deletions.
  1. +10 −0 Gemfile
  2. +4 −2 app/models/product_import.rb
  3. +0 −5 import_products.gemspec
  4. +42 −0 spec/models/product_import_spec.rb
  5. +9 −8 spec/spec_helper.rb
View
10 Gemfile
@@ -1,3 +1,13 @@
source "http://rubygems.org"
+gem 'mysql2'
+
+group :test do
+ gem 'ffaker'
+ gem 'rspec-rails'
+ gem 'launchy', '2.0.5'
+ gem 'factory_girl'
+end
+
+gem 'awesome_print'
gemspec
@@ -98,7 +98,7 @@ def create_variant_for(product, options = {:with => {}})
# Just update variant if exists
variant = Spree::Variant.find_by_sku(options[:with][:sku])
if !variant
- product.variants.new
+ variant = product.variants.new
variant.id = options[:with][:id]
else
options[:with].delete(:id)
@@ -120,10 +120,12 @@ def create_variant_for(product, options = {:with => {}})
)
if applicable_option_type.is_a?(Spree::OptionType)
product.option_types << applicable_option_type unless product.option_types.include?(applicable_option_type)
- variant.option_values << applicable_option_type.option_values.find(
+ values = applicable_option_type.option_values.find(
:all,
:conditions => ["presentation = ? OR name = ?", value, value]
)
+ values = applicable_option_type.option_values.create(:presentation => value, :name => value) if values.empty?
+ variant.option_values << values
end
end
View
@@ -21,9 +21,4 @@ Gem::Specification.new do |s|
s.add_dependency('delayed_job_active_record')
s.add_dependency 'spree_sample'
- s.add_development_dependency 'capybara', '1.0.1'
- s.add_development_dependency 'factory_girl'
- s.add_development_dependency 'ffaker'
- s.add_development_dependency 'rspec-rails', '~> 2.7'
- s.add_development_dependency 'sqlite3'
end
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+describe ProductImport do
+ describe "#create_variant_for" do
+ before do
+ product; size; color; option_color; option_size
+ end
+
+ let(:product) { Factory(:product, :sku => "001", :permalink => "S0388G-bloch-kids-tap-flexewe") }
+ let(:size) { Factory(:option_type, :name => "tshirt-size") }
+ let(:color) { Factory(:option_type, :name => "tshirt-color", :presentation => "Color") }
+ let(:option_color) { Factory(:option_value, :name => "blue", :presentation => "Blue", :option_type => color) }
+ let(:option_size) { Factory(:option_value, :name => "s", :presentation => "Small", :option_type => size) }
+
+ let(:params) do
+ {:sku=>"002", :name=>"S0388G Bloch Kids Tap Flexww", :description=>"Lace Up Split Sole Leather Tap Shoe",
+ :cost_price=>"29.25", :master_price=>"54.46", :available_on=>"1/1/10", :"tshirt-color"=>"Blue", :"tshirt-size"=>"Small",
+ :on_hand=>"2", :height=>"3", :width=>"4", :depth=>"9", :weight=>"1", :position=>"0", :category=>"Categories >
+ Clothing", :permalink=>"S0388G-bloch-kids-tap-flexewe"}
+ end
+
+ it "creates a new variant when product already exist" do
+ product.variants_with_only_master.count.should == 1
+ expect do
+ ProductImport.new.send(:create_variant_for, product, :with => params)
+ end.to change(product.variants, :count).by(1)
+ product.variants_with_only_master.count.should == 1
+ variant = product.variants.last
+ variant.price.to_f.should == 54.46
+ variant.cost_price.to_f.should == 29.25
+ product.option_types.should =~ [size, color]
+ variant.option_values.should =~ [option_size, option_color]
+ end
+
+ it "creates missing option_values for new variant" do
+ ProductImport.new.send(:create_variant_for, product, :with => params.merge(:"tshirt-size" => "Large", :"tshirt-color" => "Yellow"))
+ variant = product.variants.last
+ product.option_types.should =~ [size, color]
+ variant.option_values.should =~ Spree::OptionValue.where(:name => %w(Large Yellow))
+ end
+ end
+end
View
@@ -1,13 +1,16 @@
# This file is copied to ~/spec when you run 'ruby script/generate rspec'
# from the project root directory.
ENV["RAILS_ENV"] ||= 'test'
-require File.expand_path("../../../config/environment", __FILE__)
+require File.expand_path("../dummy/config/environment.rb", __FILE__)
+
require 'rspec/rails'
-require 'fabrication'
-# Requires supporting files with custom matchers and macros, etc,
-# in ./support/ and its subdirectories.
-Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
+# Requires supporting ruby files with custom matchers and macros, etc,
+# in spec/support/ and its subdirectories.
+Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each {|f| require f }
+
+# Requires factories defined in spree_core
+require 'spree/core/testing_support/factories'
RSpec.configure do |config|
# == Mock Framework
@@ -26,6 +29,4 @@
# examples within a transaction, comment the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
-end
-
-@configuration ||= AppConfiguration.find_or_create_by_name("Default configuration")
+end

0 comments on commit ac7aeea

Please sign in to comment.