Commit 5d2a4bc5ab9e11db0e77539cac0fa49ef1ae66c3
1 parent
dbbfc9b6
Exists in
staging
and in
2 other branches
api: accept a comma separated string to represent partial fields
Showing
2 changed files
with
11 additions
and
2 deletions
Show diff stats
lib/noosfero/api/helpers.rb
... | ... | @@ -57,7 +57,9 @@ require_relative '../../find_by_contents' |
57 | 57 | options.merge!(fields.symbolize_keys.slice(:only, :except)) |
58 | 58 | end |
59 | 59 | rescue |
60 | - options[:only] = Array.wrap(params[:fields]) | |
60 | + fields = params[:fields] | |
61 | + fields = fields.split(',') if fields.kind_of?(String) | |
62 | + options[:only] = Array.wrap(fields) | |
61 | 63 | end |
62 | 64 | end |
63 | 65 | present model, options | ... | ... |
test/unit/api/helpers_test.rb
... | ... | @@ -226,11 +226,18 @@ class APIHelpersTest < ActiveSupport::TestCase |
226 | 226 | |
227 | 227 | should 'fallback to array when fields parameter is not a json when calling present partial' do |
228 | 228 | model = mock |
229 | - params[:fields] = 'name' | |
229 | + params[:fields] = ['name'] | |
230 | 230 | expects(:present).with(model, {:only => ['name']}) |
231 | 231 | present_partial(model, {}) |
232 | 232 | end |
233 | 233 | |
234 | + should 'fallback to comma separated string when fields parameter is not an array when calling present partial' do | |
235 | + model = mock | |
236 | + params[:fields] = 'name,description' | |
237 | + expects(:present).with(model, {:only => ['name', 'description']}) | |
238 | + present_partial(model, {}) | |
239 | + end | |
240 | + | |
234 | 241 | should 'accept json as fields parameter when calling present partial' do |
235 | 242 | model = mock |
236 | 243 | params[:fields] = {only: [:name, {user: [:login]}]}.to_json | ... | ... |
-
mentioned in commit 5f06c7478ca313afe0c9f969555830e7171d998b