Skip to content

Commit bbd8cc6

Browse files
authored
Merge pull request #33 from Ivanov-Anton/make_ability_to_add_additional_payload_of_the_ajax_response
Make ability to add additional payload of the AJAX response
2 parents 08f6506 + 5549168 commit bbd8cc6

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,25 @@ build your query in a way that it can query multiple attributes at once.
193193

194194
In this example, the `all` scope will query `email OR username`.
195195

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+
196215
#### Passing Parameters
197216

198217
You can pass additional parameters to the options endpoint:

lib/activeadmin/searchable_select/option_collection.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def initialize(name, options)
88
@display_text = extract_display_text_option(options)
99
@filter = extract_filter_option(options)
1010
@per_page = options.fetch(:per_page, 10)
11+
@additional_payload = options.fetch(:additional_payload, {})
1112
end
1213

1314
def scope(template, params)
@@ -38,7 +39,7 @@ def as_json(template, params)
3839
{
3940
id: record.id,
4041
text: display_text(record)
41-
}
42+
}.merge(hash_of_additional_payload(record) || {})
4243
end
4344

4445
{ results: results, pagination: { more: more } }
@@ -98,6 +99,21 @@ def extract_filter_option(options)
9899
->(term, scope) { scope.ransack("#{text_attribute}_cont" => term).result }
99100
end
100101
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
101117
end
102118
end
103119
end

lib/activeadmin/searchable_select/resource_dsl_extension.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ module ResourceDSLExtension
2626
#
2727
# @param name [Symbol] Optional collection name if helper is
2828
# 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+
# }
2946
def searchable_select_options(name: :all, **options)
3047
option_collection = OptionCollection.new(name, options)
3148
config.searchable_select_option_collections[name] = option_collection

spec/features/options_dsl_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,60 @@
140140
end
141141
end
142142

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+
143197
it 'allows passing lambda as scope' do
144198
ActiveAdminHelpers.setup do
145199
ActiveAdmin.register(Post) do

0 commit comments

Comments
 (0)