parent
05001d54d1
commit
ae1fac0062
@ -0,0 +1,3 @@ |
||||
// Place all the styles related to the Api::Media controller here. |
||||
// They will automatically be included in application.css. |
||||
// You can use Sass (SCSS) here: http://sass-lang.com/ |
@ -0,0 +1,8 @@ |
||||
class Api::MediaController < ApiController |
||||
before_action :doorkeeper_authorize! |
||||
respond_to :json |
||||
|
||||
def create |
||||
@media = MediaAttachment.create!(account: current_user.account, file: params[:file]) |
||||
end |
||||
end |
@ -0,0 +1,2 @@ |
||||
module Api::MediaHelper |
||||
end |
@ -0,0 +1,13 @@ |
||||
class MediaAttachment < ApplicationRecord |
||||
belongs_to :account, inverse_of: :media_attachments |
||||
belongs_to :status, inverse_of: :media_attachments |
||||
|
||||
has_attached_file :file |
||||
validates_attachment_content_type :file, content_type: /\Aimage\/.*\z/ |
||||
|
||||
validates :account, presence: true |
||||
|
||||
def local? |
||||
self.remote_url.blank? |
||||
end |
||||
end |
@ -0,0 +1,3 @@ |
||||
object @media |
||||
attribute :id |
||||
node(:url) { |media| full_asset_url(media.file.url) } |
@ -0,0 +1,14 @@ |
||||
class CreateMediaAttachments < ActiveRecord::Migration[5.0] |
||||
def change |
||||
create_table :media_attachments do |t| |
||||
t.integer :status_id, null: true, default: nil |
||||
t.attachment :file |
||||
t.string :remote_url, null: false, default: '' |
||||
t.integer :account_id |
||||
|
||||
t.timestamps |
||||
end |
||||
|
||||
add_index :media_attachments, :status_id |
||||
end |
||||
end |
@ -0,0 +1,34 @@ |
||||
require 'rails_helper' |
||||
|
||||
RSpec.describe Api::MediaController, type: :controller do |
||||
render_views |
||||
|
||||
let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) } |
||||
let(:token) { double acceptable?: true, resource_owner_id: user.id } |
||||
|
||||
before do |
||||
allow(controller).to receive(:doorkeeper_token) { token } |
||||
end |
||||
|
||||
describe 'POST #create' do |
||||
before do |
||||
post :create, params: { file: fixture_file_upload('files/attachment.jpg', 'image/jpeg') } |
||||
end |
||||
|
||||
it 'returns http success' do |
||||
expect(response).to have_http_status(:success) |
||||
end |
||||
|
||||
it 'creates a media attachment' do |
||||
expect(MediaAttachment.first).to_not be_nil |
||||
end |
||||
|
||||
it 'uploads a file' do |
||||
expect(MediaAttachment.first).to have_attached_file(:file) |
||||
end |
||||
|
||||
it 'returns media ID in JSON' do |
||||
expect(body_as_json[:id]).to eq MediaAttachment.first.id |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,6 @@ |
||||
Fabricator(:media_attachment) do |
||||
status_id 1 |
||||
file "" |
||||
remote_url "MyString" |
||||
account_id 1 |
||||
end |
After Width: | Height: | Size: 60 KiB |
@ -0,0 +1,15 @@ |
||||
require 'rails_helper' |
||||
|
||||
# Specs in this file have access to a helper object that includes |
||||
# the Api::MediaHelper. For example: |
||||
# |
||||
# describe Api::MediaHelper do |
||||
# describe "string concat" do |
||||
# it "concats two strings with spaces" do |
||||
# expect(helper.concat_strings("this","that")).to eq("this that") |
||||
# end |
||||
# end |
||||
# end |
||||
RSpec.describe Api::MediaHelper, type: :helper do |
||||
pending "add some examples to (or delete) #{__FILE__}" |
||||
end |
@ -0,0 +1,5 @@ |
||||
require 'rails_helper' |
||||
|
||||
RSpec.describe MediaAttachment, type: :model do |
||||
pending "add some examples to (or delete) #{__FILE__}" |
||||
end |
Loading…
Reference in new issue