datashift 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. data/README.markdown +63 -64
  2. data/Rakefile +4 -7
  3. data/VERSION +1 -1
  4. data/datashift.gemspec +92 -62
  5. data/lib/applications/apache_poi_extensions.rb +62 -0
  6. data/lib/applications/excel.rb +78 -0
  7. data/lib/applications/excel_base.rb +65 -0
  8. data/lib/applications/jexcel_file.rb +222 -0
  9. data/lib/applications/jexcel_file_extensions.rb +244 -0
  10. data/lib/applications/jruby/{jexcel_file.rb → old_pre_proxy_jexcel_file.rb} +0 -0
  11. data/lib/applications/ruby_poi_translations.rb +64 -0
  12. data/lib/applications/spreadsheet_extensions.rb +31 -0
  13. data/lib/datashift/method_details_manager.rb +4 -0
  14. data/lib/exporters/csv_exporter.rb +3 -1
  15. data/lib/exporters/excel_exporter.rb +59 -74
  16. data/lib/generators/excel_generator.rb +70 -74
  17. data/lib/guards.rb +57 -0
  18. data/lib/loaders/excel_loader.rb +105 -105
  19. data/lib/loaders/loader_base.rb +43 -21
  20. data/lib/loaders/paperclip/attachment_loader.rb +104 -0
  21. data/lib/loaders/paperclip/datashift_paperclip.rb +78 -0
  22. data/lib/loaders/paperclip/{image_loader.rb → image_loading.rb} +2 -18
  23. data/lib/thor/{generate_excel.thor → generate.thor} +48 -0
  24. data/lib/thor/paperclip.thor +85 -0
  25. data/lib/thor/tools.thor +23 -2
  26. data/spec/Gemfile +1 -7
  27. data/spec/csv_exporter_spec.rb +4 -4
  28. data/spec/csv_loader_spec.rb +1 -1
  29. data/spec/excel_exporter_spec.rb +43 -45
  30. data/spec/excel_generator_spec.rb +132 -60
  31. data/spec/excel_loader_spec.rb +134 -140
  32. data/spec/excel_spec.rb +179 -0
  33. data/spec/fixtures/ProjectsMultiCategoriesHeaderLookup.xls +0 -0
  34. data/spec/fixtures/config/database.yml +2 -2
  35. data/spec/fixtures/db/datashift_test_models_db.sqlite +0 -0
  36. data/spec/{db → fixtures/db}/migrate/20110803201325_create_test_bed.rb +0 -0
  37. data/spec/fixtures/load_datashift.thor +3 -0
  38. data/spec/fixtures/models/category.rb +7 -0
  39. data/spec/fixtures/models/empty.rb +2 -0
  40. data/spec/fixtures/models/loader_release.rb +10 -0
  41. data/spec/fixtures/models/long_and_complex_table_linked_to_version.rb +6 -0
  42. data/spec/fixtures/models/milestone.rb +8 -0
  43. data/spec/fixtures/models/owner.rb +5 -0
  44. data/spec/fixtures/models/project.rb +26 -0
  45. data/spec/fixtures/models/test_model_defs.rb +67 -0
  46. data/spec/fixtures/models/version.rb +7 -0
  47. data/spec/loader_spec.rb +4 -3
  48. data/spec/method_dictionary_spec.rb +7 -6
  49. data/spec/method_mapper_spec.rb +3 -2
  50. data/spec/rails_sandbox/.gitignore +15 -0
  51. data/spec/rails_sandbox/Gemfile +40 -0
  52. data/spec/rails_sandbox/README.rdoc +261 -0
  53. data/spec/rails_sandbox/Rakefile +7 -0
  54. data/spec/rails_sandbox/app/assets/images/rails.png +0 -0
  55. data/spec/rails_sandbox/app/assets/javascripts/application.js +15 -0
  56. data/spec/rails_sandbox/app/assets/stylesheets/application.css +13 -0
  57. data/spec/rails_sandbox/app/controllers/application_controller.rb +3 -0
  58. data/spec/rails_sandbox/app/helpers/application_helper.rb +2 -0
  59. data/spec/rails_sandbox/app/mailers/.gitkeep +0 -0
  60. data/spec/rails_sandbox/app/models/.gitkeep +0 -0
  61. data/spec/rails_sandbox/app/models/category.rb +7 -0
  62. data/spec/rails_sandbox/app/models/empty.rb +2 -0
  63. data/spec/rails_sandbox/app/models/loader_release.rb +10 -0
  64. data/spec/rails_sandbox/app/models/long_and_complex_table_linked_to_version.rb +6 -0
  65. data/spec/rails_sandbox/app/models/milestone.rb +8 -0
  66. data/spec/rails_sandbox/app/models/owner.rb +5 -0
  67. data/spec/rails_sandbox/app/models/project.rb +26 -0
  68. data/spec/rails_sandbox/app/models/test_model_defs.rb +67 -0
  69. data/spec/rails_sandbox/app/models/version.rb +7 -0
  70. data/spec/rails_sandbox/app/views/layouts/application.html.erb +14 -0
  71. data/spec/rails_sandbox/config.ru +4 -0
  72. data/spec/rails_sandbox/config/application.rb +62 -0
  73. data/spec/rails_sandbox/config/boot.rb +6 -0
  74. data/spec/rails_sandbox/config/database.yml +20 -0
  75. data/spec/rails_sandbox/config/environment.rb +5 -0
  76. data/spec/rails_sandbox/config/environments/development.rb +37 -0
  77. data/spec/rails_sandbox/config/environments/production.rb +67 -0
  78. data/spec/rails_sandbox/config/environments/test.rb +37 -0
  79. data/spec/rails_sandbox/config/initializers/backtrace_silencers.rb +7 -0
  80. data/spec/rails_sandbox/config/initializers/inflections.rb +15 -0
  81. data/spec/rails_sandbox/config/initializers/mime_types.rb +5 -0
  82. data/spec/rails_sandbox/config/initializers/secret_token.rb +7 -0
  83. data/spec/rails_sandbox/config/initializers/session_store.rb +8 -0
  84. data/spec/rails_sandbox/config/initializers/wrap_parameters.rb +14 -0
  85. data/spec/rails_sandbox/config/locales/en.yml +5 -0
  86. data/spec/rails_sandbox/config/routes.rb +58 -0
  87. data/spec/rails_sandbox/db/migrate/20110803201325_create_test_bed.rb +96 -0
  88. data/spec/rails_sandbox/db/schema.rb +81 -0
  89. data/spec/rails_sandbox/db/seeds.rb +7 -0
  90. data/spec/rails_sandbox/lib/assets/.gitkeep +0 -0
  91. data/spec/rails_sandbox/lib/tasks/.gitkeep +0 -0
  92. data/spec/rails_sandbox/log/.gitkeep +0 -0
  93. data/spec/rails_sandbox/public/404.html +26 -0
  94. data/spec/rails_sandbox/public/422.html +26 -0
  95. data/spec/rails_sandbox/public/500.html +25 -0
  96. data/spec/rails_sandbox/public/favicon.ico +0 -0
  97. data/spec/rails_sandbox/public/index.html +241 -0
  98. data/spec/rails_sandbox/public/robots.txt +5 -0
  99. data/spec/rails_sandbox/script/rails +6 -0
  100. data/spec/rails_sandbox/test/fixtures/.gitkeep +0 -0
  101. data/spec/rails_sandbox/test/functional/.gitkeep +0 -0
  102. data/spec/rails_sandbox/test/integration/.gitkeep +0 -0
  103. data/spec/rails_sandbox/test/performance/browsing_test.rb +12 -0
  104. data/spec/rails_sandbox/test/test_helper.rb +13 -0
  105. data/spec/rails_sandbox/test/unit/.gitkeep +0 -0
  106. data/spec/rails_sandbox/vendor/assets/javascripts/.gitkeep +0 -0
  107. data/spec/rails_sandbox/vendor/assets/stylesheets/.gitkeep +0 -0
  108. data/spec/rails_sandbox/vendor/plugins/.gitkeep +0 -0
  109. data/spec/spec_helper.rb +144 -121
  110. data/spec/thor_spec.rb +34 -14
  111. metadata +207 -194
  112. data/lib/helpers/spree_helper.rb +0 -213
  113. data/lib/loaders/spreadsheet_loader.rb +0 -144
  114. data/lib/loaders/spree/image_loader.rb +0 -90
  115. data/lib/loaders/spree/product_loader.rb +0 -354
  116. data/lib/thor/spree/bootstrap_cleanup.thor +0 -61
  117. data/lib/thor/spree/products_images.thor +0 -252
  118. data/lib/thor/spree/reports.thor +0 -56
  119. data/public/spree/products/large/DEMO_001_ror_bag.jpeg +0 -0
  120. data/public/spree/products/large/DEMO_002_Powerstation.jpg +0 -0
  121. data/public/spree/products/large/DEMO_003_ror_mug.jpeg +0 -0
  122. data/public/spree/products/mini/DEMO_001_ror_bag.jpeg +0 -0
  123. data/public/spree/products/mini/DEMO_002_Powerstation.jpg +0 -0
  124. data/public/spree/products/mini/DEMO_003_ror_mug.jpeg +0 -0
  125. data/public/spree/products/original/DEMO_001_ror_bag.jpeg +0 -0
  126. data/public/spree/products/original/DEMO_002_Powerstation.jpg +0 -0
  127. data/public/spree/products/original/DEMO_003_ror_mug.jpeg +0 -0
  128. data/public/spree/products/product/DEMO_001_ror_bag.jpeg +0 -0
  129. data/public/spree/products/product/DEMO_002_Powerstation.jpg +0 -0
  130. data/public/spree/products/product/DEMO_003_ror_mug.jpeg +0 -0
  131. data/public/spree/products/small/DEMO_001_ror_bag.jpeg +0 -0
  132. data/public/spree/products/small/DEMO_002_Powerstation.jpg +0 -0
  133. data/public/spree/products/small/DEMO_003_ror_mug.jpeg +0 -0
  134. data/spec/fixtures/datashift_Spree_db.sqlite +0 -0
  135. data/spec/fixtures/datashift_test_models_db.sqlite +0 -0
  136. data/spec/fixtures/negative/SpreeProdMiss1Mandatory.csv +0 -4
  137. data/spec/fixtures/negative/SpreeProdMiss1Mandatory.xls +0 -0
  138. data/spec/fixtures/negative/SpreeProdMissManyMandatory.csv +0 -4
  139. data/spec/fixtures/negative/SpreeProdMissManyMandatory.xls +0 -0
  140. data/spec/fixtures/spree/SpreeImages.xls +0 -0
  141. data/spec/fixtures/spree/SpreeMultiVariant.csv +0 -4
  142. data/spec/fixtures/spree/SpreeProducts.csv +0 -4
  143. data/spec/fixtures/spree/SpreeProducts.xls +0 -0
  144. data/spec/fixtures/spree/SpreeProductsDefaults.yml +0 -15
  145. data/spec/fixtures/spree/SpreeProductsMandatoryOnly.xls +0 -0
  146. data/spec/fixtures/spree/SpreeProductsMultiColumn.csv +0 -4
  147. data/spec/fixtures/spree/SpreeProductsMultiColumn.xls +0 -0
  148. data/spec/fixtures/spree/SpreeProductsSimple.csv +0 -4
  149. data/spec/fixtures/spree/SpreeProductsSimple.xls +0 -0
  150. data/spec/fixtures/spree/SpreeProductsWithImages.csv +0 -4
  151. data/spec/fixtures/spree/SpreeProductsWithImages.xls +0 -0
  152. data/spec/fixtures/spree/SpreeZoneExample.csv +0 -5
  153. data/spec/fixtures/spree/SpreeZoneExample.xls +0 -0
  154. data/spec/spree_exporter_spec.rb +0 -72
  155. data/spec/spree_generator_spec.rb +0 -96
  156. data/spec/spree_images_loader_spec.rb +0 -107
  157. data/spec/spree_loader_spec.rb +0 -375
  158. data/spec/spree_method_mapping_spec.rb +0 -226
  159. data/spec/spree_variants_loader_spec.rb +0 -189
  160. data/tasks/export/excel_generator.rake +0 -102
  161. data/tasks/import/excel.rake +0 -75
  162. data/test/helper.rb +0 -18
  163. data/test/test_interact.rb +0 -7
@@ -0,0 +1,64 @@
1
+ # Copyright:: Autotelik Media Ltd
2
+ # Author :: Tom Statter
3
+ # Date :: July 2010
4
+ # License::
5
+ #
6
+ #
7
+ if(DataShift::Guards::jruby?)
8
+
9
+ require 'java'
10
+
11
+ require 'benchmark'
12
+ require "poi-3.7-20101029.jar"
13
+
14
+ module RubyPoiTranslations
15
+
16
+ java_import 'org.apache.poi.poifs.filesystem.POIFSFileSystem'
17
+
18
+ java_import 'org.apache.poi.hssf.usermodel.HSSFCell'
19
+ java_import 'org.apache.poi.hssf.usermodel.HSSFWorkbook'
20
+ java_import 'org.apache.poi.hssf.usermodel.HSSFCellStyle'
21
+ java_import 'org.apache.poi.hssf.usermodel.HSSFDataFormat'
22
+ java_import 'org.apache.poi.hssf.usermodel.HSSFClientAnchor'
23
+ java_import 'org.apache.poi.hssf.usermodel.HSSFRichTextString'
24
+
25
+ java_import 'org.apache.poi.hssf.util.HSSFColor'
26
+
27
+ java_import 'java.io.ByteArrayOutputStream'
28
+ java_import 'java.util.Date'
29
+ java_import 'java.io.FileInputStream'
30
+ java_import 'java.io.FileOutputStream'
31
+
32
+ # Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_STRING, Cell.CELL_TYPE_FORMULA,
33
+ # Cell.CELL_TYPE_BLANK, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_ERROR
34
+
35
+ def cell_value(cell)
36
+ case (cell.getCellType())
37
+ when HSSFCell::CELL_TYPE_FORMULA then return cell.getCellFormula()
38
+ when HSSFCell::CELL_TYPE_NUMERIC then return cell.getNumericCellValue()
39
+ when HSSFCell::CELL_TYPE_STRING then return cell.getStringCellValue()
40
+ when HSSFCell::CELL_TYPE_BOOLEAN then return cell.getBooleanCellValue()
41
+ when HSSFCell::CELL_TYPE_ERROR then return cell.getErrorCellValue()
42
+
43
+ when HSSFCell::CELL_TYPE_BLANK then return ""
44
+ end
45
+ end
46
+
47
+ # Return the suitable type for a HSSFCell from a Ruby data type
48
+
49
+ def poi_cell_type(data)
50
+
51
+ if(data.kind_of?(Numeric))
52
+ HSSFCell::CELL_TYPE_NUMERIC
53
+ elsif(data.nil?)
54
+ HSSFCell::CELL_TYPE_BLANK
55
+ elsif(data.is_a?(TrueClass) || data.is_a?(FalseClass))
56
+ HSSFCell::CELL_TYPE_BOOLEAN
57
+ else
58
+ HSSFCell::CELL_TYPE_STRING
59
+ end
60
+ # HSSFCell::CELL_TYPE_FORMULA
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,31 @@
1
+ # Copyright:: Autotelik Media Ltd
2
+ # Author :: Tom Statter
3
+ # Date :: July 2010
4
+ # License::
5
+ #
6
+ #
7
+ # Extend the Sporeadsheet classes with some of our common methods
8
+ #
9
+ # ... to do extract into separate module with pure ruby that works with both POI and Spreadsheet
10
+
11
+ require 'excel_base'
12
+
13
+ class Spreadsheet::Worksheet
14
+
15
+ include ExcelBase
16
+
17
+ # Convert array into a header row
18
+ def set_headers(headers, apply_style = nil)
19
+ return if headers.empty?
20
+
21
+ headers.each_with_index do |datum, i|
22
+ self[0, i] = datum
23
+ end
24
+ end
25
+
26
+ def num_rows
27
+ rows.size
28
+ end
29
+
30
+ end
31
+
@@ -50,9 +50,13 @@ module DataShift
50
50
  @method_details_list[type.to_sym] || []
51
51
  end
52
52
 
53
+ alias_method(:get_list_of_method_details, :get_list)
54
+
53
55
  def get_operators( op_type )
54
56
  get_list(op_type).collect { |md| md.operator }
55
57
  end
58
+
59
+ alias_method(:get_list_of_operators, :get_list)
56
60
 
57
61
  def all_available_operators
58
62
  method_details_list.values.flatten.collect(&:operator)
@@ -47,7 +47,7 @@ module DataShift
47
47
 
48
48
  File.open(f, "w") do |csv|
49
49
 
50
- headers = first.serializable_hash.keys.collect
50
+ headers = first.serializable_hash.keys
51
51
 
52
52
  [*options[:methods]].each do |c| headers << c if(first.respond_to?(c)) end if(options[:methods])
53
53
 
@@ -125,6 +125,8 @@ module DataShift
125
125
  if(assoc_object.is_a?ActiveRecord::Base)
126
126
  column_text = record_to_column(assoc_object) # belongs_to or has_one
127
127
 
128
+ # TODO -ColumnPacker class shared between excel/csv
129
+
128
130
  csv << "#{@text_delim}#{column_text}#{@text_delim}" << Delimiters::csv_delim
129
131
  #csv << record_to_csv(r)
130
132
 
@@ -12,108 +12,93 @@ module DataShift
12
12
 
13
13
  require 'exporter_base'
14
14
 
15
- if(Guards::jruby?)
15
+ require 'excel'
16
16
 
17
- require 'jruby/jexcel_file'
17
+ class ExcelExporter < ExporterBase
18
18
 
19
- class ExcelExporter < ExporterBase
20
-
21
- attr_accessor :filename
19
+ attr_accessor :filename
22
20
 
23
- def initialize(filename)
24
- @filename = filename
25
- end
21
+ def initialize(filename)
22
+ @filename = filename
23
+ end
26
24
 
27
25
 
28
- # Create an Excel file from list of ActiveRecord objects
29
- def export(records, options = {})
26
+ # Create an Excel file from list of ActiveRecord objects
27
+ def export(records, options = {})
30
28
 
31
- raise ArgumentError.new('Please supply array of records to export') unless records.is_a? Array
29
+ raise ArgumentError.new('Please supply array of records to export') unless records.is_a? Array
32
30
 
33
- excel = JExcelFile.new()
31
+ excel = Excel.new
34
32
 
35
- if(options[:sheet_name] )
36
- excel.create_sheet( options[:sheet_name] )
37
- else
38
- excel.create_sheet( records.first.class.name )
39
- end
33
+ if(options[:sheet_name] )
34
+ excel.create_worksheet( :name => options[:sheet_name] )
35
+ else
36
+ excel.create_worksheet( :name => records.first.class.name )
37
+ end
40
38
 
41
- excel.ar_to_headers(records)
39
+ excel.ar_to_headers(records)
42
40
 
43
- excel.ar_to_xls(records)
44
-
45
-
46
- # => :methods => List of methods to additionally export on each record
47
-
48
- excel.save( filename() )
49
- end
41
+ excel.ar_to_xls(records)
42
+
43
+ excel.write( filename() )
44
+ end
50
45
 
51
- # Create an Excel file from list of ActiveRecord objects
52
- # Specify which associations to export via :with or :exclude
53
- # Possible values are : [:assignment, :belongs_to, :has_one, :has_many]
54
- #
55
- def export_with_associations(klass, items, options = {})
46
+ # Create an Excel file from list of ActiveRecord objects
47
+ # Specify which associations to export via :with or :exclude
48
+ # Possible values are : [:assignment, :belongs_to, :has_one, :has_many]
49
+ #
50
+ def export_with_associations(klass, items, options = {})
56
51
 
57
- excel = JExcelFile.new()
52
+ excel = Excel.new
58
53
 
59
- if(options[:sheet_name] )
60
- excel.create_sheet( options[:sheet_name] )
61
- else
62
- excel.create_sheet( items.first.class.name )
63
- end
54
+ if(options[:sheet_name] )
55
+ excel.create_worksheet( :name => options[:sheet_name] )
56
+ else
57
+ excel.create_worksheet( :name => items.first.class.name )
58
+ end
64
59
 
65
- MethodDictionary.find_operators( klass )
60
+ MethodDictionary.find_operators( klass )
66
61
 
67
- MethodDictionary.build_method_details( klass )
62
+ MethodDictionary.build_method_details( klass )
68
63
 
69
- work_list = options[:with] || MethodDetail::supported_types_enum
64
+ work_list = options[:with] || MethodDetail::supported_types_enum
70
65
 
71
- headers = []
72
- puts "work_list : [#{work_list.inspect}]"
66
+ headers = []
73
67
 
74
- details_mgr = MethodDictionary.method_details_mgrs[klass]
75
-
76
-
77
- data = []
78
- # For each type belongs has_one, has_many etc find the operators
79
- # and create headers, then for each record call those operators
80
- work_list.each do |op_type|
68
+ details_mgr = MethodDictionary.method_details_mgrs[klass]
69
+
70
+ data = []
71
+ # For each type belongs has_one, has_many etc find the operators
72
+ # and create headers, then for each record call those operators
73
+ work_list.each do |op_type|
81
74
 
82
- list_for_class_and_op = details_mgr.get_list(op_type)
75
+ list_for_class_and_op = details_mgr.get_list(op_type)
83
76
 
84
- next if(list_for_class_and_op.nil? || list_for_class_and_op.empty?)
77
+ next if(list_for_class_and_op.nil? || list_for_class_and_op.empty?)
85
78
 
86
- # method_details = MethodDictionary.send("#{mdtype}_for", klass)
79
+ # method_details = MethodDictionary.send("#{mdtype}_for", klass)
87
80
 
88
- list_for_class_and_op.each do |md|
89
- headers << "#{md.operator}"
90
- items.each do |i|
91
- data << i.send( md.operator )
92
- end
93
-
81
+ list_for_class_and_op.each do |md|
82
+ headers << "#{md.operator}"
83
+ items.each do |i|
84
+ data << i.send( md.operator )
94
85
  end
86
+
87
+ end
95
88
 
96
- excel.set_headers( headers )
89
+ excel.set_headers( headers )
97
90
 
98
- row_index = 1
91
+ row = 1
92
+ column = 0
99
93
 
100
- items.each do |datum|
101
- excel.create_row(row_index += 1)
102
- excel.ar_to_xls_row(1, datum)
103
- end
104
-
105
- excel.save( filename() )
94
+ items.each do |row_of_data|
95
+ excel.ar_to_xls_row(row, column, row_of_data)
96
+ row += 1
106
97
  end
107
- end
108
- end # ExcelGenerator
109
-
110
- else
111
- class ExcelExporter < ExporterBase
112
-
113
- def initialize(filename)
114
- raise DataShift::BadRuby, "Apologies but DataShift Excel facilities currently need JRuby. Please switch to, or install JRuby"
98
+
99
+ excel.write( filename() )
115
100
  end
116
101
  end
117
- end # jruby
102
+ end # ExcelGenerator
118
103
 
119
104
  end # DataShift
@@ -12,111 +12,107 @@ module DataShift
12
12
 
13
13
  require 'generator_base'
14
14
 
15
- if(Guards::jruby?)
15
+ require 'excel'
16
16
 
17
- require 'jruby/jexcel_file'
17
+ class ExcelGenerator < GeneratorBase
18
18
 
19
- class ExcelGenerator < GeneratorBase
20
-
21
- include DataShift::Logging
19
+ include DataShift::Logging
22
20
 
23
- attr_accessor :excel, :filename
21
+ attr_accessor :excel, :filename
24
22
 
25
- def initialize(filename)
26
- @filename = filename
27
- end
23
+ def initialize(filename)
24
+ @filename = filename
25
+ end
28
26
 
29
- # Create an Excel file template (header row) representing supplied Model
30
- # Options:
31
- # * <tt>:autosize</tt> - Autosize all the columns
32
- #
33
- def generate(klass, options = {})
27
+ # Create an Excel file template (header row) representing supplied Model
28
+ # Options:
29
+ # * <tt>:autosize</tt> - Autosize all the columns
30
+ #
31
+ def generate(klass, options = {})
34
32
 
35
- prepare(klass, options)
33
+ prepare_excel(klass, options)
36
34
 
37
- @excel.set_headers(MethodDictionary.assignments[klass])
35
+ @excel.set_headers(MethodDictionary.assignments[klass])
38
36
 
39
- logger.info("ExcelGenerator saving generated template #{@filename}")
37
+ logger.info("ExcelGenerator saving generated template #{@filename}")
40
38
 
41
- @excel.autosize if(options[:autosize])
39
+ #@excel.autosize if(options[:autosize])
42
40
 
43
- @excel.save( @filename )
44
- end
41
+ @excel.write( @filename )
42
+ end
45
43
 
46
44
 
47
- # Create an Excel file template (header row) representing supplied Model
48
- # and it's associations
49
- #
50
- # Options:
51
- # * <tt>:autosize</tt> - Autosize all the columns
52
- #
53
- # * <tt>:exclude</tt> - Associations to exclude.
54
- # You can specify a hash of {association_type => [array of association names] }
55
- # to exclude from the template.
56
- #
57
- # Possible association_type values are given by MethodDetail::supported_types_enum
58
- # ... [:assignment, :belongs_to, :has_one, :has_many]
59
- #
60
- def generate_with_associations(klass, options = {})
45
+ # Create an Excel file template (header row) representing supplied Model
46
+ # and it's associations
47
+ #
48
+ # Options:
49
+ # * <tt>:autosize</tt> - Autosize all the columns
50
+ #
51
+ # * <tt>:exclude</tt> - Association TYPE(s) to exclude.
52
+ # You can specify a hash of {association_type => [array of association names] }
53
+ # to exclude from the template.
54
+ #
55
+ # Possible association_type values are given by MethodDetail::supported_types_enum
56
+ # ... [:assignment, :belongs_to, :has_one, :has_many]
57
+ #
58
+ # * <tt>:remove</tt> - Association NAME(s) to remove .. :title, :id, :name
59
+ # .
60
+ def generate_with_associations(klass, options = {})
61
61
 
62
- prepare(klass, options)
62
+ prepare_excel(klass, options)
63
63
 
64
- MethodDictionary.build_method_details( klass )
64
+ MethodDictionary.build_method_details( klass )
65
65
 
66
- work_list = MethodDetail::supported_types_enum.to_a
67
- work_list -= options[:exclude].to_a
66
+ work_list = MethodDetail::supported_types_enum.to_a - [ *options[:exclude] ]
67
+
68
+ remove_list = [ *options[:remove] ].compact.collect{|x| x.to_s.downcase.to_sym }
68
69
 
69
- headers = []
70
+ headers = []
70
71
 
71
- details_mgr = MethodDictionary.method_details_mgrs[klass]
72
+ details_mgr = MethodDictionary.method_details_mgrs[klass]
72
73
 
73
- work_list.each do |op_type|
74
- list_for_class_and_op = details_mgr.get_list(op_type)
74
+ work_list.each do |assoc_type|
75
+ method_details_for_assoc_type = details_mgr.get_list_of_method_details(assoc_type)
75
76
 
76
- next if(list_for_class_and_op.nil? || list_for_class_and_op.empty?)
77
- list_for_class_and_op.each {|md| headers << "#{md.operator}" }
77
+ next if(method_details_for_assoc_type.nil? || method_details_for_assoc_type.empty?)
78
+
79
+ method_details_for_assoc_type.each do |md|
80
+ comparable_association = md.operator.to_s.downcase.to_sym
81
+
82
+ i = remove_list.index { |r| r == comparable_association }
83
+
84
+ (i) ? remove_list.delete_at(i) : headers << "#{md.operator}"
78
85
  end
86
+ end
79
87
 
80
- @excel.set_headers( headers )
88
+ @excel.set_headers( headers )
81
89
 
82
- @excel.autosize if(options[:autosize])
90
+ # @excel.autosize if(options[:autosize])
83
91
 
84
- @excel.save( filename() )
85
- end
92
+ @excel.write( filename() )
93
+ end
86
94
 
87
- private
95
+ private
88
96
 
89
- def prepare(klass, options = {})
90
- @filename = options[:filename] if options[:filename]
97
+ def prepare_excel(klass, options = {})
98
+ @filename = options[:filename] if options[:filename]
91
99
 
92
- logger.info("ExcelGenerator creating template with associations for class #{klass}")
100
+ logger.info("ExcelGenerator creating template with associations for class #{klass}")
93
101
 
94
- @excel = JExcelFile.new()
102
+ @excel = Excel.new()
95
103
 
96
- if(options[:sheet_name] )
97
- @excel.create_sheet( options[:sheet_name] )
98
- else
99
- @excel.create_sheet( klass.name )
100
- end
104
+ name = options[:sheet_name] || klass.name
101
105
 
102
- unless @excel.sheet
103
- logger.error("Excel failed to create WorkSheet for #{klass.name}")
104
-
105
- raise "Failed to create Excel WorkSheet for #{klass.name}"
106
- end
106
+ sheet = @excel.create_worksheet( :name => name )
107
+
108
+ unless sheet
109
+ logger.error("Excel failed to create WorkSheet for #{name}")
107
110
 
108
- MethodDictionary.find_operators( klass )
109
- end
110
- end # ExcelGenerator
111
-
112
- else
113
- class ExcelGenerator < GeneratorBase
114
-
115
- def initialize(filename)
116
- @filename = filename
117
- raise DataShift::BadRuby, "Apologies but DataShift Excel facilities currently need JRuby. Please switch to, or install JRuby"
111
+ raise "Failed to create Excel WorkSheet for #{name}"
118
112
  end
113
+
114
+ MethodDictionary.find_operators( klass )
119
115
  end
120
- end # jruby
116
+ end # ExcelGenerator
121
117
 
122
118
  end # DataShift