Skip to content

Commit

Permalink
Merge f1c7aba into 8669034
Browse files Browse the repository at this point in the history
  • Loading branch information
npauzenga committed Jan 30, 2016
2 parents 8669034 + f1c7aba commit 3a87d64
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 23 deletions.
8 changes: 4 additions & 4 deletions app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class GroupsController < AuthenticationController
def create
result = CreateGroup.call(group_params)
result = CreateGroup.call(user: current_user, group_params: group_params)

if result.success?
render json: result.group, status: :created
else
render json: result.group.errors, status: :unprocessable_entity
render json: result.errors, status: :unprocessable_entity
end
end

Expand All @@ -20,12 +20,12 @@ def show
end

def update
result = UpdateGroup.call(group_params)
result = UpdateGroup.call(id: params[:id], group_params: group_params)

if result.success?
render json: result.group, status: :ok
else
render json: result.group.errors, status: :unprocessable_entity
render json: result.errors, status: :unprocessable_entity
end
end

Expand Down
11 changes: 11 additions & 0 deletions app/interactors/create_group.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
class CreateGroup < StandardInteraction
def validate_input
context.fail!(errors: "invalid input") unless context.group_params
end

def execute
context.group = context.user.groups.create(context.group_params)
end

def validate_output
context.fail!(errors: context.group.errors) unless context.group.persisted?
end
end
14 changes: 12 additions & 2 deletions app/interactors/delete_group.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
class DeleteGroup
include Interactor
class DeleteGroup < StandardInteraction
def validate_input
context.fail!(errors: "invalid input") unless context.id
end

def execute
context.group = Group.destroy(context.id)
end

def validate_output
context.fail!(errors: "group not deleted") unless context.group.destroyed?
end
end
14 changes: 12 additions & 2 deletions app/interactors/show_group.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
class ShowGroup
include Interactor
class ShowGroup < StandardInteraction
def validate_input
context.fail!(errors: "invalid input") unless context.id
end

def execute
context.group = Group.find_by(id: context.id)
end

def validate_output
context.fail!(errors: "invalid output") unless context.group
end
end
20 changes: 18 additions & 2 deletions app/interactors/update_group.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
class UpdateGroup
include Interactor
class UpdateGroup < StandardInteraction
def validate_input
context.fail!(errors: "invalid input") unless valid_input?
end

def execute
context.fail!(errors: "group update failed") unless update_group
end

private

def update_group
Group.update(context.id, context.group_params)
end

def valid_input?
context.group_params && context.id
end
end
20 changes: 8 additions & 12 deletions spec/controllers/groups_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
end

describe "POST #create" do
let(:params) { { group: create_group_input } }
let(:params) { { group: create_group_input.fetch(:group_params) } }

let(:create_group_input) do
{ name: group.name }
{ user: user, group_params: { name: group.name } }
end

let(:create_group_context) do
Expand All @@ -30,12 +30,6 @@
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)
Expand All @@ -49,7 +43,7 @@

context "when CreateGroup is a failure" do
let(:create_group_context) do
double(:context, success?: false, group: group)
double(:context, success?: false, errors: group.errors, group: group)
end

it "returns HTTP status 422" do
Expand Down Expand Up @@ -119,10 +113,12 @@
end

describe "PATCH #update" do
let(:params) { { id: group.id, group: update_group_input } }
let(:params) do
{ id: group.id, group: update_group_input.fetch(:group_params) }
end

let(:update_group_input) do
{ name: group.name }
{ id: group.id.to_s, group_params: { name: group.name } }
end

let(:update_group_context) do
Expand Down Expand Up @@ -155,7 +151,7 @@

context "when UpdateGroup is a failure" do
let(:update_group_context) do
double(:context, success?: false, group: group)
double(:context, success?: false, errors: group.errors, group: group)
end

it "returns HTTP status 422" do
Expand Down
7 changes: 6 additions & 1 deletion spec/factories/group.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
FactoryGirl.define do
factory :group do
name Faker::Hipster.words(2)
name Faker::Team.name
city Faker::Address.city
state Faker::Address.state
country Faker::Address.country
facebook Faker::Internet.url("facebook.com")
twitter Faker::Internet.url("twitter.com")
end
end
60 changes: 60 additions & 0 deletions spec/interactors/create_group_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
RSpec.describe CreateGroup do
describe ".call" do
let(:user) do
create(:confirmed_user)
end

let(:group_params) do
{
name: "Weasley's Kneesleys",
city: "Baltimore",
state: "MD",
country: "USA",
facebook: "http://facebook.com/weasley",
twitter: "http://twitter.com/weasley"
}
end

subject do
described_class.call(user: user, group_params: group_params)
end

context "when successful" do
it "returns a successful context" do
is_expected.to be_a_success
end

it "creates a new group" do
expect(subject.group).to be_a Group
end
end

context "when group is not saved" do
before do
group_params["name"] = ""
end

it "fails" do
is_expected.to be_a_failure
end

it "returns an error" do
expect(subject.errors.messages).to eq(name: ["can't be blank"])
end
end

context "when invalid input is provided" do
subject do
described_class.call(group_params: nil)
end

it "fails" do
is_expected.to be_a_failure
end

it "returns an error" do
expect(subject.errors).to eq("invalid input")
end
end
end
end
51 changes: 51 additions & 0 deletions spec/interactors/delete_group_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
RSpec.describe DeleteGroup do
describe ".call" do
let(:group) { create(:group) }

context "when successful" do
subject do
described_class.call(id: group.id)
end

it "returns a successful context" do
is_expected.to be_a_success
end

it "deletes the group record" do
expect(subject.group.destroyed?).to be_truthy
end
end

context "when group id is invalid" do
subject do
described_class.call(id: nil)
end

it "fails" do
is_expected.to be_a_failure
end

it "returns an error" do
expect(subject.errors).to eq("invalid input")
end
end

context "when destroy fails" do
before do
allow_any_instance_of(Group).to receive(:destroyed?).and_return(false)
end

subject do
described_class.call(id: group.id)
end

it "fails" do
is_expected.to be_a_failure
end

it "returns an error" do
expect(subject.errors).to eq("group not deleted")
end
end
end
end
47 changes: 47 additions & 0 deletions spec/interactors/show_group_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
RSpec.describe ShowGroup do
describe ".call" do
let(:group) { create(:group, id: 876) }

context "when successful" do
subject do
described_class.call(id: group.id)
end

it "returns a successful context" do
is_expected.to be_a_success
end

it "returns a valid group record" do
expect(subject.group).to be_a Group
end
end

context "when invalid id is provided" do
subject do
described_class.call(id: nil)
end

it "fails" do
is_expected.to be_a_failure
end

it "returns an error" do
expect(subject.errors).to eq("invalid input")
end
end

context "when a Group was not found" do
subject do
described_class.call(id: 9999)
end

it "fails" do
is_expected.to be_a_failure
end

it "returns an error" do
expect(subject.errors).to eq("invalid output")
end
end
end
end
Loading

0 comments on commit 3a87d64

Please sign in to comment.