Class: QueryReport::ColumnModule::Column

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::SanitizeHelper
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.



51
52
53
54
55
# File 'lib/query_report/column.rb', line 51

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.



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

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#reportObject (readonly)

Returns the value of attribute report.



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

def report
  @report
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#alignObject



90
91
92
# File 'lib/query_report/column.rb', line 90

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

#has_total?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/query_report/column.rb', line 86

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

#humanizeObject



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

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

#only_on_web?Boolean

Returns:

  • (Boolean)


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

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

#rowspan?Boolean

Returns:

  • (Boolean)


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

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

#rowspan_column_humanizedObject



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

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)


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

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

#totalObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/query_report/column.rb', line 94

def total
  @total ||= begin
    if has_total?
      report.records_with_rowspan.inject(0) do |sum, r|
        r = report.content_from_element(r[humanize])
        r = strip_tags(r) if r.kind_of? String
        sum + r.to_f
      end
    else
      nil
    end
  end
end

#value(record) ⇒ Object



82
83
84
# File 'lib/query_report/column.rb', line 82

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