Commit 5f06c7478ca313afe0c9f969555830e7171d998b
Committed by
Marcos Pereira
1 parent
100df357
Exists in
staging
and in
32 other branches
api: accept a comma separated string to represent partial fields
(cherry picked from commit 5d2a4bc5ab9e11db0e77539cac0fa49ef1ae66c3)
Showing
2 changed files
with
11 additions
and
2 deletions
Show diff stats
lib/noosfero/api/helpers.rb
... | ... | @@ -46,7 +46,9 @@ require_relative '../../find_by_contents' |
46 | 46 | options.merge!(fields.symbolize_keys.slice(:only, :except)) |
47 | 47 | end |
48 | 48 | rescue |
49 | - options[:only] = Array.wrap(params[:fields]) | |
49 | + fields = params[:fields] | |
50 | + fields = fields.split(',') if fields.kind_of?(String) | |
51 | + options[:only] = Array.wrap(fields) | |
50 | 52 | end |
51 | 53 | end |
52 | 54 | present model, options | ... | ... |
test/unit/api/helpers_test.rb
... | ... | @@ -209,11 +209,18 @@ class APIHelpersTest < ActiveSupport::TestCase |
209 | 209 | |
210 | 210 | should 'fallback to array when fields parameter is not a json when calling present partial' do |
211 | 211 | model = mock |
212 | - params[:fields] = 'name' | |
212 | + params[:fields] = ['name'] | |
213 | 213 | expects(:present).with(model, {:only => ['name']}) |
214 | 214 | present_partial(model, {}) |
215 | 215 | end |
216 | 216 | |
217 | + should 'fallback to comma separated string when fields parameter is not an array when calling present partial' do | |
218 | + model = mock | |
219 | + params[:fields] = 'name,description' | |
220 | + expects(:present).with(model, {:only => ['name', 'description']}) | |
221 | + present_partial(model, {}) | |
222 | + end | |
223 | + | |
217 | 224 | should 'accept json as fields parameter when calling present partial' do |
218 | 225 | model = mock |
219 | 226 | params[:fields] = {only: [:name, {user: [:login]}]}.to_json | ... | ... |