44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/spreadsheet_architect.rb', line 44
def self.get_options(options={}, klass)
has_custom_columns = klass.instance_methods.include?(:spreadsheet_columns)
if !options[:data] && defined?(ActiveRecord) && klass.ancestors.include?(ActiveRecord::Base)
options[:data] = klass.where(options[:where]).order(options[:order]).to_a
end
if !options[:data] || options[:data].empty?
raise SpreadsheetArchitect::NoDataError
end
if !has_custom_columns && defined?(ActiveRecord) && klass.ancestors.include?(ActiveRecord::Base)
ignored_columns = ["id","created_at","updated_at","deleted_at"]
the_column_names = (klass.column_names - ignored_columns)
= the_column_names.map{|x| str_humanize(x)}
columns = the_column_names.map{|x| x.to_sym}
types = klass.columns.keep_if{|x| !ignored_columns.include?(x.name)}.collect(&:type)
types.map!{|type| self.get_type(nil, type)}
elsif has_custom_columns
= []
columns = []
types = []
array = options[:spreadsheet_columns] || options[:data].first.spreadsheet_columns
array.each do |x|
if x.is_a?(Array)
.push x[0].to_s
columns.push x[1]
types.push self.get_type(x[1], x[2])
else
.push str_humanize(x.to_s)
columns.push x
types.push self.get_type(x, nil)
end
end
else
raise SpreadsheetArchitect::SpreadsheetColumnsNotDefined, klass
end
data = []
options[:data].each do |instance|
if has_custom_columns && !options[:spreadsheet_columns]
row_data = []
instance.spreadsheet_columns.each do |x|
if x.is_a?(Array)
row_data.push(x[1].is_a?(Symbol) ? instance.instance_eval(x[1].to_s) : x[1])
else
row_data.push(x.is_a?(Symbol) ? instance.instance_eval(x.to_s) : x)
end
end
data.push row_data
else
data.push columns.map{|col| col.is_a?(Symbol) ? instance.instance_eval(col.to_s) : col}
end
end
= (options[:headers] == false ? false : )
= {background_color: "AAAAAA", color: "FFFFFF", align: :center, bold: false, font_name: 'Arial', font_size: 10, italic: false, underline: false}
if options[:header_style]
.merge!(options[:header_style])
elsif options[:header_style] == false
= false
end
row_style = {background_color: nil, color: "000000", align: :left, bold: false, font_name: 'Arial', font_size: 10, italic: false, underline: false}
if options[:row_style]
row_style.merge!(options[:row_style])
end
sheet_name = options[:sheet_name] || klass.name
return {headers: , header_style: , row_style: row_style, types: types, sheet_name: sheet_name, data: data}
end
|