clark_kent 0.9.2 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fae2d9e072d39d8cd9d717477ed9ecf888230b22
4
- data.tar.gz: 666e6483c2a7ff766b5aed1d2d6bf962d3734958
3
+ metadata.gz: cff67d05d931728a4d59a4a6fda5228dbd06eeff
4
+ data.tar.gz: b9d6e364e1cdd574c34f995b1a9434847886a476
5
5
  SHA512:
6
- metadata.gz: 4702f55ad2e1dc631f7eb53cb6dfb37f32d757958843aad7b22f68dcdf90959da3bc401defbb2e6242b2b0ca8cc701aa8611edd516a351a8e0f6f948d9ca29c6
7
- data.tar.gz: a1488c26ef2c42e4c49f7fd7b3b32d176fedb6487b82131f21b18a9f1ce8430f5c88e3c93e804b236fd217943ed4c54df5542b1345625af4361a7c6456ed267e
6
+ metadata.gz: f50b2f886b9d98d75cf32a5d854d3e2460173655834233764c80252504d8d217280fe8139cce95eb2b49bac5fd5893cc9759aa2da0f3461220db67a88bf9fc9e
7
+ data.tar.gz: 00600cad919cf0694d33cbd53e8ac4922078176c55279f3dd494c9edbb2a99b58cfc3a1a73a0f34d4dd28f383555f15e69b56f2d6f408c859cacc1a3e65281c2
@@ -177,11 +177,11 @@ require 'aws-sdk-v1'
177
177
  end
178
178
 
179
179
  def filter_options_for(filter_name)
180
- self.resource_class::REPORT_FILTER_OPTIONS.detect{|filter| filter.param == filter_name}
180
+ self.resource_class.report_filter_options.detect{|filter| filter.param == filter_name}
181
181
  end
182
182
 
183
183
  def column_options
184
- @column_options ||= self.resource_class::REPORT_COLUMN_OPTIONS
184
+ @column_options ||= self.resource_class.report_column_options
185
185
  end
186
186
 
187
187
  def column_options_for(column_name)
@@ -200,7 +200,7 @@ require 'aws-sdk-v1'
200
200
  end
201
201
 
202
202
  def date_filter_names
203
- self.resource_class::REPORT_FILTER_OPTIONS.select{|filter| 'date_filter' == filter.kind}.map{|filter| filter.param}
203
+ self.resource_class.report_filter_options.select{|filter| 'date_filter' == filter.kind}.map{|filter| filter.param}
204
204
  end
205
205
 
206
206
  ## These are the filters available for defining a report for this resource. They do not include date
@@ -210,23 +210,23 @@ require 'aws-sdk-v1'
210
210
  end
211
211
 
212
212
  def available_filter_options
213
- self.available_filters.map{|id| [self.filter_options_for(id).label,id]}
213
+ self.available_filters.map{|filter_name| [self.filter_options_for(filter_name).label,filter_name]}
214
214
  end
215
215
 
216
216
  ## This is the full set of filter options for defining a report, including the date filters for
217
217
  ## an automatic, timed, emailed report.
218
218
  def available_email_filters
219
- self.resource_class::REPORT_DEFINITION_OPTIONS.reject{|name| (viable_report_filters.map(&:filter_name)).include? name}
219
+ self.resource_class.report_definition_options.reject{|name| (viable_report_filters.map(&:filter_name)).include? name}
220
220
  end
221
221
 
222
222
  def collection_for(filter_name)
223
- self.resource_class::REPORT_FILTER_OPTIONS.detect{|filter| filter.param == filter_name}.collection
223
+ self.filter_options_for(filter_name).collection
224
224
  end
225
225
 
226
226
  ## These are the filters available at runtime, ie. not including the ones set to define this report.
227
227
  ## If updating the report, this is the set available to add as new report definition filters.
228
228
  def custom_filters
229
- self.resource_class::REPORT_FILTER_OPTIONS.select{|filter| viable_report_filters.map(&:filter_name).exclude? filter.param}
229
+ self.resource_class.report_filter_options.select{|filter| viable_report_filters.map(&:filter_name).exclude? filter.param}
230
230
  end
231
231
 
232
232
  ## This is the set of columns not chosed to use in the report. These are the ones available to add
@@ -253,7 +253,7 @@ require 'aws-sdk-v1'
253
253
  end
254
254
 
255
255
  def get_filter_class(params)
256
- filter_option = self.resource_class::REPORT_FILTER_OPTIONS.detect{|filter| filter.param == params[:filter_name]}
256
+ filter_option = self.resource_class.report_filter_options.detect{|filter| filter.param == params[:filter_name]}
257
257
  "ClarkKent::Report#{filter_option.kind.camelcase}".constantize
258
258
  end
259
259
 
@@ -1,19 +1,15 @@
1
1
  module ClarkKent
2
2
  module ReportConfig
3
- module ClassMethods
4
- def filter_config(params)
5
- filter_option_class = ('ClarkKent::' + ((params[:kind] + '_option').camelcase)).constantize
6
- filter_option_class.new(params)
7
- end
8
-
9
- def column_config(params)
10
- ClarkKent::ReportColumnConfig.new(params)
11
- end
3
+ include ClarkKent::Reportable
4
+ def filter_config(params)
5
+ filter_option_class = ('ClarkKent::' + ((params[:kind] + '_option').camelcase)).constantize
6
+ filter_option_class.new(params)
12
7
  end
13
- extend ClassMethods
14
- def self.included( other )
15
- other.extend( ClassMethods )
8
+
9
+ def column_config(params)
10
+ ClarkKent::ReportColumnConfig.new(params)
16
11
  end
12
+
17
13
  end
18
14
  class ReportColumnConfig
19
15
  attr_accessor :name, :order_sql, :custom_select, :link, :time_zone_column, :time_format, :summarizable, :includes, :joins, :extra_scopes, :where, :group
@@ -86,7 +86,7 @@ module ClarkKent
86
86
  end
87
87
 
88
88
  def available_email_filters
89
- self.resource_class::REPORT_DEFINITION_OPTIONS.reject{|name, label| (self.viable_report_filters.pluck(:filter_name) + self.report.viable_report_filters.pluck(:filter_name)).include? name}
89
+ self.resource_class.report_definition_options.reject{|name, label| (self.viable_report_filters.pluck(:filter_name) + self.report.viable_report_filters.pluck(:filter_name)).include? name}
90
90
  end
91
91
 
92
92
  def available_filters
@@ -1,182 +1,174 @@
1
1
  module ClarkKent
2
2
  module Reportable
3
- def self.included(base)
4
- base.extend ClassMethods
3
+ def chain_up(query, params)
4
+ params.each do |key,val|
5
+ arel_method_name = self.arel_method_for(key)
6
+ if arel_method_name.present? and self.respond_to? arel_method_name and val.present?
7
+ query = self.send(arel_method_name, query, key, val)
8
+ end
9
+ end
10
+ query = query.distinct if params[:distinct]
11
+ query
5
12
  end
6
13
 
7
- module ClassMethods
8
-
9
- def chain_up(query, params)
10
- params.each do |key,val|
11
- arel_method_name = self.arel_method_for(key)
12
- if arel_method_name.present? and self.respond_to? arel_method_name and val.present?
13
- query = self.send(arel_method_name, query, key, val)
14
- end
15
- end
16
- query = query.distinct if params[:distinct]
17
- query
18
- end
14
+ def required_date_params
15
+ self.report_filter_options.select{|rfo| rfo.in_required_date_group}.map{|rfo| rfo.filter_params}.flatten.map(&:to_sym)
16
+ end
19
17
 
20
- def required_date_params
21
- self::REPORT_FILTER_OPTIONS.select{|rfo| rfo.in_required_date_group}.map{|rfo| rfo.filter_params}.flatten.map(&:to_sym)
22
- end
18
+ def validate_params(params,report)
19
+ if required_date_params.any?
20
+ missing_params = required_date_params - params.select{|k,v| v.present? }.symbolize_keys.keys
21
+ # a bit clunky, it only requires any 2 date filters. It would be better to require at least one pair of before/after filters
22
+ if missing_params.length > (required_date_params.length - 2)
23
+ raise ClarkKent::ReportFilterError.new("At least one date range is required.")
24
+ end
25
+ end
26
+ end
23
27
 
24
- def validate_params(params,report)
25
- if required_date_params.any?
26
- missing_params = required_date_params - params.select{|k,v| v.present? }.symbolize_keys.keys
27
- # a bit clunky, it only requires any 2 date filters. It would be better to require at least one pair of before/after filters
28
- if missing_params.length > (required_date_params.length - 2)
29
- raise ClarkKent::ReportFilterError.new("At least one date range is required.")
30
- end
28
+ def report(params,report,count = false)
29
+ @selects = []
30
+ @includes = []
31
+ @joins = []
32
+ @extra_scopes = []
33
+ @extra_filters = []
34
+ @groups = []
35
+ if 'ClarkKent::ReportEmail' == report.class.name
36
+ @report_email = report
37
+ report = @report_email.report
38
+ end
39
+ if count == false
40
+ report.select_clauses.each do |select_clause|
41
+ @selects.push select_clause
31
42
  end
43
+ end
44
+ report.arel_includes.each do |arel_include|
45
+ @includes.push arel_include
32
46
  end
33
-
34
- def report(params,report,count = false)
35
- @selects = []
36
- @includes = []
37
- @joins = []
38
- @extra_scopes = []
39
- @extra_filters = []
40
- @groups = []
41
- if 'ClarkKent::ReportEmail' == report.class.name
42
- @report_email = report
43
- report = @report_email.report
44
- end
45
- if count == false
46
- report.select_clauses.each do |select_clause|
47
- @selects.push select_clause
48
- end
49
- end
50
- report.arel_includes.each do |arel_include|
51
- @includes.push arel_include
52
- end
53
- report.arel_joins.each do |arel_join|
54
- @joins.push arel_join
55
- end
56
- report.extra_scopes.each do |extra_scope|
57
- @extra_scopes.push extra_scope
58
- end
59
- report.extra_filters.each do |extra_filter|
60
- @extra_filters.push extra_filter
61
- end
62
- report.groups.each do |grouper|
63
- @groups.push grouper
64
- end
65
- query = self.all
66
- if @report_email and @report_email.is_a? ClarkKent::ReportEmail
67
- params = @report_email.report_filter_params.symbolize_keys!.merge(params.symbolize_keys)
68
- else
69
- params = report.report_filter_params.symbolize_keys!.merge(params.symbolize_keys)
70
- end
71
- validate_params(params, report)
72
- params.each do |param_type,param_value|
73
- if param_value.present?
74
- arel_method_name = self.arel_method_for(param_type)
75
- if arel_method_name.present?
76
- query = self.send(arel_method_name, query, param_type, param_value)
77
- if report_column_options = column_options_for(param_type)
78
- if(report_column_options.joins.present?) && (@joins.exclude? report_column_options.joins)
79
- @joins.push report_column_options.joins
80
- end
81
- if(report_column_options.includes.present?) && (@includes.exclude? report_column_options.includes)
82
- @includes.push report_column_options.includes
83
- end
84
- if (count == false) && (report_column_options.custom_select.present?) && (@selects.exclude? report_column_options.custom_select)
85
- @selects.push report_column_options.custom_select
86
- end
87
- end
47
+ report.arel_joins.each do |arel_join|
48
+ @joins.push arel_join
49
+ end
50
+ report.extra_scopes.each do |extra_scope|
51
+ @extra_scopes.push extra_scope
52
+ end
53
+ report.extra_filters.each do |extra_filter|
54
+ @extra_filters.push extra_filter
55
+ end
56
+ report.groups.each do |grouper|
57
+ @groups.push grouper
58
+ end
59
+ query = self.all
60
+ if @report_email and @report_email.is_a? ClarkKent::ReportEmail
61
+ params = @report_email.report_filter_params.symbolize_keys!.merge(params.symbolize_keys)
62
+ else
63
+ params = report.report_filter_params.symbolize_keys!.merge(params.symbolize_keys)
64
+ end
65
+ validate_params(params, report)
66
+ params.each do |param_type,param_value|
67
+ if param_value.present?
68
+ arel_method_name = self.arel_method_for(param_type)
69
+ if arel_method_name.present?
70
+ query = self.send(arel_method_name, query, param_type, param_value)
71
+ if report_column_options = column_options_for(param_type)
72
+ if(report_column_options.joins.present?) && (@joins.exclude? report_column_options.joins)
73
+ @joins.push report_column_options.joins
74
+ end
75
+ if(report_column_options.includes.present?) && (@includes.exclude? report_column_options.includes)
76
+ @includes.push report_column_options.includes
77
+ end
78
+ if (count == false) && (report_column_options.custom_select.present?) && (@selects.exclude? report_column_options.custom_select)
79
+ @selects.push report_column_options.custom_select
80
+ end
88
81
  end
89
- end
82
+ end
83
+ end
84
+ end
85
+ if @selects.any?
86
+ query = query.select("DISTINCT " + self.column_names.map{|cn| self.table_name + '.' + cn}.join(', '))
87
+ @selects.uniq.each do |selectable|
88
+ query = query.select(selectable)
90
89
  end
91
- if @selects.any?
92
- query = query.select("DISTINCT " + self.column_names.map{|cn| self.table_name + '.' + cn}.join(', '))
93
- @selects.uniq.each do |selectable|
94
- query = query.select(selectable)
95
- end
96
- end
97
- if self.respond_to? :clark_kent_required_filters
98
- query = self.send(:clark_kent_required_filters, query)
99
- end
100
- @includes.uniq.each do |includeable|
101
- query = query.includes(includeable)
102
- end if @includes.any?
103
- @extra_scopes.uniq.each do |extra_scope|
104
- query = query.send(extra_scope)
105
- end if @extra_scopes.any?
106
- @extra_filters.uniq.each do |extra_filter|
107
- query = query.where(extra_filter)
108
- end if @extra_filters.any?
109
- @joins.uniq.each do |joinable|
110
- query = query.joins(joinable).uniq
111
- end if @joins.any?
112
- @groups.uniq.each do |grouper|
113
- query = query.group(grouper)
114
- end if @groups.any?
115
- if count == true
116
- return query.count
117
- else
118
- return query
119
- end
120
-
121
90
  end
122
-
123
- def column_options_for(column_name)
124
- self::REPORT_COLUMN_OPTIONS.detect{|co| column_name == co.name}
91
+ if self.respond_to? :clark_kent_required_filters
92
+ query = self.send(:clark_kent_required_filters, query)
125
93
  end
94
+ @includes.uniq.each do |includeable|
95
+ query = query.includes(includeable)
96
+ end if @includes.any?
97
+ @extra_scopes.uniq.each do |extra_scope|
98
+ query = query.send(extra_scope)
99
+ end if @extra_scopes.any?
100
+ @extra_filters.uniq.each do |extra_filter|
101
+ query = query.where(extra_filter)
102
+ end if @extra_filters.any?
103
+ @joins.uniq.each do |joinable|
104
+ query = query.joins(joinable).uniq
105
+ end if @joins.any?
106
+ @groups.uniq.each do |grouper|
107
+ query = query.group(grouper)
108
+ end if @groups.any?
109
+ if count == true
110
+ return query.count
111
+ else
112
+ return query
113
+ end
126
114
 
127
- def arel_method_for(param_type)
128
- method_name = self::AREL_METHODS[param_type.to_s]
129
- method_name ||= "#{param_type}_arel" if self.respond_to? "#{param_type}_arel"
130
- method_name
131
- end
115
+ end
132
116
 
133
- def simple_equality_arel(query, field_name, match_value)
134
- query.
135
- where(field_name.to_sym => match_value)
136
- end
117
+ def column_options_for(column_name)
118
+ self.report_column_options.detect{|co| column_name == co.name}
119
+ end
137
120
 
138
- def before_date_arel(query, field_name, match_value)
139
- query.
140
- where("#{self.table_name}.#{field_name.to_s.sub(/_until/,'')} <= :date_limit", date_limit: match_value)
141
- end
121
+ def arel_method_for(param_type)
122
+ method_name = self.arel_methods[param_type.to_s]
123
+ method_name ||= "#{param_type}_arel" if self.respond_to? "#{param_type}_arel"
124
+ method_name
125
+ end
142
126
 
143
- def after_date_arel(query, field_name, match_value)
144
- query.
145
- where("#{self.table_name}.#{field_name.to_s.sub(/_from/,'')} >= :date_limit", date_limit: match_value)
146
- end
127
+ def simple_equality_arel(query, field_name, match_value)
128
+ query.
129
+ where(field_name.to_sym => match_value)
130
+ end
147
131
 
148
- def above_number_arel(query, field_name, match_value)
149
- query.
150
- where("#{self.table_name}.#{field_name.to_s.sub(/_above/,'')} >= :lower_limit", lower_limit: match_value)
151
- end
132
+ def before_date_arel(query, field_name, match_value)
133
+ query.
134
+ where("#{self.table_name}.#{field_name.to_s.sub(/_until/,'')} <= :date_limit", date_limit: match_value)
135
+ end
152
136
 
153
- def below_number_arel(query, field_name, match_value)
154
- query.
155
- where("#{self.table_name}.#{field_name.to_s.sub(/_below/,'')} <= :upper_limit", upper_limit: match_value)
156
- end
137
+ def after_date_arel(query, field_name, match_value)
138
+ query.
139
+ where("#{self.table_name}.#{field_name.to_s.sub(/_from/,'')} >= :date_limit", date_limit: match_value)
140
+ end
157
141
 
158
- def order_arel(query, field_name, match_value)
159
- if match_value.is_a? ClarkKent::ReportSort
160
- order_column = match_value.order_column
161
- order_direction = match_value.order_direction
162
- else
163
- order_column, order_direction = match_value.split('-')
164
- end
165
- column_info = column_options_for(order_column.to_sym)
166
- if column_info.respond_to?(:order_sql) && column_info.order_sql.present?
167
- order_sql = column_info.order_sql
168
- order_sql = "#{order_sql} #{order_direction}"
169
- query = query.order(order_sql)
170
- if column_info.respond_to? :includes
171
- order_includes = column_info.includes
172
- query = query.includes(order_includes).references(order_includes)
173
- end
174
- query
175
- else
176
- query
177
- end
178
- end
142
+ def above_number_arel(query, field_name, match_value)
143
+ query.
144
+ where("#{self.table_name}.#{field_name.to_s.sub(/_above/,'')} >= :lower_limit", lower_limit: match_value)
145
+ end
146
+
147
+ def below_number_arel(query, field_name, match_value)
148
+ query.
149
+ where("#{self.table_name}.#{field_name.to_s.sub(/_below/,'')} <= :upper_limit", upper_limit: match_value)
150
+ end
179
151
 
180
- end
152
+ def order_arel(query, field_name, match_value)
153
+ if match_value.is_a? ClarkKent::ReportSort
154
+ order_column = match_value.order_column
155
+ order_direction = match_value.order_direction
156
+ else
157
+ order_column, order_direction = match_value.split('-')
158
+ end
159
+ column_info = column_options_for(order_column.to_sym)
160
+ if column_info.respond_to?(:order_sql) && column_info.order_sql.present?
161
+ order_sql = column_info.order_sql
162
+ order_sql = "#{order_sql} #{order_direction}"
163
+ query = query.order(order_sql)
164
+ if column_info.respond_to? :includes
165
+ order_includes = column_info.includes
166
+ query = query.includes(order_includes).references(order_includes)
167
+ end
168
+ query
169
+ else
170
+ query
171
+ end
172
+ end
181
173
  end
182
174
  end
@@ -18,8 +18,6 @@
18
18
  label: false, include_blank: false %>
19
19
  </div>
20
20
  <div class="ih-row" <%= revealer_target_attrs("sharing_options") %>>
21
- <%= ClarkKent.user_class_name %>
22
-
23
21
  <%= f.input :sharing_scope_id, as: :hidden, input_html: {value: current_user.id, data: {foo: ClarkKent.user_class_name}}, wrapper_html: revealer_option("sharing_options", trigger: ClarkKent.user_class_name).merge(id: "sharing_options_#{ClarkKent.user_class_name}") %>
24
22
  <% ClarkKent::SharingScopeKind.custom_for_user(current_user).each do |sharing_scope_kind| %>
25
23
  <% if sharing_scope_kind.associated_containers_for(current_user).respond_to? :each %>