Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1041 from boie0025/nb/refactor/content_assets
Content Assets API resource
- Loading branch information
Showing
7 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Locomotive | ||
module API | ||
module Entities | ||
|
||
class ContentAssetEntity < BaseEntity | ||
expose :content_type, :width, :height, :vignette_url, :alternative_vignette_url | ||
|
||
expose :filename do |content_asset, _| | ||
truncate(content_asset.source_filename, length: 28) | ||
end | ||
|
||
expose :short_name do |content_asset, _| | ||
truncate(content_asset.name, length: 15) | ||
end | ||
|
||
expose :extname, format_with: :truncate_to_3 | ||
|
||
|
||
expose :full_filename do |content_asset, _| | ||
content_asset.source_filename | ||
end | ||
|
||
expose :content_type_text do |content_asset, _| | ||
value = content_asset.content_type.to_s == 'other' ? content_asset.extname : content_asset.content_type | ||
value.blank? ? '?' : value | ||
end | ||
|
||
expose :with_thumbnail do |content_asset, _| | ||
%w(image pdf).include?(content_asset.content_type) | ||
end | ||
|
||
expose :raw_size do |content_asset, _| | ||
content_asset.size | ||
end | ||
|
||
expose :url do |content_asset, _| | ||
content_asset.source.url | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Locomotive | ||
module API | ||
module Forms | ||
|
||
class ContentAssetForm < BaseForm | ||
|
||
attrs :source | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
84 changes: 84 additions & 0 deletions
84
app/api/locomotive/api/resources/content_asset_resource.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
module Locomotive | ||
module API | ||
module Resources | ||
|
||
class ContentAssetResource < Grape::API | ||
|
||
resource :content_assets do | ||
entity_klass = Entities::ContentAssetEntity | ||
|
||
before do | ||
setup_resource_methods_for(:content_assets) | ||
end | ||
|
||
desc 'Index of content assets' | ||
get do | ||
authenticate_locomotive_account! | ||
authorize ContentAsset, :index? | ||
|
||
present content_assets, with: entity_klass | ||
end | ||
|
||
desc 'Show a content asset' | ||
params do | ||
requires :id, type: String, desc: 'Content asset ID' | ||
end | ||
route_param :id do | ||
get do | ||
authenticate_locomotive_account! | ||
authorize content_asset, :show? | ||
|
||
present content_asset, with: entity_klass | ||
end | ||
end | ||
|
||
desc 'Create a content asset' | ||
params do | ||
requires :content_asset, type: Hash do | ||
requires :source | ||
end | ||
end | ||
post do | ||
authorize ContentAsset, :create? | ||
form = form_klass.new(content_asset_params) | ||
persist_from_form(form) | ||
|
||
present content_asset, with: entity_klass | ||
end | ||
|
||
desc 'Update a content asset' | ||
params do | ||
requires :content_asset, type: Hash do | ||
requires :source | ||
end | ||
end | ||
put ':id' do | ||
authenticate_locomotive_account! | ||
authorize content_asset, :update? | ||
|
||
form = form_klass.new(content_asset_params) | ||
persist_from_form(form) | ||
|
||
present content_asset, with: entity_klass | ||
end | ||
|
||
desc 'Delete a content asset' | ||
params do | ||
requires :id, type: String, desc: 'Content asset ID' | ||
end | ||
delete ':id' do | ||
authenticate_locomotive_account! | ||
authorize content_asset, :destroy? | ||
|
||
content_asset.destroy | ||
|
||
present content_asset, with: entity_klass | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
104 changes: 104 additions & 0 deletions
104
spec/api/locomotive/api/entities/content_asset_entity_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
require 'spec_helper' | ||
|
||
describe Locomotive::API::Entities::ContentAssetEntity do | ||
|
||
subject { described_class } | ||
|
||
%i(content_type width height vignette_url alternative_vignette_url).each do |exposure| | ||
it { is_expected.to represent(exposure) } | ||
end | ||
describe 'overrides' do | ||
let!(:asset) { create(:asset) } | ||
|
||
subject { described_class.new(asset) } | ||
let(:exposure) { subject.serializable_hash } | ||
before { subject.object.source_filename = 'very-long-file-name-that-is-used' } | ||
|
||
describe 'full_file_name' do | ||
it 'returns the full file name' do | ||
expect(exposure[:full_filename]).to eq 'very-long-file-name-that-is-used' | ||
end | ||
end | ||
|
||
describe 'short_name' do | ||
it 'returns filename truncated to 15 chars' do | ||
expect(exposure[:short_name]).to eq 'very-long-fi...' | ||
end | ||
end | ||
|
||
describe 'extname' do | ||
it 'returns the file extension truncated to 3 chars' do | ||
subject.object.source_filename = 'foo.barbaz' | ||
expect(exposure[:extname]).to eq '...' | ||
end | ||
|
||
it 'returns the file extension if 3 chars' do | ||
subject.object.source_filename = 'foo.baz' | ||
expect(exposure[:extname]).to eq 'baz' | ||
end | ||
end | ||
|
||
describe 'content_type_text' do | ||
context 'content_type is other' do | ||
context 'file extension blank' do | ||
it 'returns a question mark' do | ||
expect(exposure[:content_type_text]).to eq '?' | ||
end | ||
end | ||
|
||
context 'file extension not blank' do | ||
before { subject.object.source_filename = 'foo.bar' } | ||
it 'returns the file extension' do | ||
expect(exposure[:content_type_text]).to eq 'bar' | ||
end | ||
end | ||
end | ||
|
||
context 'content_type is a string' do | ||
it 'returns the content_type' do | ||
subject.object.content_type = 'string' | ||
expect(exposure[:content_type_text]).to eq 'string' | ||
end | ||
end | ||
end | ||
|
||
describe 'with_thumbnail' do | ||
context 'content_type is other' do | ||
it 'returns false' do | ||
expect(exposure[:with_thumbnail]).to be_falsey | ||
end | ||
end | ||
|
||
context 'content_type is image or pdf' do | ||
before do | ||
subject.object.width = '50' | ||
subject.object.height = '50' | ||
end | ||
|
||
specify 'image returns true' do | ||
subject.object.content_type = 'image' | ||
expect(exposure[:with_thumbnail]).to be_truthy | ||
end | ||
|
||
specify 'pdf returns true' do | ||
subject.object.content_type = 'pdf' | ||
expect(exposure[:with_thumbnail]).to be_truthy | ||
end | ||
end | ||
end | ||
|
||
describe 'raw_size' do | ||
it 'returns the size' do | ||
expect(exposure[:raw_size]).to eq asset.size | ||
end | ||
end | ||
|
||
describe 'url' do | ||
it 'returns the source URL' do | ||
expect(exposure[:url]).to eq asset.source.url | ||
end | ||
end | ||
|
||
end | ||
|
||
end |
81 changes: 81 additions & 0 deletions
81
spec/api/locomotive/api/resources/content_asset_resource_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
require 'spec_helper' | ||
|
||
describe Locomotive::API::Resources::ContentAssetResource do | ||
|
||
include_context 'api site setup' | ||
|
||
let(:rack_upload) { Rack::Test::UploadedFile.new(path) } | ||
let(:path) { Rails.root.join('../../spec/fixtures/images/rails.png').to_s } | ||
|
||
let!(:asset) { create(:asset, site: site, source: rack_upload) } | ||
let(:params) { { locale: :en } } | ||
let(:url_prefix) { '/locomotive/acmi/api/v3/content_assets' } | ||
|
||
context 'authenticated site' do | ||
include_context 'api header setup' | ||
|
||
describe 'GET index' do | ||
context 'JSON' do | ||
before { get "#{url_prefix}.json" } | ||
it 'returns a successful response' do | ||
expect(last_response).to be_successful | ||
end | ||
|
||
it 'contains one asset' do | ||
expect(parsed_response.count).to eq 1 | ||
end | ||
end | ||
end | ||
|
||
describe 'GET show' do | ||
context 'JSON' do | ||
before { get "#{url_prefix}/#{asset.id}.json"} | ||
|
||
it 'returns a successful response' do | ||
expect(last_response).to be_successful | ||
end | ||
end | ||
end | ||
|
||
describe 'POST create' do | ||
context 'JSON' do | ||
let(:asset) do | ||
attributes_for(:asset).tap do |attributes| | ||
attributes[:source] = Rack::Test::UploadedFile.new( | ||
File.join(Rails.root, '..', '..', 'spec', 'fixtures', 'images', 'rails_2.png') | ||
) | ||
end | ||
end | ||
|
||
it 'creates a content_asset on the current site' do | ||
expect{ post("#{url_prefix}.json", content_asset: asset) } | ||
.to change{ Locomotive::ContentAsset.count }.by(1) | ||
end | ||
end | ||
end | ||
|
||
describe "PUT update" do | ||
context 'JSON' do | ||
let(:put_request) { put("#{url_prefix}/#{asset.id}.json", content_asset: { source: nil }) } | ||
|
||
it 'does not change the number of existing content assets' do | ||
expect{ put_request }.to_not change{ Locomotive::ContentAsset.count } | ||
end | ||
|
||
end | ||
end | ||
|
||
describe "DELETE destroy" do | ||
context 'JSON' do | ||
let(:delete_request) { delete("#{url_prefix}/#{asset.id}.json") } | ||
|
||
it 'deletes the content asset' do | ||
expect{ delete_request }.to change { Locomotive::ContentAsset.count }.by(-1) | ||
end | ||
|
||
end | ||
end | ||
|
||
end | ||
|
||
end |