activeadmin-searchable_select 1.4.0 → 1.5.0

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
  SHA256:
3
- metadata.gz: fe0935611dac081858397d808fa99c65a72944153ce323c8b10f3afb96657a83
4
- data.tar.gz: e5c1faa951288612248a1f87f4ed1961f7f45861b7ac1946a404c5f15030f656
3
+ metadata.gz: eae3cd46ff6e641c50c4f6e02c7aa4ab8105a160740c07fd82efea3eb4af26e8
4
+ data.tar.gz: 37274666f643b0df81a37aa0dbbd197cc7b94f45ef804398496a1ecbb3f1094f
5
5
  SHA512:
6
- metadata.gz: '097917cab8c292dcf3cdab69661ff59f6a0377698514b6f7323b28a3a70bc2c3f66c41b64a7e26ae47f981559f791d6ae35df96ec1a736f214fcd7753cd25693'
7
- data.tar.gz: 10592311d0cf0ad805abaf218ce97a9af4ff8a8350b861571465e01106815d6b3eaddd9641f2256e5323e5a05a6ed7cd0759a6531cdfe5af2cf858e69affedf2
6
+ metadata.gz: a0448e24dbb3c48ca7421eca976836d5be764e91db9e89a129e8c9b47dbb5dbbbdb94ac8937d049b5af35d6c68a17933bc6e3712719fd920b125e65d29147b88
7
+ data.tar.gz: 4c90fe918890f40de37d138ba3b82b12307bf5331772a0dd8a139afb1698a8cf3a95620a72372415437b103851ee4ae574807d11a888cd8b850a6d360a2af6e9
data/CHANGELOG.md CHANGED
@@ -1,16 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
- ### Version 1.4.0
3
+ ### Version 1.5.0
4
4
 
5
- 2021-02-04
5
+ 2021-02-16
6
6
 
7
- [Compare changes](https://github.com/codevise/activeadmin-searchable_select/compare/1-3-stable...v1.4.0)
7
+ [Compare changes](https://github.com/codevise/activeadmin-searchable_select/compare/1-4-stable...v1.5.0)
8
8
 
9
- - Support admin resources for namespaced models.
10
- ([#30](https://github.com/codevise/activeadmin-searchable_select/pull/30))
11
- - Improve README.md
12
- ([#28](https://github.com/codevise/activeadmin-searchable_select/pull/28))
9
+ - Allow adding additional payload to option items in AJAX response
10
+ ([#33](https://github.com/codevise/activeadmin-searchable_select/pull/33))
13
11
 
14
12
  See
15
- [1-3-stable branch](https://github.com/codevise/activeadmin-searchable_select/blob/1-3-stable/CHANGELOG.md)
13
+ [1-4-stable branch](https://github.com/codevise/activeadmin-searchable_select/blob/1-4-stable/CHANGELOG.md)
16
14
  for previous changes.
data/README.md CHANGED
@@ -193,6 +193,25 @@ build your query in a way that it can query multiple attributes at once.
193
193
 
194
194
  In this example, the `all` scope will query `email OR username`.
195
195
 
196
+ You can add the additional payload as dsl option:
197
+
198
+ ```ruby
199
+ ActiveAdmin.register Category do
200
+ searchable_select_options(scope: Category.all,
201
+ text_attribute: :name,
202
+ additional_payload: ->(record) { { foo: record.bar } } )
203
+ end
204
+ ```
205
+
206
+ response example which uses additional_payload:
207
+
208
+ ```json
209
+ {
210
+ "results": [{ "id": "1", "text": "Bicycles", "foo": "Bar" }],
211
+ "pagination": { "more": "false" }
212
+ }
213
+ ```
214
+
196
215
  #### Passing Parameters
197
216
 
198
217
  You can pass additional parameters to the options endpoint:
@@ -8,6 +8,7 @@ module ActiveAdmin
8
8
  @display_text = extract_display_text_option(options)
9
9
  @filter = extract_filter_option(options)
10
10
  @per_page = options.fetch(:per_page, 10)
11
+ @additional_payload = options.fetch(:additional_payload, {})
11
12
  end
12
13
 
13
14
  def scope(template, params)
@@ -38,7 +39,7 @@ module ActiveAdmin
38
39
  {
39
40
  id: record.id,
40
41
  text: display_text(record)
41
- }
42
+ }.merge(hash_of_additional_payload(record) || {})
42
43
  end
43
44
 
44
45
  { results: results, pagination: { more: more } }
@@ -98,6 +99,21 @@ module ActiveAdmin
98
99
  ->(term, scope) { scope.ransack("#{text_attribute}_cont" => term).result }
99
100
  end
100
101
  end
102
+
103
+ def build_additional_payload(record)
104
+ case @additional_payload
105
+ when Proc
106
+ @additional_payload.call(record).to_h
107
+ else
108
+ {}
109
+ end
110
+ end
111
+
112
+ def hash_of_additional_payload(record)
113
+ return nil if @additional_payload.nil? && @additional_payload.empty?
114
+
115
+ build_additional_payload(record)
116
+ end
101
117
  end
102
118
  end
103
119
  end
@@ -26,6 +26,23 @@ module ActiveAdmin
26
26
  #
27
27
  # @param name [Symbol] Optional collection name if helper is
28
28
  # used multiple times within one resource.
29
+ #
30
+ # @param additional_payload [Proc]
31
+ # Adds additional attributes to the results array
32
+ # @example
33
+ #
34
+ # ActiveAdmin.register Tag do
35
+ # searchable_select_options(
36
+ # scope: Color,
37
+ # text_attributes: :title,
38
+ # additional_payload: lambda { |record| { color: record.color } }
39
+ # )
40
+ # end
41
+ # @json
42
+ # {
43
+ # "results": [{ "id": "1", "text": "Red", "color": "#FFF" }],
44
+ # "pagination": { "more": "false" }
45
+ # }
29
46
  def searchable_select_options(name: :all, **options)
30
47
  option_collection = OptionCollection.new(name, options)
31
48
  config.searchable_select_option_collections[name] = option_collection
@@ -1,5 +1,5 @@
1
1
  module ActiveAdmin
2
2
  module SearchableSelect
3
- VERSION = '1.4.0'.freeze
3
+ VERSION = '1.5.0'.freeze
4
4
  end
5
5
  end
@@ -140,6 +140,60 @@ RSpec.describe 'searchable_select_options dsl', type: :request do
140
140
  end
141
141
  end
142
142
 
143
+ describe 'with additional_payload' do
144
+ context 'as lambda' do
145
+ before(:each) do
146
+ ActiveAdminHelpers.setup do
147
+ ActiveAdmin.register(Post) do
148
+ searchable_select_options(
149
+ scope: Post,
150
+ text_attribute: :title,
151
+ additional_payload: lambda do |record|
152
+ { published: record.published }
153
+ end
154
+ )
155
+ end
156
+ end
157
+ end
158
+ let!(:post) { Post.create!(title: 'A post', published: false) }
159
+
160
+ subject { get '/admin/posts/all_options' }
161
+
162
+ it 'returns options with our additional attribute' do
163
+ subject
164
+ expect(json_response).to match(
165
+ results: [{ text: 'A post', id: post.id, published: false }],
166
+ pagination: { more: false }
167
+ )
168
+ end
169
+ end
170
+
171
+ context 'as Proc' do
172
+ before(:each) do
173
+ ActiveAdminHelpers.setup do
174
+ ActiveAdmin.register(Post) do
175
+ searchable_select_options(
176
+ scope: Post,
177
+ text_attribute: :title,
178
+ additional_payload: proc { |record| { published: record.published } }
179
+ )
180
+ end
181
+ end
182
+ end
183
+ let!(:post) { Post.create!(title: 'A post', published: false) }
184
+
185
+ subject { get '/admin/posts/all_options' }
186
+
187
+ it 'returns options with our additional attribute' do
188
+ subject
189
+ expect(json_response).to match(
190
+ results: [{ text: 'A post', id: post.id, published: false }],
191
+ pagination: { more: false }
192
+ )
193
+ end
194
+ end
195
+ end
196
+
143
197
  it 'allows passing lambda as scope' do
144
198
  ActiveAdminHelpers.setup do
145
199
  ActiveAdmin.register(Post) do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeadmin-searchable_select
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Codevise Solutions Ltd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-04 00:00:00.000000000 Z
11
+ date: 2021-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler