reports_kit 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91c1e89f707ed386d31fbd29b764143a579bd634
4
- data.tar.gz: 4afd3e37a4f9d315693e2f8626359ead4ba36870
3
+ metadata.gz: f73a1370ec5380438c97d70101c44741b71e11d4
4
+ data.tar.gz: 512485a9a8703592305f62bc0af6f21cc339d4f0
5
5
  SHA512:
6
- metadata.gz: aecd3b0e784ec878e3cd154f1b16404f3c88b5a4aa910361ffc70c0cbf8c769fb40c90c6adea9069e2a7096d34c95c6f01dfaacff2140cbc8206b6444936298d
7
- data.tar.gz: 636e9ba27cef392cb7d22e8ae3ef61606e5e859744490baaad9fe8a5e75767431fb07d8686c5fe989e4072cf9de87d6e82987e94492421cb463ee206e3ee9554
6
+ metadata.gz: 7f05e2853acc4f162d822351bdeb4e25243557334af27cde8b20edbccc825f95b1458e076a514490b054eb733807d32fa6d310f84e20412f9793d4d56fa47c46
7
+ data.tar.gz: 58c18993f9d3e24025a4fafe901b2c0345b8fa0019aedc01258873d0a128cc22bf1c92fbd173a7703bf5000c3919d10720f1b4b766c6d0fe5e509b7e9ad31e9b
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- reports_kit (0.4.1)
4
+ reports_kit (0.5.0)
5
5
  rails (>= 3)
6
6
  spreadsheet (>= 1.1)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- reports_kit (0.4.1)
4
+ reports_kit (0.5.0)
5
5
  rails (>= 3)
6
6
  spreadsheet (>= 1.1)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- reports_kit (0.4.1)
4
+ reports_kit (0.5.0)
5
5
  rails (>= 3)
6
6
  spreadsheet (>= 1.1)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- reports_kit (0.4.1)
4
+ reports_kit (0.5.0)
5
5
  rails (>= 3)
6
6
  spreadsheet (>= 1.1)
7
7
 
@@ -5,9 +5,9 @@ module ReportsKit
5
5
 
6
6
  delegate :settings_from_model, to: :model_settings
7
7
 
8
- def initialize(key, series:)
8
+ def initialize(key, model_class)
9
9
  self.key = key.to_sym
10
- self.model_settings = ModelSettings.new(series, :contextual_filters, self.key)
10
+ self.model_settings = ModelSettings.new(model_class, :contextual_filters, self.key)
11
11
  end
12
12
 
13
13
  def apply(relation, context_params)
@@ -80,6 +80,15 @@ module ReportsKit
80
80
  type_class
81
81
  end
82
82
 
83
+ def apply_contextual_filters(relation, context_params)
84
+ return relation if properties[:contextual_filters].blank? || context_params.blank?
85
+ contextual_filters = properties[:contextual_filters].map { |key| ContextualFilter.new(key, relation.base_class) }
86
+ contextual_filters.each do |contextual_filter|
87
+ relation = contextual_filter.apply(relation, context_params)
88
+ end
89
+ relation
90
+ end
91
+
83
92
  def apply(relation)
84
93
  filter_type.apply_filter(relation)
85
94
  end
@@ -1,10 +1,11 @@
1
1
  module ReportsKit
2
2
  module Reports
3
3
  class GenerateAutocompleteResults
4
- attr_accessor :params, :filter_key, :filter, :context_record
4
+ attr_accessor :params, :filter_key, :filter, :context_params, :context_record
5
5
 
6
6
  def initialize(params, properties, context_record: nil)
7
7
  self.params = params
8
+ self.context_params = params[:context_params]
8
9
  self.filter_key = params[:key]
9
10
  self.filter = Reports::PropertiesToFilter.new(properties, context_record: context_record).perform(filter_key)
10
11
  self.context_record = context_record
@@ -13,7 +14,7 @@ module ReportsKit
13
14
  def perform
14
15
  raise ArgumentError.new("Could not find a model for filter_key: '#{filter_key}'") unless model
15
16
  return autocomplete_results_method.call(params: params, context_record: context_record, relation: relation) if autocomplete_results_method
16
- results = relation
17
+ results = filter.apply_contextual_filters(relation, context_params)
17
18
  results = results.limit(10_000)
18
19
  results = results.map { |result| { id: result.id, text: result.to_s } }
19
20
  results = results.sort_by { |result| result[:text].downcase }
@@ -17,7 +17,7 @@ module ReportsKit
17
17
  def initialize(inferrable, inferrable_type)
18
18
  self.inferrable = inferrable
19
19
  self.inferrable_type = inferrable_type
20
- self.model_settings = ModelSettings.new(series, inferrable_type, key)
20
+ self.model_settings = ModelSettings.new(series.model_class, inferrable_type, key)
21
21
  end
22
22
 
23
23
  def configuration_strategy
@@ -1,10 +1,10 @@
1
1
  module ReportsKit
2
2
  module Reports
3
3
  class ModelSettings
4
- attr_accessor :series, :model_configuration_type, :key
4
+ attr_accessor :model_class, :model_configuration_type, :key
5
5
 
6
- def initialize(series, model_configuration_type, key)
7
- self.series = series
6
+ def initialize(model_class, model_configuration_type, key)
7
+ self.model_class = model_class
8
8
  self.model_configuration_type = model_configuration_type
9
9
  self.key = key
10
10
  end
@@ -19,11 +19,6 @@ module ReportsKit
19
19
  config_hash || {}
20
20
  end
21
21
 
22
- def model_class
23
- return unless series
24
- series.model_class
25
- end
26
-
27
22
  private
28
23
 
29
24
  def model_configuration
@@ -23,7 +23,7 @@ module ReportsKit
23
23
  self.context_record = context_record
24
24
  self.dimensions = dimension_hashes.map { |dimension_hash| DimensionWithSeries.new(dimension: Dimension.new(dimension_hash), series: self) }
25
25
  self.filters = filter_hashes.map { |filter_hash| FilterWithSeries.new(filter: Filter.new(filter_hash), series: self) }
26
- self.contextual_filters = contextual_filter_keys.map { |key| ContextualFilter.new(key, series: self) }
26
+ self.contextual_filters = contextual_filter_keys.map { |key| ContextualFilter.new(key, model_class) }
27
27
  end
28
28
 
29
29
  def key
@@ -1,3 +1,3 @@
1
1
  module ReportsKit
2
- VERSION = '0.4.1'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe ReportsKit::Reports::GenerateAutocompleteResults do
4
+ subject { described_class.new(params, properties).perform }
5
+ let!(:repo) { create(:repo) }
6
+ let!(:repo2) { create(:repo) }
7
+ let!(:issues) do
8
+ [
9
+ create(:issue, repo: repo, opened_at: now - 2.weeks),
10
+ create(:issue, repo: repo2, opened_at: now - 2.weeks),
11
+ create(:issue, repo: repo, opened_at: now)
12
+ ]
13
+ end
14
+
15
+ context 'with valid params' do
16
+ let(:params) { { key: 'repo' } }
17
+ let(:properties) do
18
+ {
19
+ measure: 'issue',
20
+ filters: %w(repo),
21
+ dimensions: %w(opened_at)
22
+ }
23
+ end
24
+
25
+ it 'returns the results' do
26
+ expect(subject).to eq([
27
+ { id: repo.id, text: repo.to_s },
28
+ { id: repo2.id, text: repo2.to_s }
29
+ ])
30
+ end
31
+ end
32
+
33
+ context 'with a contextual_filter' do
34
+ context 'with context_params' do
35
+ let(:params) { { key: 'repo', context_params: { repo_id: repo.id } } }
36
+ let(:properties) do
37
+ {
38
+ measure: 'issue',
39
+ filters: [{ key: 'repo', contextual_filters: %w(for_repo) }],
40
+ dimensions: %w(opened_at)
41
+ }
42
+ end
43
+
44
+ it 'returns the results' do
45
+ expect(subject).to eq([
46
+ { id: repo.id, text: repo.to_s }
47
+ ])
48
+ end
49
+ end
50
+
51
+ context 'without context_params' do
52
+ let(:params) { { key: 'repo' } }
53
+ let(:properties) do
54
+ {
55
+ measure: 'issue',
56
+ filters: [{ key: 'repo', contextual_filters: %w(for_repo) }],
57
+ dimensions: %w(opened_at)
58
+ }
59
+ end
60
+
61
+ it 'returns the results' do
62
+ expect(subject).to eq([
63
+ { id: repo.id, text: repo.to_s },
64
+ { id: repo2.id, text: repo2.to_s }
65
+ ])
66
+ end
67
+ end
68
+ end
69
+ end
@@ -1,6 +1,12 @@
1
1
  class Repo < ActiveRecord::Base
2
+ include ReportsKit::Model
3
+
2
4
  has_many :issues
3
5
 
6
+ reports_kit do
7
+ contextual_filter :for_repo, ->(relation, context_params) { context_params ? relation.where(id: context_params[:repo_id]) : relation }
8
+ end
9
+
4
10
  def to_s
5
11
  full_name
6
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reports_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Benner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-17 00:00:00.000000000 Z
11
+ date: 2017-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -271,6 +271,7 @@ files:
271
271
  - spec/reports_kit/reports/data/normalize_properties_spec.rb
272
272
  - spec/reports_kit/reports/dimension_with_series_spec.rb
273
273
  - spec/reports_kit/reports/filter_with_series_spec.rb
274
+ - spec/reports_kit/reports/generate_autocomplete_results_spec.rb
274
275
  - spec/spec_helper.rb
275
276
  - spec/support/config.rb
276
277
  - spec/support/factory_girl.rb