Module: SpreadsheetArchitect::Helpers

Defined in:
lib/spreadsheet_architect.rb

Class Method Summary collapse

Class Method Details

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



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)
    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])
      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

  headers = (options[:headers] == false ? false : headers)

  header_style = {background_color: "AAAAAA", color: "FFFFFF", align: :center, bold: false, font_name: 'Arial', font_size: 10, italic: false, underline: false}
  
  if options[:header_style]
    header_style.merge!(options[:header_style])
  elsif options[:header_style] == false
    header_style = 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: headers, header_style: header_style, row_style: row_style, types: types, sheet_name: sheet_name, data: data}
end

.get_type(value, type = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/spreadsheet_architect.rb', line 34

def self.get_type(value, type=nil)
  #return type if !type.blank?
  if value.is_a?(Numeric) || [:integer, :float, :decimal].include?(type)
    type = :float
  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