Skip to content

Instantly share code, notes, and snippets.

@kulte

kulte/create_project.rb

Last active Aug 29, 2015
Embed
What would you like to do?
interactor-demo
class CreateProject
include Interactor
def call
context.project = context.controller.current_user.projects.build(context.project_params)
if context.project.save
context.notice = 'Your project was created successfully.'
send_emails_for(context.project)
else
context.fail!(notice: context.project.errors.full_messages)
end
end
private
def send_emails_for(project)
AdminNotifier.project_created(project: @project).deliver
UserNotifier.project_created(project: @project).deliver
DesignerNotifier.project_created(project: @project).deliver
end
end
require 'spec_helper'
describe CreateProject do
describe '#call' do
let(:user) { double(:user) }
let(:interactor) do
CreateProject.new(
controller: double(:controller, current_user: user),
project_params: attributes_for(:project)
)
end
let(:context) { interactor.context }
context 'when given valid attributes' do
let(:project) { create(:project) }
before do
allow(Project).to recieve(:save)
end
end
context 'when given invalid attributes' do
end
end
end
class ProjectsController < ApplicationController
before_filter :require_login
def create
result = CreateProject.call(controller: self, project_params: project_params)
if result.success?
redirect_to project_path(result.project), notice: result.notice
else
render :new, notice: result.notice
end
end
end
class ProjectsController < ApplicationController
before_filter :require_login
def create
@project = current_user.projects.build(project_params)
if @project.save
AdminNotifier.project_created(project: @project).deliver
UserNotifier.project_created(project: @project).deliver
DesignerNotifier.project_created(project: @project).deliver
redirect_to project_path(@project), notice: 'Your project was created successfully.'
else
render :new, notice: @project.errors.full_messages.join(', ')
end
end
end
require 'spec_helper'
describe ProjectsController do
describe '#create' do
before do
sign_in_as(user)
expect(CreateProject).to receive(:call).once.with(
controller: controller,
project_params: project_params
).and_return(context)
end
context 'when successful' do
let(:user) { double(:user) }
let(:project) { double(:project) }
let(:context) { double(:context, success?: true, notice: 'Your project was created successfully.', project: project) }
let(:project_params) { attributes_for(:project) }
it 'redirects to the project page' do
expect do
post :create, project_params
end.to redirect_to(project_path(project))
end
end
context 'when unsuccessful' do
it 'renders the new project page' do
let(:context) { double(:context, success?: false, notice: 'notice') }
expect do
post :create, attributes_for(project, :invalid)
end.to render_template(:new)
end
it 'sets a flash notice' do
expect do
post :create, attributes_for(project, :invalid)
end.to change do
flash[:notice]
end.from(nil).to('notice')
end
end
end
end
require 'spec_helper'
describe ProjectsController do
let(:user) { create(:user) }
describe '#create' do
before { sign_in_as(user) }
context 'with valid project_params' do
it 'should create a Project' do
expect do
post :create, attributes_for(project)
end.to change do
Project.count
end.by(1)
end
it 'should notify Admin, User, and Designer' do
expect do
post :create, attributes_for(project)
end.to change do
ActionMailer::Base.deliveries.count
end.by(3)
end
end
context 'with invalid project_params' do
it 'should not create a Project' do
expect do
post :create, attributes_for(project, :invalid)
end.to_not change do
Project.count
end
end
it 'should not notify Admin, User, and Designer' do
expect do
post :create, attributes_for(project, :invalid)
end.to_not change do
ActionMailer::Base.deliveries.count
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.