datashift 0.9.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +63 -64
- data/Rakefile +4 -7
- data/VERSION +1 -1
- data/datashift.gemspec +92 -62
- data/lib/applications/apache_poi_extensions.rb +62 -0
- data/lib/applications/excel.rb +78 -0
- data/lib/applications/excel_base.rb +65 -0
- data/lib/applications/jexcel_file.rb +222 -0
- data/lib/applications/jexcel_file_extensions.rb +244 -0
- data/lib/applications/jruby/{jexcel_file.rb → old_pre_proxy_jexcel_file.rb} +0 -0
- data/lib/applications/ruby_poi_translations.rb +64 -0
- data/lib/applications/spreadsheet_extensions.rb +31 -0
- data/lib/datashift/method_details_manager.rb +4 -0
- data/lib/exporters/csv_exporter.rb +3 -1
- data/lib/exporters/excel_exporter.rb +59 -74
- data/lib/generators/excel_generator.rb +70 -74
- data/lib/guards.rb +57 -0
- data/lib/loaders/excel_loader.rb +105 -105
- data/lib/loaders/loader_base.rb +43 -21
- data/lib/loaders/paperclip/attachment_loader.rb +104 -0
- data/lib/loaders/paperclip/datashift_paperclip.rb +78 -0
- data/lib/loaders/paperclip/{image_loader.rb → image_loading.rb} +2 -18
- data/lib/thor/{generate_excel.thor → generate.thor} +48 -0
- data/lib/thor/paperclip.thor +85 -0
- data/lib/thor/tools.thor +23 -2
- data/spec/Gemfile +1 -7
- data/spec/csv_exporter_spec.rb +4 -4
- data/spec/csv_loader_spec.rb +1 -1
- data/spec/excel_exporter_spec.rb +43 -45
- data/spec/excel_generator_spec.rb +132 -60
- data/spec/excel_loader_spec.rb +134 -140
- data/spec/excel_spec.rb +179 -0
- data/spec/fixtures/ProjectsMultiCategoriesHeaderLookup.xls +0 -0
- data/spec/fixtures/config/database.yml +2 -2
- data/spec/fixtures/db/datashift_test_models_db.sqlite +0 -0
- data/spec/{db → fixtures/db}/migrate/20110803201325_create_test_bed.rb +0 -0
- data/spec/fixtures/load_datashift.thor +3 -0
- data/spec/fixtures/models/category.rb +7 -0
- data/spec/fixtures/models/empty.rb +2 -0
- data/spec/fixtures/models/loader_release.rb +10 -0
- data/spec/fixtures/models/long_and_complex_table_linked_to_version.rb +6 -0
- data/spec/fixtures/models/milestone.rb +8 -0
- data/spec/fixtures/models/owner.rb +5 -0
- data/spec/fixtures/models/project.rb +26 -0
- data/spec/fixtures/models/test_model_defs.rb +67 -0
- data/spec/fixtures/models/version.rb +7 -0
- data/spec/loader_spec.rb +4 -3
- data/spec/method_dictionary_spec.rb +7 -6
- data/spec/method_mapper_spec.rb +3 -2
- data/spec/rails_sandbox/.gitignore +15 -0
- data/spec/rails_sandbox/Gemfile +40 -0
- data/spec/rails_sandbox/README.rdoc +261 -0
- data/spec/rails_sandbox/Rakefile +7 -0
- data/spec/rails_sandbox/app/assets/images/rails.png +0 -0
- data/spec/rails_sandbox/app/assets/javascripts/application.js +15 -0
- data/spec/rails_sandbox/app/assets/stylesheets/application.css +13 -0
- data/spec/rails_sandbox/app/controllers/application_controller.rb +3 -0
- data/spec/rails_sandbox/app/helpers/application_helper.rb +2 -0
- data/spec/rails_sandbox/app/mailers/.gitkeep +0 -0
- data/spec/rails_sandbox/app/models/.gitkeep +0 -0
- data/spec/rails_sandbox/app/models/category.rb +7 -0
- data/spec/rails_sandbox/app/models/empty.rb +2 -0
- data/spec/rails_sandbox/app/models/loader_release.rb +10 -0
- data/spec/rails_sandbox/app/models/long_and_complex_table_linked_to_version.rb +6 -0
- data/spec/rails_sandbox/app/models/milestone.rb +8 -0
- data/spec/rails_sandbox/app/models/owner.rb +5 -0
- data/spec/rails_sandbox/app/models/project.rb +26 -0
- data/spec/rails_sandbox/app/models/test_model_defs.rb +67 -0
- data/spec/rails_sandbox/app/models/version.rb +7 -0
- data/spec/rails_sandbox/app/views/layouts/application.html.erb +14 -0
- data/spec/rails_sandbox/config.ru +4 -0
- data/spec/rails_sandbox/config/application.rb +62 -0
- data/spec/rails_sandbox/config/boot.rb +6 -0
- data/spec/rails_sandbox/config/database.yml +20 -0
- data/spec/rails_sandbox/config/environment.rb +5 -0
- data/spec/rails_sandbox/config/environments/development.rb +37 -0
- data/spec/rails_sandbox/config/environments/production.rb +67 -0
- data/spec/rails_sandbox/config/environments/test.rb +37 -0
- data/spec/rails_sandbox/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/rails_sandbox/config/initializers/inflections.rb +15 -0
- data/spec/rails_sandbox/config/initializers/mime_types.rb +5 -0
- data/spec/rails_sandbox/config/initializers/secret_token.rb +7 -0
- data/spec/rails_sandbox/config/initializers/session_store.rb +8 -0
- data/spec/rails_sandbox/config/initializers/wrap_parameters.rb +14 -0
- data/spec/rails_sandbox/config/locales/en.yml +5 -0
- data/spec/rails_sandbox/config/routes.rb +58 -0
- data/spec/rails_sandbox/db/migrate/20110803201325_create_test_bed.rb +96 -0
- data/spec/rails_sandbox/db/schema.rb +81 -0
- data/spec/rails_sandbox/db/seeds.rb +7 -0
- data/spec/rails_sandbox/lib/assets/.gitkeep +0 -0
- data/spec/rails_sandbox/lib/tasks/.gitkeep +0 -0
- data/spec/rails_sandbox/log/.gitkeep +0 -0
- data/spec/rails_sandbox/public/404.html +26 -0
- data/spec/rails_sandbox/public/422.html +26 -0
- data/spec/rails_sandbox/public/500.html +25 -0
- data/spec/rails_sandbox/public/favicon.ico +0 -0
- data/spec/rails_sandbox/public/index.html +241 -0
- data/spec/rails_sandbox/public/robots.txt +5 -0
- data/spec/rails_sandbox/script/rails +6 -0
- data/spec/rails_sandbox/test/fixtures/.gitkeep +0 -0
- data/spec/rails_sandbox/test/functional/.gitkeep +0 -0
- data/spec/rails_sandbox/test/integration/.gitkeep +0 -0
- data/spec/rails_sandbox/test/performance/browsing_test.rb +12 -0
- data/spec/rails_sandbox/test/test_helper.rb +13 -0
- data/spec/rails_sandbox/test/unit/.gitkeep +0 -0
- data/spec/rails_sandbox/vendor/assets/javascripts/.gitkeep +0 -0
- data/spec/rails_sandbox/vendor/assets/stylesheets/.gitkeep +0 -0
- data/spec/rails_sandbox/vendor/plugins/.gitkeep +0 -0
- data/spec/spec_helper.rb +144 -121
- data/spec/thor_spec.rb +34 -14
- metadata +207 -194
- data/lib/helpers/spree_helper.rb +0 -213
- data/lib/loaders/spreadsheet_loader.rb +0 -144
- data/lib/loaders/spree/image_loader.rb +0 -90
- data/lib/loaders/spree/product_loader.rb +0 -354
- data/lib/thor/spree/bootstrap_cleanup.thor +0 -61
- data/lib/thor/spree/products_images.thor +0 -252
- data/lib/thor/spree/reports.thor +0 -56
- data/public/spree/products/large/DEMO_001_ror_bag.jpeg +0 -0
- data/public/spree/products/large/DEMO_002_Powerstation.jpg +0 -0
- data/public/spree/products/large/DEMO_003_ror_mug.jpeg +0 -0
- data/public/spree/products/mini/DEMO_001_ror_bag.jpeg +0 -0
- data/public/spree/products/mini/DEMO_002_Powerstation.jpg +0 -0
- data/public/spree/products/mini/DEMO_003_ror_mug.jpeg +0 -0
- data/public/spree/products/original/DEMO_001_ror_bag.jpeg +0 -0
- data/public/spree/products/original/DEMO_002_Powerstation.jpg +0 -0
- data/public/spree/products/original/DEMO_003_ror_mug.jpeg +0 -0
- data/public/spree/products/product/DEMO_001_ror_bag.jpeg +0 -0
- data/public/spree/products/product/DEMO_002_Powerstation.jpg +0 -0
- data/public/spree/products/product/DEMO_003_ror_mug.jpeg +0 -0
- data/public/spree/products/small/DEMO_001_ror_bag.jpeg +0 -0
- data/public/spree/products/small/DEMO_002_Powerstation.jpg +0 -0
- data/public/spree/products/small/DEMO_003_ror_mug.jpeg +0 -0
- data/spec/fixtures/datashift_Spree_db.sqlite +0 -0
- data/spec/fixtures/datashift_test_models_db.sqlite +0 -0
- data/spec/fixtures/negative/SpreeProdMiss1Mandatory.csv +0 -4
- data/spec/fixtures/negative/SpreeProdMiss1Mandatory.xls +0 -0
- data/spec/fixtures/negative/SpreeProdMissManyMandatory.csv +0 -4
- data/spec/fixtures/negative/SpreeProdMissManyMandatory.xls +0 -0
- data/spec/fixtures/spree/SpreeImages.xls +0 -0
- data/spec/fixtures/spree/SpreeMultiVariant.csv +0 -4
- data/spec/fixtures/spree/SpreeProducts.csv +0 -4
- data/spec/fixtures/spree/SpreeProducts.xls +0 -0
- data/spec/fixtures/spree/SpreeProductsDefaults.yml +0 -15
- data/spec/fixtures/spree/SpreeProductsMandatoryOnly.xls +0 -0
- data/spec/fixtures/spree/SpreeProductsMultiColumn.csv +0 -4
- data/spec/fixtures/spree/SpreeProductsMultiColumn.xls +0 -0
- data/spec/fixtures/spree/SpreeProductsSimple.csv +0 -4
- data/spec/fixtures/spree/SpreeProductsSimple.xls +0 -0
- data/spec/fixtures/spree/SpreeProductsWithImages.csv +0 -4
- data/spec/fixtures/spree/SpreeProductsWithImages.xls +0 -0
- data/spec/fixtures/spree/SpreeZoneExample.csv +0 -5
- data/spec/fixtures/spree/SpreeZoneExample.xls +0 -0
- data/spec/spree_exporter_spec.rb +0 -72
- data/spec/spree_generator_spec.rb +0 -96
- data/spec/spree_images_loader_spec.rb +0 -107
- data/spec/spree_loader_spec.rb +0 -375
- data/spec/spree_method_mapping_spec.rb +0 -226
- data/spec/spree_variants_loader_spec.rb +0 -189
- data/tasks/export/excel_generator.rake +0 -102
- data/tasks/import/excel.rake +0 -75
- data/test/helper.rb +0 -18
- data/test/test_interact.rb +0 -7
File without changes
|
@@ -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
|
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
|
-
|
15
|
+
require 'excel'
|
16
16
|
|
17
|
-
|
17
|
+
class ExcelExporter < ExporterBase
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
attr_accessor :filename
|
19
|
+
attr_accessor :filename
|
22
20
|
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
def initialize(filename)
|
22
|
+
@filename = filename
|
23
|
+
end
|
26
24
|
|
27
25
|
|
28
|
-
|
29
|
-
|
26
|
+
# Create an Excel file from list of ActiveRecord objects
|
27
|
+
def export(records, options = {})
|
30
28
|
|
31
|
-
|
29
|
+
raise ArgumentError.new('Please supply array of records to export') unless records.is_a? Array
|
32
30
|
|
33
|
-
|
31
|
+
excel = Excel.new
|
34
32
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
-
|
39
|
+
excel.ar_to_headers(records)
|
42
40
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
excel.save( filename() )
|
49
|
-
end
|
41
|
+
excel.ar_to_xls(records)
|
42
|
+
|
43
|
+
excel.write( filename() )
|
44
|
+
end
|
50
45
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
-
|
52
|
+
excel = Excel.new
|
58
53
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|
-
|
60
|
+
MethodDictionary.find_operators( klass )
|
66
61
|
|
67
|
-
|
62
|
+
MethodDictionary.build_method_details( klass )
|
68
63
|
|
69
|
-
|
64
|
+
work_list = options[:with] || MethodDetail::supported_types_enum
|
70
65
|
|
71
|
-
|
72
|
-
puts "work_list : [#{work_list.inspect}]"
|
66
|
+
headers = []
|
73
67
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
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
|
-
|
75
|
+
list_for_class_and_op = details_mgr.get_list(op_type)
|
83
76
|
|
84
|
-
|
77
|
+
next if(list_for_class_and_op.nil? || list_for_class_and_op.empty?)
|
85
78
|
|
86
|
-
|
79
|
+
# method_details = MethodDictionary.send("#{mdtype}_for", klass)
|
87
80
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
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
|
-
|
89
|
+
excel.set_headers( headers )
|
97
90
|
|
98
|
-
|
91
|
+
row = 1
|
92
|
+
column = 0
|
99
93
|
|
100
|
-
|
101
|
-
|
102
|
-
|
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
|
-
|
108
|
-
|
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 #
|
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
|
-
|
15
|
+
require 'excel'
|
16
16
|
|
17
|
-
|
17
|
+
class ExcelGenerator < GeneratorBase
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
include DataShift::Logging
|
19
|
+
include DataShift::Logging
|
22
20
|
|
23
|
-
|
21
|
+
attr_accessor :excel, :filename
|
24
22
|
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
def initialize(filename)
|
24
|
+
@filename = filename
|
25
|
+
end
|
28
26
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
-
|
33
|
+
prepare_excel(klass, options)
|
36
34
|
|
37
|
-
|
35
|
+
@excel.set_headers(MethodDictionary.assignments[klass])
|
38
36
|
|
39
|
-
|
37
|
+
logger.info("ExcelGenerator saving generated template #{@filename}")
|
40
38
|
|
41
|
-
|
39
|
+
#@excel.autosize if(options[:autosize])
|
42
40
|
|
43
|
-
|
44
|
-
|
41
|
+
@excel.write( @filename )
|
42
|
+
end
|
45
43
|
|
46
44
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
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
|
-
|
62
|
+
prepare_excel(klass, options)
|
63
63
|
|
64
|
-
|
64
|
+
MethodDictionary.build_method_details( klass )
|
65
65
|
|
66
|
-
|
67
|
-
|
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
|
-
|
70
|
+
headers = []
|
70
71
|
|
71
|
-
|
72
|
+
details_mgr = MethodDictionary.method_details_mgrs[klass]
|
72
73
|
|
73
|
-
|
74
|
-
|
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
|
-
|
77
|
-
|
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
|
-
|
88
|
+
@excel.set_headers( headers )
|
81
89
|
|
82
|
-
|
90
|
+
# @excel.autosize if(options[:autosize])
|
83
91
|
|
84
|
-
|
85
|
-
|
92
|
+
@excel.write( filename() )
|
93
|
+
end
|
86
94
|
|
87
|
-
|
95
|
+
private
|
88
96
|
|
89
|
-
|
90
|
-
|
97
|
+
def prepare_excel(klass, options = {})
|
98
|
+
@filename = options[:filename] if options[:filename]
|
91
99
|
|
92
|
-
|
100
|
+
logger.info("ExcelGenerator creating template with associations for class #{klass}")
|
93
101
|
|
94
|
-
|
102
|
+
@excel = Excel.new()
|
95
103
|
|
96
|
-
|
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
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
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
|
-
|
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 #
|
116
|
+
end # ExcelGenerator
|
121
117
|
|
122
118
|
end # DataShift
|