ar_loader 0.0.6 → 0.0.8

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.
Files changed (53) hide show
  1. data/LICENSE +9 -9
  2. data/README.markdown +268 -221
  3. data/Rakefile +76 -76
  4. data/lib/VERSION +1 -1
  5. data/lib/ar_loader.rb +87 -66
  6. data/lib/ar_loader/exceptions.rb +2 -0
  7. data/lib/{engine → ar_loader}/file_definitions.rb +353 -353
  8. data/lib/{engine → ar_loader}/mapping_file_definitions.rb +87 -87
  9. data/lib/ar_loader/method_detail.rb +257 -0
  10. data/lib/ar_loader/method_mapper.rb +213 -0
  11. data/lib/helpers/jruby/jexcel_file.rb +187 -0
  12. data/lib/{engine → helpers/jruby}/word.rb +79 -70
  13. data/lib/helpers/spree_helper.rb +85 -0
  14. data/lib/loaders/csv_loader.rb +87 -0
  15. data/lib/loaders/excel_loader.rb +132 -0
  16. data/lib/loaders/loader_base.rb +205 -73
  17. data/lib/loaders/spree/image_loader.rb +45 -41
  18. data/lib/loaders/spree/product_loader.rb +140 -91
  19. data/lib/to_b.rb +24 -24
  20. data/spec/csv_loader_spec.rb +27 -0
  21. data/spec/database.yml +19 -6
  22. data/spec/db/migrate/20110803201325_create_test_bed.rb +78 -0
  23. data/spec/excel_loader_spec.rb +113 -98
  24. data/spec/fixtures/BadAssociationName.xls +0 -0
  25. data/spec/fixtures/DemoNegativeTesting.xls +0 -0
  26. data/spec/fixtures/DemoTestModelAssoc.xls +0 -0
  27. data/spec/fixtures/ProjectsMultiCategories.xls +0 -0
  28. data/spec/fixtures/SimpleProjects.xls +0 -0
  29. data/spec/fixtures/SpreeProducts.xls +0 -0
  30. data/spec/fixtures/SpreeZoneExample.csv +5 -0
  31. data/spec/fixtures/SpreeZoneExample.xls +0 -0
  32. data/spec/loader_spec.rb +116 -0
  33. data/spec/logs/test.log +5000 -0
  34. data/spec/method_mapper_spec.rb +222 -0
  35. data/spec/models.rb +55 -0
  36. data/spec/spec_helper.rb +85 -18
  37. data/spec/spree_loader_spec.rb +223 -157
  38. data/tasks/config/seed_fu_product_template.erb +15 -15
  39. data/tasks/config/tidy_config.txt +12 -12
  40. data/tasks/db_tasks.rake +64 -64
  41. data/tasks/excel_loader.rake +63 -113
  42. data/tasks/file_tasks.rake +36 -37
  43. data/tasks/loader.rake +45 -0
  44. data/tasks/spree/image_load.rake +108 -107
  45. data/tasks/spree/product_loader.rake +49 -107
  46. data/tasks/word_to_seedfu.rake +166 -166
  47. metadata +66 -61
  48. data/lib/engine/jruby/jexcel_file.rb +0 -182
  49. data/lib/engine/jruby/method_mapper_excel.rb +0 -44
  50. data/lib/engine/method_detail.rb +0 -140
  51. data/lib/engine/method_mapper.rb +0 -157
  52. data/lib/engine/method_mapper_csv.rb +0 -28
  53. data/spec/db/migrate/20110803201325_create_testbed.rb +0 -25
@@ -1,157 +0,0 @@
1
- # Copyright:: (c) Autotelik Media Ltd 2011
2
- # Author :: Tom Statter
3
- # Date :: Aug 2010
4
- # License:: MIT
5
- #
6
- # Details:: A base class that stores details of all possible associations on AR classes and,
7
- # given user supplied class and name, attempts to find correct attribute/association.
8
- #
9
- # Derived classes define where the user supplied list of names originates from.
10
- #
11
- # Example usage, load from a spreadsheet where the column names are only
12
- # an approximation of the actual associations. Given a column heading of
13
- # 'Product Properties' on class Product, find_method_detail() would search AR model,
14
- # and return details of real has_many association 'product_properties'.
15
- #
16
- # This real association can then be used to send spreadsheet row data to the AR object.
17
- #
18
- require 'method_detail'
19
-
20
- class MethodMapper
21
-
22
- attr_accessor :header_row, :headers
23
- attr_accessor :methods
24
-
25
- @@has_many = Hash.new
26
- @@belongs_to = Hash.new
27
- @@assignments = Hash.new
28
- @@column_types = Hash.new
29
-
30
- def initialize
31
- @methods = []
32
- @headers = []
33
- end
34
-
35
- # Build complete picture of the methods whose names listed in method_list
36
- # Handles method names as defined by a user or in file headers where names may
37
- # not be exactly as required e.g handles capitalisation, white space, _ etc
38
-
39
- def find_method_details( klass, method_list )
40
- @methods = method_list.collect { |x| MethodMapper::find_method_detail( klass, x ) }
41
- end
42
-
43
- def method_names()
44
- @methods.collect( &:name )
45
- end
46
-
47
- def check_mandatory( mandatory_list )
48
- method_list = method_names()
49
-
50
- mandatory_list.each { |x| raise "Mandatory column missing - need a '#{x}' column" unless(method_list.index(x)) }
51
- end
52
-
53
- # Create picture of the operators for assignment available on an AR model,
54
- # including via associations (which provide both << and = )
55
- #
56
- def self.find_operators(klass, options = {} )
57
-
58
- if( options[:reload] || @@has_many[klass].nil? )
59
- @@has_many[klass] = klass.reflect_on_all_associations(:has_many).map { |i| i.name.to_s }
60
- klass.reflect_on_all_associations(:has_and_belongs_to_many).inject(@@has_many[klass]) { |x,i| x << i.name.to_s }
61
- end
62
-
63
- # puts "DEBUG: Has Many Associations:", @@has_many[klass].inspect
64
-
65
- if( options[:reload] || @@belongs_to[klass].nil? )
66
- @@belongs_to[klass] = klass.reflect_on_all_associations(:belongs_to).map { |i| i.name.to_s }
67
- end
68
-
69
- # puts "DEBUG: Belongs To Associations:", @@belongs_to[klass].inspect
70
-
71
- if( options[:reload] || @@assignments[klass].nil? )
72
- @@assignments[klass] = (klass.column_names + klass.instance_methods.grep(/=/).map{|i| i.gsub(/=/, '')})
73
- @@assignments[klass] = @@assignments[klass] - @@has_many[klass] if(@@has_many[klass])
74
- @@assignments[klass] = @@assignments[klass] - @@belongs_to[klass] if(@@belongs_to[klass])
75
-
76
- @@assignments[klass].uniq!
77
-
78
- @@assignments[klass].each do |assign|
79
- found = klass.columns.find{ |col| col.name == assign }
80
- @@column_types[column_key(klass, assign)] = found if found
81
- end
82
- end
83
- end
84
-
85
- # Find the proper format of name, appropriate call + column type for a given name.
86
- # e.g Given users entry in spread sheet check for pluralization, missing underscores etc
87
- #
88
- # If not nil returned method can be used directly in for example klass.new.send( call, .... )
89
- #
90
- def self.find_method_detail( klass, name )
91
- true_name, assign, belongs_to, has_many = nil, nil, nil, nil
92
-
93
- # TODO - check out regexp to do this work better plus Inflections ??
94
- [
95
- name,
96
- name.gsub(' ', '_'),
97
- name.gsub(' ', ''),
98
- name.gsub(' ', '_').downcase,
99
- name.gsub(' ', '').downcase,
100
- name.gsub(' ', '_').underscore
101
-
102
- ].each do |n|
103
- has_many = (@@has_many[klass] && @@has_many[klass].include?(n)) ? n : nil
104
- belongs_to = (@@belongs_to[klass] && @@belongs_to[klass].include?(n)) ? n : nil
105
- assign = (@@assignments[klass] && @@assignments[klass].include?(n))? n + '=' : nil
106
-
107
- if(assign || has_many || belongs_to)
108
- true_name = n
109
- break
110
- end
111
- end
112
-
113
- return MethodDetail.new(klass, true_name, assign, belongs_to, has_many, @@column_types[column_key(klass, true_name)])
114
- end
115
-
116
- def self.clear
117
- @@has_many.clear
118
- @@assignments.clear
119
- @@column_types.clear
120
- end
121
-
122
- def self.column_key(klass, column)
123
- "#{klass.name}:#{column}"
124
- end
125
-
126
- def self.has_many
127
- @@has_many
128
- end
129
- def self.assignments
130
- @@assignments
131
- end
132
- def self.column_types
133
- @@column_types
134
- end
135
-
136
- def self.has_many_for(klass)
137
- @@has_many[klass]
138
- end
139
- def self.assignments_for(klass)
140
- @@assignments[klass]
141
- end
142
-
143
- def self.column_type_for(klass, column)
144
- @@column_types[column_key(klass, column)]
145
- end
146
-
147
-
148
- def find_or_new( klass, condition_hash = {} )
149
- @records[klass] = klass.find(:all, :conditions => condition_hash)
150
- if @records[klass].any?
151
- return @records[klass].first
152
- else
153
- return klass.new
154
- end
155
- end
156
-
157
- end
@@ -1,28 +0,0 @@
1
- # Copyright:: (c) Autotelik Media Ltd 2011
2
- # Author :: Tom Statter
3
- # Date :: Jan 2011
4
- # License:: MIT
5
- #
6
- # Details:: Extract the headings from a user supplied CSV file, and map heading names
7
- # to the attributes and/or assocaiitons of an AR Model defined by supplied klass.
8
- #
9
- require 'method_mapper'
10
-
11
- class MethodMapperCsv < MethodMapper
12
-
13
- # Read the headers from CSV file and map to ActiveRecord members/associations
14
-
15
- def initialize( file_name, klass, sheet_number = 0 )
16
- super
17
-
18
- File.open(file_name) do
19
- @headers = @header_row.split(/,/)
20
- end
21
-
22
- # Gather list of all possible 'setter' methods on AR class (instance variables and associations)
23
- self.find_operators( klass )
24
-
25
- # Convert the list of headers into suitable calls on the Active Record class
26
- find_method_details( klass, @headers )
27
- end
28
- end
@@ -1,25 +0,0 @@
1
- class CreateTestBed < ActiveRecord::Migration
2
-
3
- def self.up
4
-
5
- create_table :test_model do |t|
6
- t.string :value_as_string
7
- t.text :value_as_text
8
- t.boolean :value_as_boolean, :default => false
9
- t.datetime :value_as_datetime, :default => nil
10
- t.timestamps
11
- end
12
-
13
- create_table :test_association_model do |t|
14
- t.string :value_as_string
15
- t.references :test_model
16
- t.timestamps
17
- end
18
-
19
- end
20
-
21
- def self.down
22
- drop_table :test_model
23
- drop_table :test_association_model
24
- end
25
- end