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.



97
98
99
100
101
# File 'lib/query_report/column.rb', line 97

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.



95
96
97
# File 'lib/query_report/column.rb', line 95

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



95
96
97
# File 'lib/query_report/column.rb', line 95

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



95
96
97
# File 'lib/query_report/column.rb', line 95

def options
  @options
end

#reportObject (readonly)

Returns the value of attribute report.



95
96
97
# File 'lib/query_report/column.rb', line 95

def report
  @report
end

#typeObject (readonly)

Returns the value of attribute type.



95
96
97
# File 'lib/query_report/column.rb', line 95

def type
  @type
end

Instance Method Details

#alignObject



148
149
150
# File 'lib/query_report/column.rb', line 148

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

#has_total?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/query_report/column.rb', line 144

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

#humanizeObject



136
137
138
# File 'lib/query_report/column.rb', line 136

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

#only_on_web?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/query_report/column.rb', line 103

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

#pdf_optionsObject



119
120
121
# File 'lib/query_report/column.rb', line 119

def pdf_options
  @options[:pdf] || {}
end

#rowspan?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/query_report/column.rb', line 115

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

#rowspan_column_humanizedObject



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/query_report/column.rb', line 123

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

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


111
112
113
# File 'lib/query_report/column.rb', line 111

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

#sortable?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/query_report/column.rb', line 107

def sortable?
  @options[:sortable].present? && @options[:sortable] != false
end

#totalObject



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/query_report/column.rb', line 152

def total
  @total ||= begin
    if has_total?
      report.records_to_render.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



140
141
142
# File 'lib/query_report/column.rb', line 140

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