Module: SpreadsheetArchitect::Helpers

Defined in:
lib/spreadsheet_architect.rb

Class Method Summary collapse

Class Method Details

.get_cell_data(options = {}, klass) ⇒ Object



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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/spreadsheet_architect.rb', line 56

def self.get_cell_data(options={}, klass)
  if klass.name == 'SpreadsheetArchitect'
    if !options[:data] || options[:data].empty?
      raise SpreadsheetArchitect::NoDataError
    end

    if options[:headers] && !options[:headers].empty?
      headers = options[:headers]
    else
      headers = false
    end
    
    data = options[:data]

    types = []
    data.first.each do |x|
      types.push self.get_type(x, nil)
    end
  else
    has_custom_columns = options[:spreadsheet_columns] || klass.instance_methods.include?(:spreadsheet_columns)

    if !options[:instances] && defined?(ActiveRecord) && klass.ancestors.include?(ActiveRecord::Base)
      options[:instances] = klass.where(nil).to_a # triggers the relation call, not sure how this works but it does
    end

    if !options[:instances] || options[:instances].empty?
      raise SpreadsheetArchitect::NoInstancesError
    end

    if has_custom_columns 
      headers = []
      columns = []
      types = []
      array = options[:spreadsheet_columns] || options[:instances].first.spreadsheet_columns
      array.each do |x|
        if x.is_a?(Array)
          headers.push x[0].to_s
          columns.push x[1]
          #types.push self.get_type(x[1], x[2])
          types.push self.get_type(x[1], nil)
        else
          headers.push str_humanize(x.to_s)
          columns.push x
          types.push self.get_type(x, nil)
        end
      end
    elsif !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)
      headers = 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)}
    else
      raise SpreadsheetArchitect::SpreadsheetColumnsNotDefined, klass
    end

    if options[:headers].nil?
      options[:headers] = klass::SPREADSHEET_OPTIONS[:headers] if defined?(klass::SPREADSHEET_OPTIONS)
      options[:headers] = SpreadsheetArchitect::SPREADSHEET_OPTIONS[:headers] if options[:headers].nil?
    end
    if options[:headers].nil? || options[:headers] == false
      headers = false
    end

    data = []
    options[:instances].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
  
    # Fixes missing types from symbol methods
    if has_custom_columns || options[:spreadsheet_columns]
      data.first.each_with_index do |x,i|
        if types[i] == :symbol
          types[i] = self.get_type(x, nil, true)
        end
      end
    end
  end

  return options.merge(headers: headers, data: data, types: types)
end

.get_options(options = {}, klass) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/spreadsheet_architect.rb', line 151

def self.get_options(options={}, klass)
  if options[:headers]
    if defined?(klass::SPREADSHEET_OPTIONS)
      header_style = SpreadsheetArchitect::SPREADSHEET_OPTIONS[:header_style].merge(klass::SPREADSHEET_OPTIONS[:header_style] || {})
    else
      header_style = SpreadsheetArchitect::SPREADSHEET_OPTIONS[:header_style]
    end
    
    if options[:header_style]
      header_style.merge!(options[:header_style])
    elsif options[:header_style] == false
      header_style = false
    end
  else
    header_style = false
  end

  if options[:row_style] == false
    row_style = false
  else
    if defined?(klass::SPREADSHEET_OPTIONS)
      row_style = SpreadsheetArchitect::SPREADSHEET_OPTIONS[:row_style].merge(klass::SPREADSHEET_OPTIONS[:row_style] || {})
    else
      row_style = SpreadsheetArchitect::SPREADSHEET_OPTIONS[:row_style]
    end

    if options[:row_style]
      row_style.merge!(options[:row_style])
    end
  end

  if defined?(klass::SPREADSHEET_OPTIONS)
    sheet_name = options[:sheet_name] || klass::SPREADSHEET_OPTIONS[:sheet_name] || SpreadsheetArchitect::SPREADSHEET_OPTIONS[:sheet_name] || klass.name
  else
    sheet_name = options[:sheet_name] || SpreadsheetArchitect::SPREADSHEET_OPTIONS[:sheet_name] || klass.name
  end

  return {headers: options[:headers], header_style: header_style, row_style: row_style, types: options[:types], sheet_name: sheet_name, data: options[:data]}
end

.get_type(value, type = nil, last_run = false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/spreadsheet_architect.rb', line 40

def self.get_type(value, type=nil, last_run=false)
  return type if !type.blank?
  if value.is_a?(Numeric)
    if [:float, :decimal].include?(type)
      type = :float
    else
      type = :integer
    end
  elsif !last_run && value.is_a?(Symbol)
    type = :symbol
  else
    type = :string
  end
  return type
end

.str_humanize(str, capitalize = true) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/spreadsheet_architect.rb', line 32

def self.str_humanize(str, capitalize = true)
  str = str.sub(/\A_+/, '').gsub(/[_\.]/,' ').sub(' rescue nil','')
  if capitalize
    str = str.gsub(/(\A|\ )\w/){|x| x.upcase}
  end
  return str
end