Class: QueryReport::ColumnModule::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/query_report/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(report, column_name, options = {}, block = nil) ⇒ Column

Returns a new instance of Column.



49
50
51
52
53
# File 'lib/query_report/column.rb', line 49

def initialize(report, column_name, options={}, block = nil)
  @report, @name, @options = report, column_name, options
  @type = @report.model_class.columns_hash[column_name.to_s].try(:type) || options[:type] || :string rescue :string
  @data = block || column_name.to_sym
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



47
48
49
# File 'lib/query_report/column.rb', line 47

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



47
48
49
# File 'lib/query_report/column.rb', line 47

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



47
48
49
# File 'lib/query_report/column.rb', line 47

def options
  @options
end

#reportObject (readonly)

Returns the value of attribute report.



47
48
49
# File 'lib/query_report/column.rb', line 47

def report
  @report
end

#typeObject (readonly)

Returns the value of attribute type.



47
48
49
# File 'lib/query_report/column.rb', line 47

def type
  @type
end

Instance Method Details

#alignObject



88
89
90
# File 'lib/query_report/column.rb', line 88

def align
  @options[:align] || (has_total? ? :right : :left)
end

#has_total?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/query_report/column.rb', line 84

def has_total?
  @options[:show_total] == true
end

#humanizeObject



76
77
78
# File 'lib/query_report/column.rb', line 76

def humanize
  @humanize ||= options[:as] || @report.model_class.human_attribute_name(name)
end

#only_on_web?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/query_report/column.rb', line 55

def only_on_web?
  @options[:only_on_web] == true
end

#rowspan?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/query_report/column.rb', line 63

def rowspan?
  @options[:rowspan] == true || @options[:rowspan].kind_of?(Symbol)
end

#rowspan_column_humanizedObject



67
68
69
70
71
72
73
74
# File 'lib/query_report/column.rb', line 67

def rowspan_column_humanized
  rowspan_column_name = @options[:rowspan].kind_of?(Symbol) ? @options[:rowspan] : self.name

  report.columns.each do |column|
    return column.humanize if column.name == rowspan_column_name
  end
  return self.humanize
end

#sortable?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/query_report/column.rb', line 59

def sortable?
  @options[:sortable] == true
end

#totalObject



92
93
94
# File 'lib/query_report/column.rb', line 92

def total
  @total ||= has_total? ? report.records.inject(0) { |sum, r| sum + report.content_from_element(r[humanize]).to_f } : nil
end

#value(record) ⇒ Object



80
81
82
# File 'lib/query_report/column.rb', line 80

def value(record)
  self.data.kind_of?(Symbol) ? (record.respond_to?(self.name) ? record.send(self.name) : record[self.name]) : self.data.call(record)
end