3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
|
# File 'lib/spreadsheet_architect/utils.rb', line 3
def self.get_cell_data(options, klass)
if options[:data] && options[:instances]
raise SpreadsheetArchitect::Exceptions::MultipleDataSourcesError
elsif options[:data]
data = options[:data]
end
if options[:headers] == true
= []
if options[:data]
= false
else
= true
end
elsif options[:headers].is_a?(Array)
= options[:headers]
else
= false
end
if options[:column_types]
column_types = options[:column_types]
else
column_types = []
needs_column_types = true
end
if !data
if !options[:instances]
if is_ar_model?(klass)
options[:instances] = klass.where(nil).to_a else
raise SpreadsheetArchitect::Exceptions::NoDataError
end
end
if options[:spreadsheet_columns]
if [String, Symbol].any?{|x| options[:spreadsheet_columns].is_a?(x)}
cols_method_name = options[:spreadsheet_columns]
if klass != SpreadsheetArchitect && !klass.instance_methods.include?(cols_method_name)
raise SpreadsheetArchitect::Exceptions::SpreadsheetColumnsNotDefinedError.new(klass, cols_method_name)
end
end
else
if klass != SpreadsheetArchitect && !klass.instance_methods.include?(:spreadsheet_columns)
if is_ar_model?(klass)
the_column_names = klass.column_names
= the_column_names.map{|x| str_titleize(x)} if
columns = the_column_names.map{|x| x.to_sym}
else
raise SpreadsheetArchitect::Exceptions::SpreadsheetColumnsNotDefinedError.new(klass)
end
end
end
data = []
options[:instances].each do |instance|
if columns
data.push columns.map{|col| col.is_a?(Symbol) ? instance.send(col) : col}
else
row_data = []
if options[:spreadsheet_columns]
if cols_method_name
instance_cols = instance.send(cols_method_name)
else
instance_cols = options[:spreadsheet_columns].call(instance)
end
else
if klass == SpreadsheetArchitect && !instance.respond_to?(:spreadsheet_columns)
raise SpreadsheetArchitect::Exceptions::SpreadsheetColumnsNotDefinedError.new(instance.class)
else
instance_cols = instance.spreadsheet_columns
end
end
instance_cols.each_with_index do |x,i|
if x.is_a?(Array)
.push(x[0].to_s) if
row_data.push(x[1].is_a?(Symbol) ? instance.send(x[1]) : x[1])
if needs_column_types
column_types[i] = x[2]
end
else
.push(str_titleize(x.to_s)) if
row_data.push(x.is_a?(Symbol) ? instance.send(x) : x)
end
end
data.push row_data
= false
needs_column_types = false
end
end
end
if && ![0].is_a?(Array)
= []
end
if column_types.compact.empty?
column_types = nil
end
return options.merge(headers: , data: data, column_types: column_types)
end
|