Class: QueryReport::Report

Inherits:
Object
  • Object
show all
Includes:
ColumnModule, FilterModule
Defined in:
lib/query_report/report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FilterModule

#filter, #filter_with_values

Methods included from ColumnModule

#column, #column_names, #columns

Constructor Details

#initialize(params, template, options = {}, &block) ⇒ Report

Returns a new instance of Report.



15
16
17
18
19
20
21
# File 'lib/query_report/report.rb', line 15

def initialize(params, template, options={}, &block)
  @params, @template = params, template
  @columns, @filters, @scopes, @charts = [], [], [], []
  @current_scope = @params[:scope] || 'all'
  @options = QueryReport::DEFAULT_OPTIONS.merge options
  instance_eval &block if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

to support the helper methods



32
33
34
35
36
37
38
# File 'lib/query_report/report.rb', line 32

def method_missing(meth, *args, &block)
  if @template.respond_to?(meth)
    @template.send(meth, *args)
  else
    super
  end
end

Instance Attribute Details

#chartObject

Returns the value of attribute chart.



13
14
15
# File 'lib/query_report/report.rb', line 13

def chart
  @chart
end

#chartsObject

Returns the value of attribute charts.



13
14
15
# File 'lib/query_report/report.rb', line 13

def charts
  @charts
end

#current_scopeObject

Returns the value of attribute current_scope.



13
14
15
# File 'lib/query_report/report.rb', line 13

def current_scope
  @current_scope
end

#filtersObject

Returns the value of attribute filters.



13
14
15
# File 'lib/query_report/report.rb', line 13

def filters
  @filters
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/query_report/report.rb', line 13

def options
  @options
end

#paramsObject

Returns the value of attribute params.



13
14
15
# File 'lib/query_report/report.rb', line 13

def params
  @params
end

#scopesObject

Returns the value of attribute scopes.



13
14
15
# File 'lib/query_report/report.rb', line 13

def scopes
  @scopes
end

#templateObject

Returns the value of attribute template.



13
14
15
# File 'lib/query_report/report.rb', line 13

def template
  @template
end

Instance Method Details

#all_recordsObject



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

def all_records
  @cached_all_records ||= map_record(query_without_pagination)
end

#compare_with_column_chart(title, x_axis = '', &block) ⇒ Object



84
85
86
87
88
89
# File 'lib/query_report/report.rb', line 84

def compare_with_column_chart(title, x_axis='', &block)
  @chart = QueryReport::Chart::CustomChart.new(:column, title, query_without_pagination)
  @chart.add_column x_axis
  @chart.instance_eval &block if block_given?
  @charts << @chart
end

#map_record(query) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/query_report/report.rb', line 66

def map_record(query)
  query.clone.map do |record|
    array = @columns.collect { |column| [column.humanize,
                                         (column.data.kind_of?(Symbol) ? record.send(column.name) : column.data.call(record))] }
    Hash[*array.flatten]
  end
end

#model_nameObject



40
41
42
# File 'lib/query_report/report.rb', line 40

def model_name
  query.table.name.singularize
end

#pie_chart(title, &block) ⇒ Object



91
92
93
94
95
# File 'lib/query_report/report.rb', line 91

def pie_chart(title, &block)
  @chart = QueryReport::Chart::PieChart.new(title, query_without_pagination)
  @chart.instance_eval &block if block_given?
  @charts << @chart
end

#queryObject



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

def query
  apply_filters_and_pagination
  @query_cache
end

#query=(query) ⇒ Object



44
45
46
# File 'lib/query_report/report.rb', line 44

def query=(query)
  @query_cache = query
end

#query_without_paginationObject



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

def query_without_pagination
  apply_filters_and_pagination
  @query_without_pagination_cache
end

#recordsObject



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

def records
  @cached_records ||= map_record(query)
end

#scope(scope) ⇒ Object



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

def scope(scope)
  @scopes << scope
  @scopes = @scopes.uniq
end

#searchObject



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

def search
  apply_filters_and_pagination
  @search
end