Class: ReportBuilder::Base
- Inherits:
-
Object
- Object
- ReportBuilder::Base
show all
- Defined in:
- lib/report_builder.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(report = nil) ⇒ Base
Returns a new instance of Base.
22
23
24
25
26
27
|
# File 'lib/report_builder.rb', line 22
def initialize(report=nil)
unless report.blank?
self.report = report
self.template = report.report_template
end
end
|
Instance Attribute Details
#report ⇒ Object
Returns the value of attribute report.
7
8
9
|
# File 'lib/report_builder.rb', line 7
def report
@report
end
|
#template ⇒ Object
Returns the value of attribute template.
8
9
10
|
# File 'lib/report_builder.rb', line 8
def template
@template
end
|
Class Method Details
.requires_dates! ⇒ Object
10
11
12
|
# File 'lib/report_builder.rb', line 10
def self.requires_dates!
@requires_dates = true
end
|
.requires_dates? ⇒ Boolean
14
15
16
|
# File 'lib/report_builder.rb', line 14
def self.requires_dates?
!!@requires_dates
end
|
Instance Method Details
#arelize(field) ⇒ Object
33
34
35
36
37
38
39
40
|
# File 'lib/report_builder.rb', line 33
def arelize(field)
field = field.split('.')
table = tables[field.first]
raise "Table `#{field.first}' not found for `#{template.kind}' report kind" if table.blank?
table[field.last.to_sym]
end
|
#human_calculation_name(field) ⇒ Object
108
109
110
|
# File 'lib/report_builder.rb', line 108
def human_calculation_name(field)
I18n.t "smartkiosk.reports.data.#{keyword}.calculations.#{field}"
end
|
#human_calculation_names ⇒ Object
124
125
126
|
# File 'lib/report_builder.rb', line 124
def human_calculation_names
calculations.keys.map{|x| [human_calculation_name(x), x]}
end
|
#human_condition_name(field) ⇒ Object
112
113
114
|
# File 'lib/report_builder.rb', line 112
def human_condition_name(field)
I18n.t "smartkiosk.reports.data.#{keyword}.conditions.#{field}"
end
|
#human_condition_values(condition) ⇒ Object
128
129
130
|
# File 'lib/report_builder.rb', line 128
def human_condition_values(condition)
conditions[condition].call().with_indifferent_access
end
|
#human_field_name(field) ⇒ Object
89
90
91
92
93
94
95
96
97
|
# File 'lib/report_builder.rb', line 89
def human_field_name(field)
model, name = field.split '.'
title = []
title << I18n.t("activerecord.models.#{model}", :count => 1)
title << I18n.t("activerecord.attributes.#{model}.#{name}")
title.join(': ')
end
|
#human_field_value(field, value) ⇒ Object
99
100
101
102
103
104
105
106
|
# File 'lib/report_builder.rb', line 99
def human_field_value(field, value)
return value unless respond_to?(:field_localizations)
@localizations ||= field_localizations
return value unless @localizations[field]
@localizations[field].call(value)
end
|
#human_groupping_field_names(groupping) ⇒ Object
120
121
122
|
# File 'lib/report_builder.rb', line 120
def human_groupping_field_names(groupping)
fields[groupping].map{|x| [human_field_name(x), x]}
end
|
#human_groupping_names ⇒ Object
116
117
118
|
# File 'lib/report_builder.rb', line 116
def human_groupping_names
groupping.map{|x| [human_field_name(x), x]}
end
|
#keyword ⇒ Object
29
30
31
|
# File 'lib/report_builder.rb', line 29
def keyword
self.class.name.underscore.gsub('_report', '')
end
|
#query ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/report_builder.rb', line 42
def query
query = context(report.start, report.finish)
aggregate = {
'postgresql' => 'MIN',
'oracle_enhanced' => 'MIN'
}[ActiveRecord::Base.configurations[Rails.env]['adapter']]
fields = template.fields.each_with_index do |field, i|
projection = arelize(field)
if !aggregate.blank? && !template.groupping.blank?
projection = Arel::Nodes::NamedFunction.new(
aggregate, [ projection ]
)
end
projection = projection.as("\"_#{i}\"")
query = query.project(projection)
end
order = arelize(template.sorting) unless template.sorting.blank?
order = order.desc if !order.blank? && template.sort_desc
query = query.group(arelize template.groupping) unless template.groupping.blank?
query = query.order(order) unless order.blank?
unless template.conditions.blank?
template.conditions.each do |column, value|
next if value.blank?
if value.is_a?(Array)
query = query.where(arelize(column).in(value))
else
query = query.where(arelize(column).eq(value))
end
end
end
unless template.calculations.blank?
template.calculations.select{|x| !x.blank?}.each do |calculation|
query = calculations[calculation].call(query, template)
end
end
query.to_sql
end
|
#requires_dates? ⇒ Boolean
18
19
20
|
# File 'lib/report_builder.rb', line 18
def requires_dates?
self.class.requires_dates?
end
|