Skip to content

Commit

Permalink
Allow for specifying of additional attributes for pictures
Browse files Browse the repository at this point in the history
  • Loading branch information
Jillian Tullo committed Feb 22, 2018
1 parent 9fcf7a1 commit f5351b7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
20 changes: 18 additions & 2 deletions app/controllers/api/mixins/pictures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,24 @@ def fetch_picture(resource)
end

def format_picture_response(picture)
return unless picture
picture.attributes.except('content').merge('image_href' => picture.image_href)
picture_attrs = picture.attributes.except('content')
picture_attrs['image_href'] = picture.image_href
picture_attrs['extension'] = picture.extension
picture_attrs['href'] = picture.href_slug

if @req.collection == "pictures"
return picture_attrs if attribute_selection == "all"
additional_attrs = validate_attr_selection(picture).flatten
additional_attrs.each do |attr|
picture_attrs[attr] = picture.public_send(attr)
end
picture_attrs.slice(*(additional_attrs + BaseController::ID_ATTRS))
else
attribute_selection_for("picture").each do |attr|
picture_attrs[attr] = picture.public_send(attr)
end
picture_attrs
end
end
end
end
Expand Down
16 changes: 9 additions & 7 deletions app/controllers/api/pictures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ module Api
class PicturesController < BaseController
include Api::Mixins::Pictures

before_action :set_additional_attributes, :only => [:index, :show]
def index
pictures = Picture.all.collect { |picture| format_picture_response(picture) }
render_resource(:picture, :name => "pictures", :count => pictures.count, :subcount => pictures.count, :resources => pictures)
end

def show
picture = resource_search(@req.collection_id, :pictures, Picture)
render_resource(:picture, format_picture_response(picture), {})
end

def create_resource(_type, _id, data)
picture = Picture.create_from_base64(data)
format_picture_response(picture)
rescue => err
raise BadRequestError, "Failed to create Picture - #{err}"
end

private

def set_additional_attributes
@additional_attributes = %w(image_href extension)
end
end
end
33 changes: 33 additions & 0 deletions spec/requests/pictures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,29 @@ def expect_result_to_include_picture_href(source_id)
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it 'allows specifying of additional attributes' do
api_basic_authorize

expected = {
'resources' => [
a_hash_including('href_slug' => @picture.href_slug,
'region_number' => @picture.region_number)
]
}
get(api_pictures_url, :params => { :expand => 'resources', :attributes => 'href_slug,region_number'})

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it 'only allows specifying of valid attributes' do
api_basic_authorize

get(api_pictures_url, :params => { :expand => 'resources', :attributes => 'bad_attr'})

expect(response).to have_http_status(:bad_request)
end
end

describe 'GET /api/pictures/:id' do
Expand All @@ -109,6 +132,16 @@ def expect_result_to_include_picture_href(source_id)
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include('image_href' => a_string_including(@picture.image_href), 'extension' => @picture.extension)
end

it 'allows specifying of additional attributes' do
api_basic_authorize

get(api_picture_url(nil, @picture), :params => { :attributes => 'href_slug,region_number' })

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include('href_slug' => @picture.href_slug,
'region_number' => @picture.region_number)
end
end

describe 'POST /api/pictures' do
Expand Down

0 comments on commit f5351b7

Please sign in to comment.