Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class Api::PicturesController < ApiController | ||
before_action :set_picture_attachment, only: [:destroy] | ||
|
||
def index | ||
@pictures = current_user.pictures_blobs.order(id: :desc) | ||
render json: @pictures.each_with_object(Picture).map(&:becomes).map(&method(:index_attributes)) | ||
end | ||
|
||
def create | ||
current_user.pictures.attach params.require(:file) | ||
render :no_content, status: :created | ||
end | ||
|
||
def destroy | ||
@picture_attachment.destroy | ||
end | ||
|
||
private | ||
|
||
def set_picture_attachment | ||
@picture_attachment = UserPicturesAttachment.find(params[:id]) | ||
end | ||
|
||
def index_attributes picture | ||
a = picture.user_attachment | ||
a.attributes | ||
.slice(*%w(id created_at)) | ||
.merge(picture.slice(*%w(filename byte_size))) | ||
.merge( | ||
user: a.user.attributes, | ||
url: rails_blob_url(picture), | ||
thumbnail_url: rails_representation_url(picture.variant(resize_to_fill: [300, 300]).processed) | ||
) | ||
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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
class Picture < ActiveStorage::Blob | ||
has_one :user_pictures_attachment, foreign_key: :blob_id | ||
has_one :user, through: :user_pictures_attachment | ||
has_many :puzzle_picture_attachments, foreign_key: :blob_id | ||
has_many :puzzles, through: :puzzle_picture_attachments | ||
has_one :user_attachment, foreign_key: :blob_id, class_name: :UserPicturesAttachment | ||
has_one :user, through: :user_attachment | ||
|
||
has_many :puzzle_attachments, foreign_key: :blob_id, class_name: :PuzzlePictureAttachment | ||
has_many :puzzles, through: :puzzle_attachments | ||
|
||
def blob | ||
becomes(self.class.superclass) | ||
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
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,87 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Api::PicturesController, type: :controller do | ||
authenticate_user | ||
|
||
let(:file) { 'pictures/mountain.jpg' } | ||
let(:file_path) { fixture_path.join file } | ||
let(:file_param) { fixture_file_upload file } | ||
let(:valid_params) { | ||
{ | ||
file: file_param | ||
} | ||
} | ||
|
||
describe "GET #index" do | ||
let!(:picture_attachments) { | ||
2.times.map do | ||
current_user.pictures.attach(io: File.open(file_path), filename: File.basename(file)) | ||
end | ||
current_user.pictures.order(id: :asc).map { |a| a.becomes UserPicturesAttachment } | ||
} | ||
|
||
it "returns picture items" do | ||
get :index | ||
expect(response).to have_http_status(:ok) | ||
body = JSON.parse response.body | ||
expect(body.count).to eq 2 | ||
expect(body.map(&:keys)).to all match_array %w(id created_at filename byte_size user url thumbnail_url) | ||
end | ||
end | ||
|
||
describe "POST #create" do | ||
context "with valid params" do | ||
let(:valid_params) { | ||
{ | ||
file: fixture_file_upload(file) | ||
} | ||
} | ||
|
||
it "returns created status" do | ||
post :create, params: valid_params | ||
expect(response).to have_http_status(:created) | ||
expect(response.body).to be_blank | ||
end | ||
|
||
it "creates a Picture" do | ||
expect { | ||
post :create, params: valid_params | ||
}.to change(Picture, :count).by(1) | ||
end | ||
end | ||
|
||
context "with multiple files" do | ||
let(:valid_params) { | ||
{ | ||
file: [ fixture_file_upload(file), fixture_file_upload(file) ] | ||
} | ||
} | ||
|
||
it "creates multiple Pictures" do | ||
expect { | ||
post :create, params: valid_params | ||
}.to change(Picture, :count).by(2) | ||
end | ||
end | ||
end | ||
|
||
describe "DELETE #destroy" do | ||
let!(:picture_attachment) { | ||
current_user.pictures.attach(io: File.open(file_path), filename: File.basename(file)) | ||
current_user.pictures_attachments.last.becomes(UserPicturesAttachment) | ||
} | ||
|
||
it "destroys the picture association" do | ||
expect { | ||
delete :destroy, params: { id: picture_attachment.id } | ||
}.to change { current_user.pictures.count }.by(-1) | ||
end | ||
|
||
it "enqueues PurgeJob" do | ||
assert_enqueued_with job: ActiveStorage::PurgeJob do | ||
delete :destroy, params: { id: picture_attachment.id } | ||
end | ||
end | ||
end | ||
|
||
end |