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.



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

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
  @data = block || column_name.to_sym
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#reportObject (readonly)

Returns the value of attribute report.



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

def report
  @report
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#has_total?Boolean

Returns:

  • (Boolean)


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

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

#humanizeObject



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

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

#only_on_web?Boolean

Returns:

  • (Boolean)


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

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

#sortable?Boolean

Returns:

  • (Boolean)


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

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

#totalObject



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

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

#value(record) ⇒ Object



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

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