-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
525a15c
commit 86f972c
Showing
8 changed files
with
170 additions
and
32 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
--format documentation | ||
--format progress | ||
--color | ||
--require spec_helper |
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,33 @@ | ||
# frozen_string_literal: true | ||
module Pragma | ||
module Operation | ||
# Finds the requested record, authorizes it, updates it accordingly to the parameters and | ||
# responds with the decorated record. | ||
# | ||
# @author Alessandro Desantis | ||
class Create < Pragma::Operation::Base | ||
include Pragma::Operation::Defaults | ||
|
||
def call | ||
record = build_record | ||
contract = build_contract(record) | ||
|
||
validate! contract | ||
authorize! contract | ||
|
||
contract.save | ||
|
||
respond_with resource: decorate(record) | ||
end | ||
|
||
protected | ||
|
||
# Builds the requested record. | ||
# | ||
# @return [Object] | ||
def build_record | ||
self.class.model_klass.new | ||
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,108 @@ | ||
# frozen_string_literal: true | ||
RSpec.describe Pragma::Operation::Create do | ||
subject(:context) do | ||
operation_klass.call( | ||
current_user: current_user, | ||
params: params | ||
) | ||
end | ||
|
||
let(:params) do | ||
{ | ||
author_id: 1, | ||
title: 'My Post' | ||
} | ||
end | ||
|
||
let(:contract_klass) do | ||
Class.new(Pragma::Contract::Base) do | ||
property :author_id | ||
property :title | ||
|
||
validation do | ||
required(:title).filled | ||
end | ||
end | ||
end | ||
|
||
let(:operation_klass) do | ||
Class.new(described_class) do | ||
def build_record | ||
OpenStruct.new | ||
end | ||
end.tap do |klass| | ||
klass.send(:contract, contract_klass) | ||
allow(klass).to receive(:name).and_return('API::V1::Post::Operation::Create') | ||
end | ||
end | ||
|
||
let(:current_user) { nil } | ||
|
||
it 'creates the record' do | ||
expect(context.resource.to_h).to eq( | ||
title: 'My Post', | ||
author_id: 1 | ||
) | ||
end | ||
|
||
context 'when invalid parameters are supplied' do | ||
let(:params) do | ||
{ | ||
author_id: 1, | ||
title: '' | ||
} | ||
end | ||
|
||
it 'responds with 422 Unprocessable Entity' do | ||
expect(context.status).to eq(:unprocessable_entity) | ||
end | ||
end | ||
|
||
context 'when a decorator is defined' do | ||
let(:decorator_klass) do | ||
Class.new(Pragma::Decorator::Base) do | ||
property :title | ||
end | ||
end | ||
|
||
before do | ||
operation_klass.send(:decorator, decorator_klass) | ||
end | ||
|
||
it 'decorates the updated resource' do | ||
expect(context.resource.to_hash).to eq( | ||
'title' => 'My Post' | ||
) | ||
end | ||
end | ||
|
||
context 'when a policy is defined' do | ||
let(:policy_klass) do | ||
Class.new(Pragma::Policy::Base) do | ||
def create? | ||
resource.author_id == user.id | ||
end | ||
end | ||
end | ||
|
||
before do | ||
operation_klass.send(:policy, policy_klass) | ||
end | ||
|
||
context 'when the user is authorized' do | ||
let(:current_user) { OpenStruct.new(id: 1) } | ||
|
||
it 'permits the creation' do | ||
expect(context.resource.to_h).to eq(title: 'My Post', author_id: 1) | ||
end | ||
end | ||
|
||
context 'when the user is not authorized' do | ||
let(:current_user) { OpenStruct.new(id: 2) } | ||
|
||
it 'does not permit the creation' do | ||
expect(context.status).to eq(:forbidden) | ||
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
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