Module: ClarkKent::Reportable

Included in:
ReportConfig
Defined in:
app/models/clark_kent/reportable.rb

Instance Method Summary collapse

Instance Method Details

#above_number_arel(query, field_name, match_value) ⇒ Object



144
145
146
147
# File 'app/models/clark_kent/reportable.rb', line 144

def above_number_arel(query, field_name, match_value)
	query.
	where("#{self.table_name}.#{field_name.to_s.sub(/_above/,'')} >= :lower_limit", lower_limit: match_value)
end

#after_date_arel(query, field_name, match_value) ⇒ Object



139
140
141
142
# File 'app/models/clark_kent/reportable.rb', line 139

def after_date_arel(query, field_name, match_value)
	query.
	where("#{self.table_name}.#{field_name.to_s.sub(/_from/,'')} >= :date_limit", date_limit: match_value)
end

#arel_method_for(param_type) ⇒ Object



123
124
125
126
127
# File 'app/models/clark_kent/reportable.rb', line 123

def arel_method_for(param_type)
  method_name = self.arel_methods[param_type.to_s]
  method_name ||= "#{param_type}_arel" if self.respond_to? "#{param_type}_arel"
  method_name
end

#before_date_arel(query, field_name, match_value) ⇒ Object



134
135
136
137
# File 'app/models/clark_kent/reportable.rb', line 134

def before_date_arel(query, field_name, match_value)
	query.
	where("#{self.table_name}.#{field_name.to_s.sub(/_until/,'')} <= :date_limit", date_limit: match_value)
end

#below_number_arel(query, field_name, match_value) ⇒ Object



149
150
151
152
# File 'app/models/clark_kent/reportable.rb', line 149

def below_number_arel(query, field_name, match_value)
	query.
	where("#{self.table_name}.#{field_name.to_s.sub(/_below/,'')} <= :upper_limit", upper_limit: match_value)
end

#chain_up(query, params) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'app/models/clark_kent/reportable.rb', line 3

def chain_up(query, params)
  params.each do |key,val|
    arel_method_name = self.arel_method_for(key)
    if arel_method_name.present? and self.respond_to? arel_method_name and val.present?
      query = self.send(arel_method_name, query, key, val)
    end
  end
  query = query.distinct if params[:distinct]
  query
end

#column_options_for(column_name) ⇒ Object



119
120
121
# File 'app/models/clark_kent/reportable.rb', line 119

def column_options_for(column_name)
self.report_column_options.detect{|co| column_name == co.name}
end

#order_arel(query, field_name, match_value) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/models/clark_kent/reportable.rb', line 154

def order_arel(query, field_name, match_value)
	if match_value.is_a? ClarkKent::ReportSort
 	order_column = match_value.order_column
 	order_direction = match_value.order_direction
 else
 	order_column, order_direction = match_value.split('-')
 end
column_info = column_options_for(order_column.to_sym)
if column_info.respond_to?(:order_sql) && column_info.order_sql.present?
  order_sql = column_info.order_sql
 	order_sql = "#{order_sql} #{order_direction}"
 	query = query.order(order_sql)
 	if column_info.respond_to? :includes
 		order_includes = column_info.includes
			query = query.includes(order_includes).references(order_includes)
		end
		query
 else
 	query
 end
end

#report(params, report, count = false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/clark_kent/reportable.rb', line 28

def report(params,report,count = false)
	@selects = []
	@includes = []
	@joins = []
	@extra_scopes = []
	@extra_filters = []
	@groups = []
	if 'ClarkKent::ReportEmail' == report.class.name
		@report_email = report
		report = @report_email.report
	end
	if count == false
	report.select_clauses.each do |select_clause|
		@selects.push select_clause
  end
end
  report.arel_includes.each do |arel_include|
  	@includes.push arel_include
  end
  report.arel_joins.each do |arel_join|
  	@joins.push arel_join
  end
  report.extra_scopes.each do |extra_scope|
  	@extra_scopes.push extra_scope
  end
  report.extra_filters.each do |extra_filter|
  	@extra_filters.push extra_filter
  end
if count == false
   report.groups.each do |grouper|
   	@groups.push grouper
   end
 end
  query = self.all
  if @report_email and @report_email.is_a? ClarkKent::ReportEmail
	params = @report_email.report_filter_params.symbolize_keys!.merge(params.symbolize_keys)
  else
	params = report.report_filter_params.symbolize_keys!.merge(params.symbolize_keys)
end
	validate_params(params, report)
  params.each do |param_type,param_value|
    if param_value.present?
       arel_method_name = self.arel_method_for(param_type)
      if arel_method_name.present?
        query = self.send(arel_method_name, query, param_type, param_value)
        if report_column_options = column_options_for(param_type)
         if(report_column_options.joins.present?) && (@joins.exclude? report_column_options.joins)
         	@joins.push report_column_options.joins
         end
				if(report_column_options.includes.present?) && (@includes.exclude? report_column_options.includes)
					@includes.push report_column_options.includes
         end
         if (count == false) && (report_column_options.custom_select.present?) && (@selects.exclude? report_column_options.custom_select)
         	@selects.push report_column_options.custom_select
         end
       end
      end
    end
  end
  if @selects.any?
  	query = query.select("DISTINCT " + self.column_names.map{|cn| self.table_name + '.' + cn}.join(', '))
  	@selects.uniq.each do |selectable|
   	query = query.select(selectable)
   end
 end
 if self.respond_to? :clark_kent_required_filters
 	query = self.send(:clark_kent_required_filters, query)
 end
 	@includes.uniq.each do |includeable|
  	query = query.includes(includeable)
  end if @includes.any?
 	@extra_scopes.uniq.each do |extra_scope|
  	query = query.send(extra_scope)
  end if @extra_scopes.any?
 	@extra_filters.uniq.each do |extra_filter|
  	query = query.where(extra_filter)
  end if @extra_filters.any?
 	@joins.uniq.each do |joinable|
  	query = query.joins(joinable).uniq
  end if @joins.any?
 	@groups.uniq.each do |grouper|
  	query = query.group(grouper)
  end if @groups.any?
  if count == true
  	return query.count
else
	return query
end

end

#required_date_paramsObject



14
15
16
# File 'app/models/clark_kent/reportable.rb', line 14

def required_date_params
	self.report_filter_options.select{|rfo| rfo.in_required_date_group}.map{|rfo| rfo.filter_params}.flatten.map(&:to_sym)
end

#simple_equality_arel(query, field_name, match_value) ⇒ Object



129
130
131
132
# File 'app/models/clark_kent/reportable.rb', line 129

def simple_equality_arel(query, field_name, match_value)
  query.
  where(field_name.to_sym => match_value)
end

#validate_params(params, report) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'app/models/clark_kent/reportable.rb', line 18

def validate_params(params,report)
	if required_date_params.any?
		missing_params = required_date_params - params.select{|k,v| v.present? }.symbolize_keys.keys
		# a bit clunky, it only requires any 2 date filters. It would be better to require at least one pair of before/after filters
		if missing_params.length > (required_date_params.length - 2)
  	raise ClarkKent::ReportFilterError.new("At least one date range is required.")
  end
end
end