clark_kent 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/clark_kent/application.js +18291 -0
  3. data/app/assets/stylesheets/clark_kent/application.css +21 -0
  4. data/app/controllers/clark_kent/report_columns_controller.rb +6 -3
  5. data/app/controllers/clark_kent/report_emails_controller.rb +7 -3
  6. data/app/controllers/clark_kent/report_filters_controller.rb +12 -4
  7. data/app/controllers/clark_kent/reports_controller.rb +21 -8
  8. data/app/controllers/clark_kent/user_report_emails_controller.rb +7 -3
  9. data/app/models/clark_kent/cloneable.rb +18 -0
  10. data/app/models/clark_kent/date_filter_option.rb +7 -0
  11. data/app/models/clark_kent/number_filter_option.rb +7 -0
  12. data/app/models/clark_kent/object_filter_option.rb +7 -0
  13. data/app/models/clark_kent/report.rb +49 -6
  14. data/app/models/clark_kent/report_column.rb +31 -4
  15. data/app/models/clark_kent/report_config.rb +24 -0
  16. data/app/models/clark_kent/report_date_filter.rb +0 -2
  17. data/app/models/clark_kent/report_email.rb +1 -2
  18. data/app/models/clark_kent/report_filter.rb +1 -2
  19. data/app/models/clark_kent/report_filter_error.rb +4 -0
  20. data/app/models/clark_kent/report_filter_option.rb +5 -9
  21. data/app/models/clark_kent/report_number_filter.rb +0 -3
  22. data/app/models/clark_kent/reportable.rb +28 -3
  23. data/app/models/clark_kent/resource_option.rb +10 -0
  24. data/app/models/clark_kent/sharing_scope_kind.rb +38 -4
  25. data/app/models/clark_kent/string_filter_option.rb +7 -0
  26. data/app/models/clark_kent/user_report_email.rb +2 -4
  27. data/app/views/clark_kent/report_columns/_form.html.erb +1 -1
  28. data/app/views/clark_kent/reports/_edit.html.erb +8 -4
  29. data/app/views/clark_kent/reports/show.html.erb +5 -0
  30. data/db/migrate/20131226170042_create_clark_kent_report_filter.rb +17 -0
  31. data/db/migrate/20131226170112_create_clark_kent_report_column.rb +12 -0
  32. data/db/migrate/20140114010048_create_clark_kent_report_emails.rb +9 -0
  33. data/db/migrate/20140129051754_create_clark_kent_user_report_email.rb +7 -0
  34. data/db/migrate/20150304233739_create_clark_kent_reports.rb +2 -0
  35. data/lib/clark_kent/version.rb +1 -1
  36. data/lib/clark_kent.rb +4 -2
  37. data/test/_print_report.html.erb_spec.rb +32 -0
  38. data/test/controllers/clark_kent/reports_controller_test.rb +35 -12
  39. data/test/dummy/app/controllers/application_controller.rb +12 -0
  40. data/test/dummy/app/models/department.rb +4 -0
  41. data/test/dummy/app/models/order.rb +6 -0
  42. data/test/dummy/app/models/reporting/order.rb +54 -0
  43. data/test/dummy/app/models/user.rb +9 -0
  44. data/test/dummy/app/views/layouts/application.html.erb +5 -2
  45. data/test/dummy/config/initializers/zz_clark_kent.rb +11 -0
  46. data/test/dummy/config/routes.rb +1 -1
  47. data/test/dummy/db/development.sqlite3 +0 -0
  48. data/test/dummy/db/migrate/20150309232850_create_orders.rb +10 -0
  49. data/test/dummy/db/migrate/20150309232855_create_users.rb +9 -0
  50. data/test/dummy/db/migrate/20150310143940_create_departments.rb +8 -0
  51. data/test/dummy/db/migrate/20150313144011_create_clark_kent_report_filter.clark_kent.rb +18 -0
  52. data/test/dummy/db/migrate/20150313144012_create_clark_kent_report_column.clark_kent.rb +13 -0
  53. data/test/dummy/db/migrate/20150313144013_create_clark_kent_report_emails.clark_kent.rb +10 -0
  54. data/test/dummy/db/migrate/20150313144014_create_clark_kent_user_report_email.clark_kent.rb +8 -0
  55. data/test/dummy/db/migrate/20150313144015_create_clark_kent_reports.clark_kent.rb +15 -0
  56. data/test/dummy/db/schema.rb +61 -1
  57. data/test/dummy/db/test.sqlite3 +0 -0
  58. data/test/dummy/log/development.log +18038 -0
  59. data/test/dummy/log/test.log +14137 -0
  60. data/test/dummy/tmp/cache/assets/development/sprockets/0a5b3da98f8307d16bc302a1f7206591 +0 -0
  61. data/test/dummy/tmp/cache/assets/development/sprockets/0a9995208f1340e4b34008cbd5b73c64 +0 -0
  62. data/test/dummy/tmp/cache/assets/development/sprockets/0ec37c0a58c1be93659732a3efc73581 +0 -0
  63. data/test/dummy/tmp/cache/assets/development/sprockets/0fd54fd98cd2fa0085b77e6743046927 +0 -0
  64. data/test/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  65. data/test/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  66. data/test/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  67. data/test/dummy/tmp/cache/assets/development/sprockets/8aa37926d964a9eb59cf9b940e4fe2f4 +0 -0
  68. data/test/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  69. data/test/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  70. data/test/dummy/tmp/cache/assets/development/sprockets/ebe8eac74b8e6016fd44b19e6e708e61 +0 -0
  71. data/test/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  72. data/test/dummy/tmp/cache/assets/test/sprockets/0a5b3da98f8307d16bc302a1f7206591 +0 -0
  73. data/test/dummy/tmp/cache/assets/test/sprockets/0a9995208f1340e4b34008cbd5b73c64 +0 -0
  74. data/test/dummy/tmp/cache/assets/test/sprockets/0ec37c0a58c1be93659732a3efc73581 +0 -0
  75. data/test/dummy/tmp/cache/assets/test/sprockets/0fd54fd98cd2fa0085b77e6743046927 +0 -0
  76. data/test/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  77. data/test/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  78. data/test/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  79. data/test/dummy/tmp/cache/assets/test/sprockets/60fb63be4cad769d9adc90c4c5501c67 +0 -0
  80. data/test/dummy/tmp/cache/assets/test/sprockets/8aa37926d964a9eb59cf9b940e4fe2f4 +0 -0
  81. data/test/dummy/tmp/cache/assets/test/sprockets/92058832b745b88c29a75bf2aad7245d +0 -0
  82. data/test/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  83. data/test/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  84. data/test/dummy/tmp/cache/assets/test/sprockets/ebe8eac74b8e6016fd44b19e6e708e61 +0 -0
  85. data/test/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  86. data/test/dummy/tmp/pids/server.pid +1 -0
  87. data/test/helpers/clark_kent/reports_helper_spec.rb +26 -0
  88. data/test/integration/navigation_test.rb +0 -2
  89. data/test/models/clark_kent/report_test.rb +6 -6
  90. data/test/support/db_utils.rb +29 -0
  91. data/test/test_helper.rb +7 -6
  92. metadata +148 -3
  93. data/app/assets/javascripts/clark_kent/reports.js +0 -10
@@ -3,46 +3,69 @@ require 'test_helper'
3
3
  module ClarkKent
4
4
  class ReportsControllerTest < ActionController::TestCase
5
5
  setup do
6
- @report = reports(:one)
6
+ @report = ClarkKent::Report.first
7
+ @routes = Engine.routes
8
+ @current_user = User.first
7
9
  end
8
10
 
9
11
  test "should get index" do
10
- get :index
12
+ get :index, current_user_id: @current_user.id
11
13
  assert_response :success
12
- assert_not_nil assigns(:reports)
13
14
  end
14
15
 
15
16
  test "should get new" do
16
- get :new
17
+ get :new, current_user_id: @current_user.id
17
18
  assert_response :success
18
19
  end
19
20
 
20
21
  test "should create report" do
21
22
  assert_difference('Report.count') do
22
- post :create, report: { name: @report.name, resource_type: @report.resource_type, sharing_scope_id: @report.sharing_scope_id, sharing_scope_type: @report.sharing_scope_type }
23
+ post :create,
24
+ report: {
25
+ name: 'delete me', resource_type: @report.resource_type,
26
+ sharing_scope_id: @report.sharing_scope_id, sharing_scope_type: @report.sharing_scope_type },
27
+ current_user_id: @current_user.id
23
28
  end
24
-
25
- assert_redirected_to report_path(assigns(:report))
29
+ report = Report.find_by(name: 'delete me')
30
+ report.destroy
31
+ assert_response :success
26
32
  end
27
33
 
28
34
  test "should show report" do
29
- get :show, id: @report
35
+ get :show, id: @report, current_user_id: @current_user.id
36
+ assert_response :success
37
+ end
38
+
39
+ test "should show report results" do
40
+ get :show, id: @report, current_user_id: @current_user.id, run_report: true, created_at_until: Date.today, created_at_from: Date.yesterday
30
41
  assert_response :success
42
+ assert_match 'Guitar strings', @response.body
43
+ end
44
+
45
+ test "should show report run errors" do
46
+ get :show, id: @report, current_user_id: @current_user.id, run_report: true
47
+ assert_response :success
48
+ assert_match 'At least one date range is required.', @response.body
31
49
  end
32
50
 
33
51
  test "should get edit" do
34
- get :edit, id: @report
52
+ get :edit, id: @report, current_user_id: @current_user.id
35
53
  assert_response :success
36
54
  end
37
55
 
38
56
  test "should update report" do
39
- patch :update, id: @report, report: { name: @report.name, resource_type: @report.resource_type, sharing_scope_id: @report.sharing_scope_id, sharing_scope_type: @report.sharing_scope_type }
40
- assert_redirected_to report_path(assigns(:report))
57
+ patch :update, id: @report,
58
+ report: {
59
+ name: @report.name, resource_type: @report.resource_type,
60
+ sharing_scope_id: @report.sharing_scope_id, sharing_scope_type: @report.sharing_scope_type },
61
+ current_user_id: @current_user.id
62
+ assert_response :success
41
63
  end
42
64
 
43
65
  test "should destroy report" do
66
+ report = Report.create(name: 'delete me', resource_type: 'Order')
44
67
  assert_difference('Report.count', -1) do
45
- delete :destroy, id: @report
68
+ delete :destroy, id: report, current_user_id: @current_user.id
46
69
  end
47
70
 
48
71
  assert_redirected_to reports_path
@@ -2,4 +2,16 @@ class ApplicationController < ActionController::Base
2
2
  # Prevent CSRF attacks by raising an exception.
3
3
  # For APIs, you may want to use :null_session instead.
4
4
  protect_from_forgery with: :exception
5
+ def prepare_filters
6
+ fc_struct = Struct.new(:orders,:departments,:users)
7
+ @filter_collections = fc_struct.new(::Order.all, ::Department.all, ::User.all)
8
+ end
9
+
10
+ def current_user
11
+
12
+ @current_user ||= ::User.where(id: params[:current_user_id]).first || ::User.where("id is not null").first
13
+ @current_user
14
+ end
15
+
16
+ helper_method :current_user
5
17
  end
@@ -0,0 +1,4 @@
1
+ class Department < ActiveRecord::Base
2
+ has_many :users
3
+ has_many :clark_kent_reports, as: :sharing_scope, class_name: '::ClarkKent::Report'
4
+ end
@@ -0,0 +1,6 @@
1
+ class Order < ActiveRecord::Base
2
+ include Reporting::Order
3
+ belongs_to :user
4
+
5
+ paginates_per 10
6
+ end
@@ -0,0 +1,54 @@
1
+ module Reporting
2
+ module Order
3
+ include ClarkKent::ReportConfig
4
+ # This mapping tells ClarkKent how to use params for filtering a report. It is only used for pre-defined,
5
+ # 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
6
+ # the convention <param name>_arel(query, field_name, match_value). nb. ranges can be handled with dual params like date_before/date_after
7
+ # or amount_below/amount_above etc.
8
+ AREL_METHODS ||= {
9
+ 'user_id' => 'simple_equality_arel',
10
+ 'amount_above' => 'above_number_arel',
11
+ 'amount_below' => 'below_number_arel'
12
+ }
13
+
14
+ # These are the options for permanent filters built into a report. nb dates don't make sense as permanent report filters
15
+ # except in the case of ClarkKent::ReportEmails.
16
+ REPORT_DEFINITION_OPTIONS ||= ['user_id', 'created_at']
17
+
18
+ # This is the full set of report filter options for use at report runtime. REPORT_DEFINITION_OPTIONS must refer to some subset of these
19
+ REPORT_FILTER_OPTIONS ||= [
20
+ filter_config(kind: 'date_filter', param: 'created_at', in_required_date_group: true),
21
+ filter_config(kind: 'object_filter', param: 'user_id', collection: :users, label: 'user'),
22
+ filter_config(kind: 'string_filter', param: 'user_email')
23
+ ]
24
+
25
+ # These are the available column options for building reports from this resource
26
+ REPORT_COLUMN_OPTIONS ||= {
27
+ user_name: column_config(
28
+ custom_select: "
29
+ (SELECT u.name
30
+ FROM users u
31
+ WHERE u.id = orders.user_id)
32
+ as user_name"),
33
+ id: column_config(order_sql: 'orders.id'),
34
+ amount: column_config(order_sql: 'orders.amount', summarizable: true),
35
+ description: column_config(order_sql: 'orders.description')
36
+ }
37
+
38
+ def self.included(base)
39
+ base.extend ClassMethods
40
+ base.include ClarkKent::Reportable
41
+ end
42
+
43
+ module ClassMethods
44
+
45
+ def user_email_arel(query, field_name, match_value)
46
+ query = query.
47
+ joins(:user).
48
+ where(users: {email: match_value})
49
+ end
50
+
51
+ end # ClassMethods
52
+
53
+ end
54
+ end
@@ -0,0 +1,9 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :orders
3
+ has_many :clark_kent_reports, as: :sharing_scope, class_name: '::ClarkKent::Report'
4
+ belongs_to :department
5
+
6
+ def full_name
7
+ name
8
+ end
9
+ end
@@ -3,12 +3,15 @@
3
3
  <head>
4
4
  <title>Dummy</title>
5
5
  <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= stylesheet_link_tag "clark_kent/application.css" %>
6
7
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
8
+ <%= javascript_include_tag 'clark_kent/application' %>
7
9
  <%= csrf_meta_tags %>
8
10
  </head>
9
11
  <body>
10
-
11
- <%= yield %>
12
+ <div class="container">
13
+ <%= yield %>
14
+ </div>
12
15
 
13
16
  </body>
14
17
  </html>
@@ -0,0 +1,11 @@
1
+
2
+ resource_option_struct = Struct.new(:id, :name)
3
+
4
+ ClarkKent.config({
5
+ resource_options: [
6
+ {id: 'Order', name: 'Order'}
7
+ ],
8
+ user_class_name: 'User',
9
+ other_sharing_scopes: [['Department',:department]],
10
+ base_controller_name: '::ApplicationController'
11
+ })
@@ -1,4 +1,4 @@
1
1
  Rails.application.routes.draw do
2
2
 
3
- mount ClarkKent::Engine => "/clark_kent"
3
+ mount ClarkKent::Engine => "/reports"
4
4
  end
Binary file
@@ -0,0 +1,10 @@
1
+ class CreateOrders < ActiveRecord::Migration
2
+ def change
3
+ create_table :orders do |t|
4
+ t.integer :user_id
5
+ t.integer :amount
6
+ t.string :description
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :users do |t|
4
+ t.string :name
5
+ t.string :email
6
+ t.integer :age
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ class CreateDepartments < ActiveRecord::Migration
2
+ def change
3
+ create_table :departments do |t|
4
+ t.string :name
5
+ end
6
+ add_column :users, :department_id, :integer
7
+ end
8
+ end
@@ -0,0 +1,18 @@
1
+ # This migration comes from clark_kent (originally 20131226170042)
2
+ class CreateClarkKentReportFilter < ActiveRecord::Migration
3
+ def change
4
+ create_table :clark_kent_report_filters do |t|
5
+ t.integer :filterable_id
6
+ t.string :filterable_type, :string, default: 'ClarkKent::Report'
7
+ t.string :filter_name
8
+ t.string :filter_value
9
+ t.string :type
10
+ t.string :duration
11
+ t.string :kind_of_day
12
+ t.string :offset
13
+ t.timestamps
14
+ end
15
+ add_index :clark_kent_report_filters, :filterable_id
16
+ add_index :clark_kent_report_filters, :filterable_type
17
+ end
18
+ end
@@ -0,0 +1,13 @@
1
+ # This migration comes from clark_kent (originally 20131226170112)
2
+ class CreateClarkKentReportColumn < ActiveRecord::Migration
3
+ def change
4
+ create_table :clark_kent_report_columns do |t|
5
+ t.integer :report_id
6
+ t.string :column_name
7
+ t.integer :column_order
8
+ t.string :report_sort
9
+ t.string :summary_method
10
+ end
11
+ add_index :clark_kent_report_columns, :report_id
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ # This migration comes from clark_kent (originally 20140114010048)
2
+ class CreateClarkKentReportEmails < ActiveRecord::Migration
3
+ def change
4
+ create_table :clark_kent_report_emails do |t|
5
+ t.integer :report_id
6
+ t.string :when_to_send
7
+ t.string :name
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ # This migration comes from clark_kent (originally 20140129051754)
2
+ class CreateClarkKentUserReportEmail < ActiveRecord::Migration
3
+ def change
4
+ create_table :clark_kent_user_report_emails do |t|
5
+ t.integer ClarkKent.user_class_name.underscore + '_id', :report_email_id
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,15 @@
1
+ # This migration comes from clark_kent (originally 20150304233739)
2
+ class CreateClarkKentReports < ActiveRecord::Migration
3
+ def change
4
+ create_table :clark_kent_reports do |t|
5
+ t.string :name
6
+ t.string :resource_type
7
+ t.string :sharing_scope_type
8
+ t.integer :sharing_scope_id
9
+
10
+ t.timestamps
11
+ end
12
+ add_index :clark_kent_reports, :sharing_scope_type
13
+ add_index :clark_kent_reports, :sharing_scope_id
14
+ end
15
+ end
@@ -11,7 +11,40 @@
11
11
  #
12
12
  # It's strongly recommended that you check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(version: 20150304233739) do
14
+ ActiveRecord::Schema.define(version: 20150313144015) do
15
+
16
+ create_table "clark_kent_report_columns", force: true do |t|
17
+ t.integer "report_id"
18
+ t.string "column_name"
19
+ t.integer "column_order"
20
+ t.string "report_sort"
21
+ t.string "summary_method"
22
+ end
23
+
24
+ add_index "clark_kent_report_columns", ["report_id"], name: "index_clark_kent_report_columns_on_report_id"
25
+
26
+ create_table "clark_kent_report_emails", force: true do |t|
27
+ t.integer "report_id"
28
+ t.string "when_to_send"
29
+ t.string "name"
30
+ end
31
+
32
+ create_table "clark_kent_report_filters", force: true do |t|
33
+ t.integer "filterable_id"
34
+ t.string "filterable_type", default: "ClarkKent::Report"
35
+ t.string "string", default: "ClarkKent::Report"
36
+ t.string "filter_name"
37
+ t.string "filter_value"
38
+ t.string "type"
39
+ t.string "duration"
40
+ t.string "kind_of_day"
41
+ t.string "offset"
42
+ t.datetime "created_at"
43
+ t.datetime "updated_at"
44
+ end
45
+
46
+ add_index "clark_kent_report_filters", ["filterable_id"], name: "index_clark_kent_report_filters_on_filterable_id"
47
+ add_index "clark_kent_report_filters", ["filterable_type"], name: "index_clark_kent_report_filters_on_filterable_type"
15
48
 
16
49
  create_table "clark_kent_reports", force: true do |t|
17
50
  t.string "name"
@@ -22,4 +55,31 @@ ActiveRecord::Schema.define(version: 20150304233739) do
22
55
  t.datetime "updated_at"
23
56
  end
24
57
 
58
+ add_index "clark_kent_reports", ["sharing_scope_id"], name: "index_clark_kent_reports_on_sharing_scope_id"
59
+ add_index "clark_kent_reports", ["sharing_scope_type"], name: "index_clark_kent_reports_on_sharing_scope_type"
60
+
61
+ create_table "clark_kent_user_report_emails", force: true do |t|
62
+ t.integer "user_id"
63
+ t.integer "report_email_id"
64
+ end
65
+
66
+ create_table "departments", force: true do |t|
67
+ t.string "name"
68
+ end
69
+
70
+ create_table "orders", force: true do |t|
71
+ t.integer "user_id"
72
+ t.integer "amount"
73
+ t.string "description"
74
+ t.datetime "created_at"
75
+ t.datetime "updated_at"
76
+ end
77
+
78
+ create_table "users", force: true do |t|
79
+ t.string "name"
80
+ t.string "email"
81
+ t.integer "age"
82
+ t.integer "department_id"
83
+ end
84
+
25
85
  end
Binary file