-
Notifications
You must be signed in to change notification settings - Fork 1
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
9 changed files
with
290 additions
and
1 deletion.
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,49 @@ | ||
class GroupsController < AuthenticationController | ||
def create | ||
result = CreateGroup.call(group_params) | ||
|
||
if result.success? | ||
render json: result.group, status: :created | ||
else | ||
render json: result.group.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def show | ||
result = ShowGroup.call(id: params[:id]) | ||
|
||
if result.success? | ||
render json: result.group, status: :ok | ||
else | ||
render json: result.errors, status: :not_found | ||
end | ||
end | ||
|
||
def update | ||
result = UpdateGroup.call(group_params) | ||
|
||
if result.success? | ||
render json: result.group, status: :ok | ||
else | ||
render json: result.group.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
result = DeleteGroup.call(id: params[:id]) | ||
unless result.success? | ||
render json: result.errors, status: :internal_server_error | ||
end | ||
end | ||
|
||
private | ||
|
||
def group_params | ||
params.require(:group).permit(:name, | ||
:city, | ||
:state, | ||
:country, | ||
:facebook, | ||
:twitter) | ||
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,2 @@ | ||
class CreateGroup < StandardInteraction | ||
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,3 @@ | ||
class DeleteGroup | ||
include Interactor | ||
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,3 @@ | ||
class ShowGroup | ||
include Interactor | ||
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,3 @@ | ||
class UpdateGroup | ||
include Interactor | ||
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,3 @@ | ||
class GroupSerializer < ActiveModel::Serializer | ||
attributes :id, :name, :city, :state, :country, :facebook, :twitter | ||
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,225 @@ | ||
RSpec.describe GroupsController do | ||
let(:user) { create(:confirmed_user) } | ||
let(:group) { create(:group) } | ||
|
||
before do | ||
authenticate | ||
end | ||
|
||
describe "POST #create" do | ||
let(:params) { { group: create_group_input } } | ||
|
||
let(:create_group_input) do | ||
{ name: group.name } | ||
end | ||
|
||
let(:create_group_context) do | ||
Interactor::Context.new(errors: :val, group: group) | ||
end | ||
|
||
before do | ||
allow(CreateGroup).to receive(:call).with(create_group_input). | ||
and_return(create_group_context) | ||
end | ||
|
||
context "when called" do | ||
it "calls the CreateGroup interactor" do | ||
expect(CreateGroup).to receive(:call) | ||
post :create, params | ||
end | ||
end | ||
|
||
context "when CreateGroup is a success" do | ||
let(:serializer) { GroupSerializer.new(group) } | ||
|
||
let(:serialization) do | ||
ActiveModel::Serializer::Adapter.create(serializer) | ||
end | ||
|
||
it "returns HTTP status 201" do | ||
post :create, params | ||
expect(response).to have_http_status(201) | ||
end | ||
|
||
it "renders the group as JSON" do | ||
post :create, params | ||
expect(serialize(group)).to eq(response.body) | ||
end | ||
end | ||
|
||
context "when CreateGroup is a failure" do | ||
let(:create_group_context) do | ||
double(:context, success?: false, group: group) | ||
end | ||
|
||
it "returns HTTP status 422" do | ||
post :create, params | ||
expect(response).to have_http_status(422) | ||
end | ||
|
||
it "returns an error" do | ||
group.errors.add(:name, "error") | ||
post :create, params | ||
expect(json["name"]).to eq(["error"]) | ||
end | ||
end | ||
end | ||
|
||
describe "GET #show" do | ||
let(:group) { create(:group) } | ||
let(:params) { { id: group.id } } | ||
let(:show_group_input) { { id: params.fetch(:id).to_s } } | ||
|
||
let(:show_group_context) do | ||
Interactor::Context.new(errors: :val, group: group) | ||
end | ||
|
||
before do | ||
allow(ShowGroup).to receive(:call).with(show_group_input). | ||
and_return(show_group_context) | ||
end | ||
|
||
context "when called" do | ||
it "calls the ShowGroup Interactor" do | ||
expect(ShowGroup).to receive(:call) | ||
get :show, params | ||
end | ||
end | ||
|
||
context "when ShowGroup is successful" do | ||
it "returns HTTP status 200" do | ||
get :show, params | ||
expect(response).to have_http_status(200) | ||
end | ||
|
||
it "renders the group as JSON" do | ||
get :show, params | ||
expect(serialize(group)).to eq(response.body) | ||
end | ||
end | ||
|
||
context "when ShowGroup is a failure" do | ||
let(:show_group_context) do | ||
double(:context, | ||
errors: { name: ["invalid"] }, | ||
success?: false, | ||
group: group) | ||
end | ||
|
||
it "returns HTTP status 404" do | ||
get :show, params | ||
expect(response).to have_http_status(404) | ||
end | ||
|
||
it "renders an error" do | ||
get :show, params | ||
expect(json["name"]).to eq(["invalid"]) | ||
end | ||
end | ||
end | ||
|
||
describe "PATCH #update" do | ||
let(:params) { { id: group.id, group: update_group_input } } | ||
|
||
let(:update_group_input) do | ||
{ name: group.name } | ||
end | ||
|
||
let(:update_group_context) do | ||
Interactor::Context.new(errors: :val, group: group) | ||
end | ||
|
||
before do | ||
allow(UpdateGroup).to receive(:call).with(update_group_input). | ||
and_return(update_group_context) | ||
end | ||
|
||
context "when called" do | ||
it "calls the UpdateGroup Interactor" do | ||
expect(UpdateGroup).to receive(:call) | ||
patch :update, params | ||
end | ||
end | ||
|
||
context "when UpdateGroup is successful" do | ||
it "returns HTTP status 200" do | ||
patch :update, params | ||
expect(response).to have_http_status(200) | ||
end | ||
|
||
it "renders the group as JSON" do | ||
patch :update, params | ||
expect(serialize(group)).to eq(response.body) | ||
end | ||
end | ||
|
||
context "when UpdateGroup is a failure" do | ||
let(:update_group_context) do | ||
double(:context, success?: false, group: group) | ||
end | ||
|
||
it "returns HTTP status 422" do | ||
patch :update, params | ||
expect(response).to have_http_status(422) | ||
end | ||
|
||
it "renders an error" do | ||
group.errors.add(:name, "error") | ||
patch :update, params | ||
expect(json["name"]).to eq(["error"]) | ||
end | ||
end | ||
end | ||
|
||
describe "DELETE #destroy" do | ||
let(:params) do | ||
{ id: group.id } | ||
end | ||
|
||
let(:delete_group_input) do | ||
{ id: params.fetch(:id).to_s } | ||
end | ||
|
||
let(:delete_group_context) do | ||
Interactor::Context.new(errors: :val, group: group) | ||
end | ||
|
||
before do | ||
allow(DeleteGroup).to receive(:call).with(delete_group_input). | ||
and_return(delete_group_context) | ||
end | ||
|
||
context "when called" do | ||
it "calls the DeleteGroup Interactor" do | ||
expect(DeleteGroup).to receive(:call) | ||
delete :destroy, params | ||
end | ||
end | ||
|
||
context "when DeleteGroup is successful" do | ||
it "returns HTTP status 200" do | ||
delete :destroy, params | ||
expect(response).to have_http_status(200) | ||
end | ||
end | ||
|
||
context "when DeleteGroup is a failure" do | ||
let(:delete_group_context) do | ||
double(:context, | ||
errors: { id: ["invalid"] }, | ||
success?: false, | ||
group: group) | ||
end | ||
|
||
it "returns HTTP status 500" do | ||
delete :destroy, params | ||
expect(response).to have_http_status(500) | ||
end | ||
|
||
it "renders an error" do | ||
delete :destroy, params | ||
expect(json["id"]).to eq(["invalid"]) | ||
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