admino 0.0.16 → 0.0.17

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: 58ca3e32d9e7c3a7f09785d413a90d85335f5d2e
4
- data.tar.gz: cbf4064ab9463949fb64f47d4a1825f8107852e7
3
+ metadata.gz: 119481a7da909a0c22fd1e660cf932069efda0b9
4
+ data.tar.gz: 248ae36712a2f036640ddfe7b1bffccf2ed65fa7
5
5
  SHA512:
6
- metadata.gz: 662ab77aa388252b75e0463c8ec4ad112c45fd2ee9393bdd26d15b163a3630150813f6917b5f3fb09bd83432b7ed9828e1f67b66f47419def995372a84c5622b
7
- data.tar.gz: 8469b32536e19f81058a5c8111d094515ecff383094a8494d6e1651e55afaa81265e90951242ff9d6501c8f74bbf643277f62005b331a6f7466a453de4ca7eac
6
+ metadata.gz: a7a69fb319804d48bf90a98fc9da3d7e258a8cd82af23a01b64b79fa9616307f0c97ed30dcca76636ea823b689c9665c4843ccf638f2bf88cdeabb7ca66a57c1
7
+ data.tar.gz: 1099cf74ec2b87cb2255377d6767e1fa83b8ca6432c846f3571ce262ae046c512522d3e10e5b4b7a16d0c9c30414619c99499d68aef139c61a685a7b35a7f88e
data/README.md CHANGED
@@ -200,6 +200,23 @@ class TasksQuery < Admino::Query::Base
200
200
  end
201
201
  ```
202
202
 
203
+ ### Let the query object do the chaining
204
+
205
+ If you do not want to pollute your ActiveRecord model with all these scopes, you are free to implement the scopes on the query object itself:
206
+
207
+ ```ruby
208
+ class Task < ActiveRecord::Base
209
+ end
210
+
211
+ class TasksQuery < Admino::Query::Base
212
+ search_field :title_matches
213
+
214
+ def title_matches(scope, text)
215
+ scope.where('title ILIKE ?', "%#{text}%")
216
+ end
217
+ end
218
+ ```
219
+
203
220
  ### Inspecting the query state
204
221
 
205
222
  A query object supports various methods to inspect the available search fields, filters and sortings, and their state:
@@ -6,6 +6,7 @@ require 'active_support/hash_with_indifferent_access'
6
6
  require 'active_support/core_ext/hash'
7
7
 
8
8
  require 'admino/query/dsl'
9
+ require 'admino/query/builder'
9
10
 
10
11
  module Admino
11
12
  module Query
@@ -40,19 +41,20 @@ module Admino
40
41
  raise ArgumentError, 'no starting scope provided'
41
42
  end
42
43
 
43
- scope_builder = starting_scope
44
-
44
+ query_builder = Builder.new(self, starting_scope)
45
45
  scope_augmenters = search_fields + filter_groups
46
46
  scope_augmenters << sorting if sorting
47
47
 
48
48
  scope_augmenters.each do |search_field|
49
- scope_builder = search_field.augment_scope(scope_builder)
49
+ query_builder = search_field.augment_scope(query_builder)
50
50
  end
51
51
 
52
+ scope = query_builder.scope
53
+
52
54
  if config.ending_scope_callable
53
- scope_builder.instance_exec(self, &config.ending_scope_callable)
55
+ scope.instance_exec(self, &config.ending_scope_callable)
54
56
  else
55
- scope_builder
57
+ scope
56
58
  end
57
59
  end
58
60
 
@@ -0,0 +1,23 @@
1
+ module Admino
2
+ module Query
3
+ class Builder
4
+ attr_accessor :scope
5
+ attr_reader :context
6
+
7
+ def initialize(context, scope)
8
+ @context = context
9
+ @scope = scope
10
+ end
11
+
12
+ private
13
+
14
+ def method_missing(method, *args)
15
+ if context.respond_to?(method)
16
+ Builder.new(context, context.send(method, scope, *args))
17
+ else
18
+ Builder.new(context, scope.send(method, *args))
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Admino
2
- VERSION = "0.0.16"
2
+ VERSION = "0.0.17"
3
3
  end
@@ -112,33 +112,66 @@ module Admino
112
112
  let(:filter_group_config) { config.add_filter_group(:filter_group, [:one, :two]) }
113
113
  let(:sorting_config) { config.add_sorting_scopes([:title, :year]) }
114
114
 
115
- let(:scope_chained_with_search_field) { double('scope 1') }
116
- let(:scope_chained_with_group_filter) { double('scope 2') }
117
- let(:final_chain) { double('scope 3') }
115
+ let(:params) do
116
+ {
117
+ query: {
118
+ search_field: "foo",
119
+ filter_group: "one",
120
+ },
121
+ sorting: "title",
122
+ sort_order: "desc"
123
+ }
124
+ end
118
125
 
119
126
  before do
120
127
  search_field_config
121
128
  filter_group_config
122
129
  sorting_config
123
130
  query
131
+ end
124
132
 
125
- allow(query.search_field_by_name(:search_field)).
126
- to receive(:augment_scope).
127
- with(starting_scope).
128
- and_return(scope_chained_with_search_field)
133
+ context 'if query object does not respond to scopes' do
134
+ it 'chains from starting scope' do
135
+ expect(result.chain).to eq [
136
+ :search_field, ["foo"],
137
+ :one, [],
138
+ :title, [:desc]
139
+ ]
140
+ end
141
+ end
129
142
 
130
- allow(query.filter_group_by_name(:filter_group)).
131
- to receive(:augment_scope).
132
- with(scope_chained_with_search_field).
133
- and_return(scope_chained_with_group_filter)
143
+ context 'else' do
144
+ let(:query_klass) do
145
+ Class.new(Base) do
146
+ def self.model_name
147
+ ActiveModel::Name.new(self, nil, "temp")
148
+ end
149
+
150
+ def search_field(scope, foo)
151
+ scope.my_search_field(foo)
152
+ end
153
+
154
+ def one(scope)
155
+ scope.my_one
156
+ end
157
+
158
+ def title(scope, order)
159
+ scope.my_title(order)
160
+ end
161
+ end
162
+ end
134
163
 
135
- allow(query.sorting).to receive(:augment_scope).
136
- with(scope_chained_with_group_filter).
137
- and_return(final_chain)
138
- end
164
+ subject(:query) do
165
+ query_klass.new(params, config)
166
+ end
139
167
 
140
- it 'chains them toghether' do
141
- expect(result).to eq final_chain
168
+ it 'chains from starting scope calling query object methods' do
169
+ expect(result.chain).to eq [
170
+ :my_search_field, ["foo"],
171
+ :my_one, [],
172
+ :my_title, [:desc]
173
+ ]
174
+ end
142
175
  end
143
176
  end
144
177
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: admino
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.16
4
+ version: 0.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Verna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-01 00:00:00.000000000 Z
11
+ date: 2016-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: showcase
@@ -205,6 +205,7 @@ files:
205
205
  - lib/admino/query.rb
206
206
  - lib/admino/query/base.rb
207
207
  - lib/admino/query/base_presenter.rb
208
+ - lib/admino/query/builder.rb
208
209
  - lib/admino/query/configuration.rb
209
210
  - lib/admino/query/dsl.rb
210
211
  - lib/admino/query/filter_group.rb
@@ -254,7 +255,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
254
255
  version: '0'
255
256
  requirements: []
256
257
  rubyforge_project:
257
- rubygems_version: 2.2.2
258
+ rubygems_version: 2.5.1
258
259
  signing_key:
259
260
  specification_version: 4
260
261
  summary: Make administrative views creation less repetitive