clark_kent 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,65 @@
1
+ require 'test_helper'
2
+
3
+ class ClarkKent::ReportableTest < ActiveSupport::TestCase
4
+ test "it adds required filters if present" do
5
+ report = ClarkKent::Report.create(name: 'test one', resource_type: 'Order')
6
+ query = Order.report({created_at_until: Date.today, created_at_from: Date.yesterday},report)
7
+ assert_match('orders.user_id > 0', query.to_sql)
8
+ end
9
+
10
+ test "it works without any required filters" do
11
+ report = ClarkKent::Report.create(name: 'test one', resource_type: 'TestReportable')
12
+ query = TestReportable.report({created_at_until: Date.today, created_at_from: Date.yesterday},report)
13
+ refute_match('orders.user_id > 0', query.to_sql)
14
+ assert_match('SELECT "orders".*', query.to_sql)
15
+ end
16
+ end
17
+
18
+ class TestReportable < ActiveRecord::Base
19
+
20
+ self.table_name = 'orders'
21
+
22
+ include ClarkKent::ReportConfig
23
+ include ClarkKent::Reportable
24
+
25
+ # This mapping tells ClarkKent how to use params for filtering a report. It is only used for pre-defined,
26
+ # standard filtering methods provided by ClarkKent: simple_equality_arel, order_arel, before_date_arel, and after_date_arel. Params that need custom filtering should define their own arel methods that follow
27
+ # the convention <param name>_arel(query, field_name, match_value). nb. ranges can be handled with dual params like date_before/date_after
28
+ # or amount_below/amount_above etc.
29
+ AREL_METHODS ||= {
30
+ 'user_id' => 'simple_equality_arel',
31
+ 'amount_above' => 'above_number_arel',
32
+ 'amount_below' => 'below_number_arel'
33
+ }
34
+
35
+ # These are the options for permanent filters built into a report. nb dates don't make sense as permanent report filters
36
+ # except in the case of ClarkKent::ReportEmails.
37
+ REPORT_DEFINITION_OPTIONS ||= ['user_id', 'created_at']
38
+
39
+ # This is the full set of report filter options for use at report runtime. REPORT_DEFINITION_OPTIONS must refer to some subset of these
40
+ REPORT_FILTER_OPTIONS ||= [
41
+ filter_config(kind: 'date_filter', param: 'created_at', in_required_date_group: true),
42
+ filter_config(kind: 'object_filter', param: 'user_id', collection: :users, label: 'user'),
43
+ filter_config(kind: 'string_filter', param: 'user_email')
44
+ ]
45
+
46
+ # These are the available column options for building reports from this resource
47
+ REPORT_COLUMN_OPTIONS ||= [
48
+ column_config(name: :user_name,
49
+ custom_select: "
50
+ (SELECT u.name
51
+ FROM users u
52
+ WHERE u.id = orders.user_id)
53
+ as user_name"),
54
+ column_config(name: :id, order_sql: 'orders.id', link: :order_path),
55
+ column_config(name: :amount, order_sql: 'orders.amount', summarizable: true),
56
+ column_config(name: :description, order_sql: 'orders.description')
57
+ ]
58
+
59
+ def self.user_email_arel(query, field_name, match_value)
60
+ query = query.
61
+ joins(:user).
62
+ where(users: {email: match_value})
63
+ end
64
+
65
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clark_kent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Draut
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-06 00:00:00.000000000 Z
11
+ date: 2015-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -310,6 +310,7 @@ files:
310
310
  - test/integration/navigation_test.rb
311
311
  - test/models/clark_kent/report_email_test.rb
312
312
  - test/models/clark_kent/report_test.rb
313
+ - test/models/clark_kent/reportable_test.rb
313
314
  - test/support/db_utils.rb
314
315
  - test/test_helper.rb
315
316
  homepage: https://github.com/edraut/clark-kent
@@ -431,5 +432,6 @@ test_files:
431
432
  - test/integration/navigation_test.rb
432
433
  - test/models/clark_kent/report_email_test.rb
433
434
  - test/models/clark_kent/report_test.rb
435
+ - test/models/clark_kent/reportable_test.rb
434
436
  - test/support/db_utils.rb
435
437
  - test/test_helper.rb