datashift 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.2
data/datashift.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "datashift"
8
- s.version = "0.4.0"
8
+ s.version = "0.4.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Thomas Statter"]
12
- s.date = "2012-03-12"
12
+ s.date = "2012-03-31"
13
13
  s.description = "A suite of tools to move data between ActiveRecord models,databases,applications like Excel/Open Office, files and projects including Spree"
14
14
  s.email = "rubygems@autotelik.co.uk"
15
15
  s.extra_rdoc_files = [
@@ -11,6 +11,7 @@
11
11
  # and assign values to AR object, without knowing anything about that receiving object.
12
12
  #
13
13
  require 'to_b'
14
+ require 'logging'
14
15
 
15
16
  module DataShift
16
17
 
@@ -18,7 +18,7 @@ module DataShift
18
18
  def create_image(klass, image_path, viewable_record = nil, options = {})
19
19
 
20
20
  unless File.exists?(image_path) && File.readable?(image_path)
21
- logger.error("Cannot process Image : Invalid Path #{image_path}")
21
+ logger.error("Cannot process Image from #{Dir.pwd}: Invalid Path #{image_path}")
22
22
  raise "Cannot process Image : Invalid Path #{image_path}"
23
23
  end
24
24
 
@@ -35,6 +35,8 @@ module DataShift
35
35
  @@variant_klass ||= SpreeHelper::get_spree_class('Variant')
36
36
 
37
37
  raise "Failed to create Product for loading" unless @load_object
38
+
39
+ logger.debug "PRODUCT #{@load_object.inspect} MASTER: #{@load_object.master.inspect}"
38
40
  end
39
41
 
40
42
  # Based on filename call appropriate loading function
@@ -98,21 +100,31 @@ module DataShift
98
100
 
99
101
  # Spree has some stock management stuff going on, so dont usually assign to column vut use
100
102
  # on_hand and on_hand=
101
- if(@load_object.variants.size > 0 && current_value.to_s.include?(LoaderBase::multi_assoc_delim))
103
+ if(@load_object.variants.size > 0)
104
+
105
+ if(current_value.to_s.include?(LoaderBase::multi_assoc_delim))
102
106
 
103
- #puts "DEBUG: COUNT_ON_HAND PER VARIANT",current_value.is_a?(String),
107
+ #puts "DEBUG: COUNT_ON_HAND PER VARIANT",current_value.is_a?(String),
104
108
 
105
- # Check if we processed Option Types and assign count per option
106
- values = current_value.to_s.split(LoaderBase::multi_assoc_delim)
109
+ # Check if we processed Option Types and assign count per option
110
+ values = current_value.to_s.split(LoaderBase::multi_assoc_delim)
107
111
 
108
- if(@load_object.variants.size == values.size)
109
- @load_object.variants.each_with_index {|v, i| v.on_hand = values[i]; v.save; }
112
+ if(@load_object.variants.size == values.size)
113
+ @load_object.variants.each_with_index {|v, i| v.on_hand = values[i].to_i; v.save; }
114
+ else
115
+ puts "WARNING: Count on hand entries did not match number of Variants - None Set"
116
+ end
117
+ end
118
+
119
+ # can only set count on hand on Product if no Variants exist, else model throws
120
+
121
+ elsif(@load_object.variants.size == 0)
122
+ if(current_value.to_s.include?(LoaderBase::multi_assoc_delim))
123
+ puts "WARNING: Multiple count_on_hand values specified but no Variants/OptionTypes created"
124
+ load_object.on_hand = current_value.to_s.split(LoaderBase::multi_assoc_delim).first.to_i
110
125
  else
111
- puts "WARNING: Count on hand entries did not match number of Variants - None Set"
126
+ load_object.on_hand = current_value.to_i
112
127
  end
113
- else
114
- puts "WARNING: Multiple count_on_hand values specified but no Variants/OptionTypes created" if(@load_object.variants.empty?)
115
- load_object.on_hand = current_value.to_i
116
128
  end
117
129
 
118
130
  else
@@ -153,6 +165,7 @@ module DataShift
153
165
  puts "WARNING: OptionType #{oname} NOT found - Not set Product"
154
166
  next
155
167
  end
168
+ puts "Created missing OptionType #{option_type.inspect}"
156
169
  end
157
170
 
158
171
  @load_object.option_types << option_type unless @load_object.option_types.include?(option_type)
@@ -167,7 +180,7 @@ module DataShift
167
180
  # populated .. currently need to call reload to ensure this (seems reqd for Spree 1/Rails 3, wasn't required b4
168
181
  ovalues.each_with_index do |ovname, i|
169
182
  ovname.strip!
170
- ov = @@option_value_klass.find_or_create_by_name(ovname)
183
+ ov = @@option_value_klass.find_or_create_by_name_and_option_type_id(ovname, option_type.id)
171
184
  if ov
172
185
  variant = @@variant_klass.create( :product => @load_object, :sku => "#{@load_object.sku}_#{i}", :price => @load_object.price, :available_on => @load_object.available_on)
173
186
  #puts "DEBUG: Created New Variant: #{variant.inspect}"
@@ -22,10 +22,19 @@ module Datashift
22
22
 
23
23
  require File.expand_path('config/environment.rb')
24
24
 
25
- %w{Image OptionType OptionValue Product Property ProductProperty Variant Taxonomy Taxon Zone}.each do |k|
25
+ %w{Image OptionType OptionValue Product Property ProductGroup ProductProperty ProductOptionType Variant Taxonomy Taxon Zone}.each do |k|
26
26
  instance_variable_set("@#{k}_klass", DataShift::SpreeHelper::get_spree_class(k))
27
+ puts "Clearing model #{DataShift::SpreeHelper::get_spree_class(k)}"
27
28
  instance_variable_get("@#{k}_klass").delete_all
28
29
  end
30
+
31
+ if(File.exists?('public/spree/products') )
32
+ puts "Removing old Product assets from 'public/spree/products'"
33
+ FileUtils::rm_rf('public/public/spree/products')
34
+ end
35
+
36
+ FileUtils::rm_rf('MissingRecords') if(File.exists?('MissingRecords') )
37
+
29
38
  end
30
39
 
31
40
  end
@@ -45,6 +45,7 @@ module Datashift
45
45
  else
46
46
  loader.set_default_value('available_on', Time.now.to_s(:db) )
47
47
  loader.set_default_value('cost_price', 0.0 )
48
+ loader.set_default_value('price', 0.0 )
48
49
  end
49
50
 
50
51
  loader.set_prefix('sku', options[:sku_prefix] ) if(options[:sku_prefix])
@@ -82,7 +83,11 @@ module Datashift
82
83
  require 'image_loader'
83
84
 
84
85
  @image_cache = options[:input]
85
-
86
+
87
+ puts "Using Product Name for lookup" unless(options[:sku])
88
+ puts "Using SKU for lookup" if(options[:sku])
89
+
90
+
86
91
  attachment_klazz = DataShift::SpreeHelper::get_spree_class('Product' )
87
92
  sku_klazz = DataShift::SpreeHelper::get_spree_class('Variant' )
88
93
 
@@ -140,10 +145,11 @@ module Datashift
140
145
  else
141
146
  missing_records << image_name
142
147
  end
148
+
149
+ next if(options[:dummy]) # Don't actually create/upload to DB if we are doing dummy run
143
150
 
144
- # Now do actual upload to DB unless we are doing a dummy run,
145
- # or the Image must have an associated record
146
- unless(options[:dummy] == 'true' || (options[:process_when_no_assoc] && record.nil?))
151
+ # Check if Image must have an associated record
152
+ if(record || (record.nil? && options[:process_when_no_assoc]))
147
153
  image_loader.reset()
148
154
  puts "Process Image"
149
155
  image_loader.process( image_name, record )
@@ -114,14 +114,13 @@ describe 'SpreeLoader' do
114
114
  p.description.should == "blah blah"
115
115
  p.cost_price.should == 320.00
116
116
  p.option_types.should have_exactly(1).items
117
+ p.option_types.should have_exactly(1).items
117
118
  p.count_on_hand.should == 12
118
-
119
- @klass.last.option_types.should have_exactly(2).items
120
119
  @klass.last.count_on_hand.should == 23
121
120
  end
122
121
 
123
122
 
124
- it "should support default values for Spree Products loader", :fail => true do
123
+ it "should support default values for Spree Products loader" do
125
124
 
126
125
  @expected_time = Time.now.to_s(:db)
127
126
 
@@ -137,7 +136,7 @@ describe 'SpreeLoader' do
137
136
 
138
137
  end
139
138
 
140
- it "should support default values from config for Spree Products loader", :fail => true do
139
+ it "should support default values from config for Spree Products loader" do
141
140
 
142
141
  @product_loader.configure_from( SpecHelper::spree_fixture('SpreeProductsDefaults.yml') )
143
142
 
@@ -171,12 +170,12 @@ describe 'SpreeLoader' do
171
170
  # Operation and results should be identical when loading multiple associations
172
171
  # if using either single column embedded syntax, or one column per entry.
173
172
 
174
- it "should load Products and create Variants from single column", :fail => true do
173
+ it "should load Products and create Variants from single column" do
175
174
  test_variants_creation('SpreeProducts.xls')
176
175
  end
177
176
 
178
177
 
179
- it "should load Products and create Variants from multiple column" do
178
+ it "should load Products and create Variants from multiple column", :fail => true do
180
179
  test_variants_creation('SpreeProductsMultiColumn.xls')
181
180
  end
182
181
 
@@ -207,20 +206,45 @@ describe 'SpreeLoader' do
207
206
 
208
207
  @klass.all.select {|m| m.is_master.should == true }
209
208
 
210
- p.variants.should have_exactly(3).items # count => 12|6|7
211
-
209
+
210
+ # mime_type:jpeg mime_type:PDF mime_type:PNG
211
+
212
+ p.variants.should have_exactly(3).items
213
+
214
+ p.option_types.should have_exactly(1).items # mime_type
215
+
216
+ p.option_types[0].name.should == "mime_type"
217
+ p.option_types[0].presentation.should == "Mime type"
218
+
212
219
  @Variant_klass.all[1].sku.should == "DEMO_001_0"
213
220
  @Variant_klass.all[1].price.should == 399.99
214
221
 
215
- v = p.variants[0]
222
+ # V1
223
+ v1 = p.variants[0]
216
224
 
217
- v.sku.should == "DEMO_001_0"
218
- v.price.should == 399.99
219
- v.count_on_hand.should == 12
225
+ v1.sku.should == "DEMO_001_0"
226
+ v1.price.should == 399.99
227
+ v1.count_on_hand.should == 12
220
228
 
221
- p.variants[1].count_on_hand.should == 6
222
- p.variants[2].count_on_hand.should == 7
229
+
230
+ v1.option_values.should have_exactly(1).items # mime_type: jpeg
231
+ v1.option_values[0].name.should == "jpeg"
223
232
 
233
+
234
+ v2 = p.variants[1]
235
+ v2.count_on_hand.should == 6
236
+ v2.option_values.should have_exactly(1).items # mime_type: jpeg
237
+ v2.option_values[0].name.should == "PDF"
238
+
239
+ v2.option_values[0].option_type.should_not be_nil
240
+ v2.option_values[0].option_type.position.should == 0
241
+
242
+
243
+ v3 = p.variants[2]
244
+ v3.count_on_hand.should == 7
245
+ v3.option_values.should have_exactly(1).items # mime_type: jpeg
246
+ v3.option_values[0].name.should == "PNG"
247
+
224
248
  @Variant_klass.last.price.should == 50.34
225
249
  @Variant_klass.last.count_on_hand.should == 18
226
250
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: datashift
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.4.0
5
+ version: 0.4.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Thomas Statter
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-03-12 00:00:00 Z
13
+ date: 2012-03-31 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: A suite of tools to move data between ActiveRecord models,databases,applications like Excel/Open Office, files and projects including Spree