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,7 +46,9 @@ require_relative '../../find_by_contents' | ||
46 | options.merge!(fields.symbolize_keys.slice(:only, :except)) | 46 | options.merge!(fields.symbolize_keys.slice(:only, :except)) |
47 | end | 47 | end |
48 | rescue | 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 | end | 52 | end |
51 | end | 53 | end |
52 | present model, options | 54 | present model, options |
test/unit/api/helpers_test.rb
@@ -209,11 +209,18 @@ class APIHelpersTest < ActiveSupport::TestCase | @@ -209,11 +209,18 @@ class APIHelpersTest < ActiveSupport::TestCase | ||
209 | 209 | ||
210 | should 'fallback to array when fields parameter is not a json when calling present partial' do | 210 | should 'fallback to array when fields parameter is not a json when calling present partial' do |
211 | model = mock | 211 | model = mock |
212 | - params[:fields] = 'name' | 212 | + params[:fields] = ['name'] |
213 | expects(:present).with(model, {:only => ['name']}) | 213 | expects(:present).with(model, {:only => ['name']}) |
214 | present_partial(model, {}) | 214 | present_partial(model, {}) |
215 | end | 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 | should 'accept json as fields parameter when calling present partial' do | 224 | should 'accept json as fields parameter when calling present partial' do |
218 | model = mock | 225 | model = mock |
219 | params[:fields] = {only: [:name, {user: [:login]}]}.to_json | 226 | params[:fields] = {only: [:name, {user: [:login]}]}.to_json |