Skip to content

Commit

Permalink
adds attachment component
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmaciel committed Apr 8, 2018
1 parent ee03761 commit 03538f0
Show file tree
Hide file tree
Showing 21 changed files with 212 additions and 14 deletions.
24 changes: 24 additions & 0 deletions app/graphql/mutations/attachment_mutation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module AttachmentMutation
Create = GraphQL::Relay::Mutation.define do
name 'CreateAttachment'
description 'Creates a Attachment'

input_field :sourceId, !types.ID, 'Source ID'
input_field :name, types.String, 'Attachment name'
input_field :url, !types.String, 'Attachment url'
return_field :attachment, Types::AttachmentType, 'Returns information about the new attachment'

resolve Resolvers::Attachments::Create
end

Destroy = GraphQL::Relay::Mutation.define do
name 'DestroyAttachment'
description 'Updates a Attachment'

input_field :id, !types.ID, 'The Attachment ID'

return_field :success, types.Boolean, 'Returns operation status'

resolve Resolvers::Attachments::Destroy
end
end
1 change: 1 addition & 0 deletions app/graphql/mutations/video_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module VideoInput

argument :title, types.String, 'The video title'
argument :description, types.String, 'The video description'
argument :script, types.String, 'The video script'
argument :url, types.String, 'The video URL'
argument :labels, types[types.String], 'Labels'
argument :system_id, types.ID, 'The video ID'
Expand Down
22 changes: 22 additions & 0 deletions app/graphql/resolvers/attachments/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Resolvers
module Attachments
module Create
class << self
def call(_, input, context)
new_attachment = Attachment.new(
name: input[:name],
url: input[:url],
source_id: input[:sourceId],
source_type: Video,
created_by: context[:current_user]
)

context[:current_user].authorize!(:create, new_attachment)
new_attachment.save!

{ attachment: new_attachment }
end
end
end
end
end
14 changes: 14 additions & 0 deletions app/graphql/resolvers/attachments/destroy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Resolvers
module Attachments
module Destroy
class << self
def call(_, input, context)
attachment_to_delete = Attachment.find(input[:id])
context[:current_user].authorize!(:destroy, attachment_to_delete)

{ success: attachment_to_delete.destroy! }
end
end
end
end
end
6 changes: 4 additions & 2 deletions app/graphql/resolvers/systems/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module Systems
module Update
class << self
def call(_, input, context)
system_to_update = Task.find(input[:id])
system_to_update = System.find(input[:id])

system_to_update.name = input[:name] if input[:name].present?
input[:systemAttributes].to_h.each do |attribute, value|
system_to_update.send("#{attribute}=", value)
end

context[:current_user].authorize!(:update, system_to_update)
system_to_update.save!
Expand Down
8 changes: 8 additions & 0 deletions app/graphql/types/attachment_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Types::AttachmentType = GraphQL::ObjectType.define do
name 'Attachment'

field :id, types.ID
field :name, types.String
field :url, types.String
field :created_by, Types::UserType
end
3 changes: 3 additions & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
field :assignVideo, field: VideoMutation::Assign.field
field :unassignVideo, field: VideoMutation::Unassign.field

field :createAttachment, field: AttachmentMutation::Create.field
field :destroyAttachment, field: AttachmentMutation::Destroy.field

field :postComment, field: CommentMutation::Post.field
field :editComment, field: CommentMutation::Edit.field
field :destroyComment, field: CommentMutation::Destroy.field
Expand Down
13 changes: 13 additions & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@
end
end


field :attachments, types[Types::AttachmentType] do
description 'Get attachments'
argument :videoId, !types.ID, 'Video ID'
resolve ->(_, input, context) do
video = Video.find(input[:videoId])

context[:current_user].authorize!(:video_attachments, video)

video.attachments
end
end

field :tutormakers, types[Types::UserType] do
description 'Get organizations'
resolve ->(_, input, context) do
Expand Down
1 change: 1 addition & 0 deletions app/graphql/types/video_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
field :id, types.ID
field :title, types.String
field :description, types.String
field :script, types.String
field :url, types.String
field :version, types.String
field :aasm_state, types.String
Expand Down
5 changes: 5 additions & 0 deletions app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ class Attachment < ApplicationRecord
belongs_to :created_by, class_name: 'User', foreign_key: :created_by_id

acts_as_paranoid

def name
return url.split('/').last if self[:name].blank?
self[:name]
end
end
33 changes: 26 additions & 7 deletions app/policies/access_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ def configure
can :manage, Organization
can :manage, Task
can :manage, System
can :manage, Attachment
can :read_collection, Organization
can :read_collection, User
can :read_tutormakers, User
can :read_collection, Video
can :read_collection, Attachment
can [:cancel_video, :send_request], Video
can :read_comments, Video
can [:read_comments, :video_attachments], Video
can [:post, :edit, :destroy], Comment
end

Expand All @@ -26,18 +28,23 @@ def configure
target_comment.id == current_user.organization_id
end

can [:read_comments], Video do |target_video, current_user|
can [:read_comments, :video_attachments], Video do |target_video, current_user|
current_user.video_ids.include?(target_video.id)
end
end

role :video_producer, user_role: 'video_producer' do
can [:post, :edit, :destroy], Comment do |target_comment, current_user|
current_user.video_ids.include?(target_comment.video_id) &&
target_comment.id == current_user.organization_id
target_comment.video.system.organization_id == current_user.organization_id
end

can [:read_comments], Video do |target_video, current_user|
can [:create, :destroy], Attachment do |target_attatchment, current_user|
current_user.video_ids.include?(target_attatchment.video_id) &&
target_comment.video.source.organization_id == current_user.organization_id
end

can [:read_comments, :video_attachments], Video do |target_video, current_user|
current_user.video_ids.include?(target_video.id)
end
end
Expand All @@ -51,6 +58,10 @@ def configure
target_video.system.organization_id == current_user.organization_id
end

can [:create, :destroy], Attachment do |target_attatchment, current_user|
target_attatchment.source.system.organization_id == current_user.organization_id
end

can [:update, :destroy, :read], Organization do |target_organization, current_user|
target_organization.id == current_user.organization_id
end
Expand All @@ -63,7 +74,7 @@ def configure
task_target.video.system.organization_id == current_user.organization_id
end

can [:read_comments], Video do |target_video, current_user|
can [:read_comments, :video_attachments], Video do |target_video, current_user|
target_video.system.organization_id == current_user.organization_id
end
end
Expand All @@ -85,9 +96,13 @@ def configure
task_target.video.system_id == current_user.system_id
end

can [:read_comments], Video do |target_video, current_user|
can [:read_comments, :video_attachments], Video do |target_video, current_user|
target_video.system_id == current_user.system_id
end

can [:create, :destroy], Attachment do |target_attatchment, current_user|
target_attatchment.source.system.organization_id == current_user.organization_id
end
end

role :system_member, user_role: 'system_member' do
Expand All @@ -110,10 +125,14 @@ def configure
task_target.author_id == current_user.id
end

can [:read_comments], Video do |target_video, current_user|
can [:read_comments, :video_attachments], Video do |target_video, current_user|
current_user.video_ids.include?(target_video.id) &&
target_video.system_id == current_user.system_id
end

can [:create, :destroy], Attachment do |target_attatchment, current_user|
target_attatchment.source.system.organization_id == current_user.organization_id
end
end
end
end
2 changes: 1 addition & 1 deletion db/migrate/20180215010114_create_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def change
t.belongs_to :system, foreign_key: true

t.string :name, null: false
t.string :email, null: false, unique: true, index: true
t.string :email, null: false, unique: true
t.string :password_digest, null: false
t.integer :user_role, null: false
t.string :cellphone
Expand Down
3 changes: 2 additions & 1 deletion db/migrate/20180215010115_create_videos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class CreateVideos < ActiveRecord::Migration[5.1]
def change
create_table :videos do |t|
t.string :title, null: false
t.string :description
t.text :description
t.text :script
t.integer :version, default: 0
t.string :url
t.string :aasm_state
Expand Down
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@

create_table "videos", force: :cascade do |t|
t.string "title", null: false
t.string "description"
t.text "description"
t.text "script"
t.integer "version", default: 0
t.string "url"
t.string "aasm_state"
Expand Down
3 changes: 2 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

User.create!(name: 'User Admin',email: '[email protected]' , password: '123123123' , password_confirmation: '123123123', user_role: :admin, organization: organization)
User.create!(name: 'User script writer',email: '[email protected]' , password: '123123123' , password_confirmation: '123123123', user_role: :script_writer, organization: organization)
user = User.create!(name: 'User system admin',email: 'user_role_admin@mail.com' , password: '123123123' , password_confirmation: '123123123', user_role: :system_admin, system_role_params: [{ first_system.id => 'admin'}], organization: organization)
user = User.create!(name: 'User system admin',email: 'user_role_admin2@mail.com' , password: '123123123' , password_confirmation: '123123123', user_role: :system_admin, system_role_params: [{ first_system.id => 'admin'}], organization: organization)

video = Video.create(title: 'Default Video 1', system: first_system, created_by: user, url: 'https://media.w3.org/2010/05/sintel/trailer_hd.mp4')
Comment.create(video: video, author: user, comment_destination: 'administrative', body: 'Comment test')
Task.create(video: video, created_by: user, name: 'Improve ths video')
Attachment.create(source: video, source_type: Video, created_by: user, url: '/fake/url')

organization = Organization.create!(name: 'Tutorbox')
User.create!(name: 'Jon',email: '[email protected]' , password: '123123123' , password_confirmation: '123123123', user_role: :admin, organization: organization)
6 changes: 6 additions & 0 deletions spec/fixtures/attachments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
default_attachment:
name: 'attachment'
url: '/fake/url'
source: default_video_1
source_type: Video
created_by: user_admin
15 changes: 15 additions & 0 deletions spec/graphql/mutations/attachment_mutation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

describe AttachmentMutation do
describe AttachmentMutation::Create do
it { is_expected.to have_an_input_field(:name).of_type('String!') }
it { is_expected.to have_an_input_field(:url).of_type('String!') }
it { is_expected.to have_an_input_field(:sourceId).of_type('ID!') }
it { is_expected.to have_a_return_field(:attachment).returning(Types::AttachmentType) }
end

describe AttachmentMutation::Destroy do
it { is_expected.to have_an_input_field(:id).of_type('ID!') }
it { is_expected.to have_a_return_field(:success).returning('Boolean') }
end
end
36 changes: 36 additions & 0 deletions spec/graphql/resolvers/attachments/create_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'rails_helper'

describe Resolvers::Attachments::Create do
let(:current_user) { users(:user_organization_admin) }
let(:video) { videos(:default_video_2) }
let(:input) {
{
name: 'task',
url: '/fake/url',
sourceId: video.id,
}
}
subject(:result) { described_class::call(nil, input, current_user: current_user ) }

describe '#call' do
context 'when the attachment has been created' do
let(:new_attachment) { result[:attachment] }

it 'creates attachment with its rigth attributes' do
expect(new_attachment).to be_persisted
expect(new_attachment.name).to eql input[:name]
expect(new_attachment.url).to eql input[:url]
expect(new_attachment.source_id).to eql input[:sourceId]
expect(new_attachment.created_by).to eql current_user
end
end

context 'when the attachment has not been created' do
let(:current_user) { users(:software_house_admin) }

it 'does not create a attachment and returns error' do
expect { subject }.to raise_error(Exceptions::PermissionDeniedError)
end
end
end
end
25 changes: 25 additions & 0 deletions spec/graphql/resolvers/attachments/destroy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'rails_helper'

describe Resolvers::Attachments::Destroy do
let(:current_user) { users(:user_organization_admin) }
let(:target_attachment) { attachments(:default_attachment) }

subject(:result) { described_class::call(nil, { id: target_attachment.id } , current_user: current_user ) }

describe '#call' do
context 'when the attachment has been destroyed' do

it 'destroys attachment with its rigth attributes' do
expect { result }.to change { Attachment.count }.by(-1)
end
end

context 'when the attachment has not been destroyed' do
let(:current_user) { users(:software_house_admin) }

it 'does not destroy a user and returns error' do
expect { subject }.to raise_error(Exceptions::PermissionDeniedError)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/graphql/resolvers/comments/destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
end
end
end
end
end
1 change: 1 addition & 0 deletions spec/graphql/types/video_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
it { is_expected.to have_field(:id).of_type('ID') }
it { is_expected.to have_field(:title).of_type('String') }
it { is_expected.to have_field(:description).of_type('String') }
it { is_expected.to have_field(:script).of_type('String') }
it { is_expected.to have_field(:url).of_type('String') }
it { is_expected.to have_field(:version).of_type('String') }
it { is_expected.to have_field(:aasm_state).of_type('String') }
Expand Down

0 comments on commit 03538f0

Please sign in to comment.