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

#has_total?Boolean

Returns:

  • (Boolean)


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

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

#humanizeObject



67
68
69
# File 'lib/query_report/column.rb', line 67

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
end

#sortable?Boolean

Returns:

  • (Boolean)


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

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

#totalObject



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

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

#value(record) ⇒ Object



71
72
73
# File 'lib/query_report/column.rb', line 71

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