Module: SpreadsheetArchitect::Helpers

Defined in:
lib/spreadsheet_architect.rb

Class Method Summary collapse

Class Method Details

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



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
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
# File 'lib/spreadsheet_architect.rb', line 50

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)
    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)}
  elsif has_custom_columns
    headers = []
    columns = []
    types = []
    array = options[:spreadsheet_columns] || options[:data].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
  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
  
  # 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

  if options[:headers] == false || klass::SPREADSHEET_OPTIONS[:headers] == false
    headers = false
  end

  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

  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

  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: headers, header_style: header_style, row_style: row_style, types: types, sheet_name: sheet_name, data: data}
end

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/spreadsheet_architect.rb', line 34

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



26
27
28
29
30
31
32
# File 'lib/spreadsheet_architect.rb', line 26

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