Skip to content

Instantly share code, notes, and snippets.

@aaronmcadam
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronmcadam/8e02dcddfc43108ddd3a to your computer and use it in GitHub Desktop.
Save aaronmcadam/8e02dcddfc43108ddd3a to your computer and use it in GitHub Desktop.
require "spec_helper"
require "./app/builders/profile_page"
describe Builders::ProfilePage do
describe "#build" do
it "builds a new instance of Participant::ProfilePage" do
stub_requests
profile_page = spy_on_class("Participant::ProfilePage")
builder = create_builder
builder.build
expect(profile_page).to have_received(:new)
.with(project: builder.project,
participant: builder.participant,
tasks: builder.tasks,
latest_photos: builder.latest_photos)
end
end
describe "#project" do
it "fetches the desired Project and wraps it with a ProjectDecorator" do
project_id = 1
project_decorator = spy_on_class("ProjectDecorator")
create_builder(project_id: project_id).project
expect(project_decorator).to have_received(:find).with(project_id)
end
end
describe "#participant" do
it "fetches the desired Participant and wraps it with a Participant::ProfileDecorator" do
options = { project_id: 1, participant_id: 2 }
profile_decorator = spy_on_class("Participant::ProfileDecorator")
project_participant = spy_on_class("Participant::ProjectParticipant")
create_builder(options).participant
expect(profile_decorator).to have_received(:new)
expect(project_participant).to have_received(:find_within_project)
.with(options)
end
end
describe "#tasks" do
it "wraps each Task of the Project with a Participant::TaskDecorator" do
project = instance_double("Project", tasks: [])
allow(ProjectDecorator).to receive(:find).and_return(project)
task_decorator = spy_on_class("Participant::TaskDecorator")
create_builder.tasks
expect(task_decorator).to have_received(:decorate_collection)
end
end
describe "#latest_photos" do
it "is a list of the latest photos a Participant has uploaded" do
options = { project_id: 1, participant_id: 2 }
photos = instance_spy("Participant::LatestPhotos")
allow(Participant::LatestPhotos).to receive(:new).and_return(photos)
create_builder(options).latest_photos
expect(Participant::LatestPhotos).to have_received(:new).with(options)
expect(photos).to have_received(:call)
end
end
# class_spy doesn’t override the constant automatically, it requires you to
# add .as_stubbed_const to override the constant
def spy_on_class(klass)
class_spy(klass).as_stubbed_const
end
def stub_requests
# ...
end
def create_builder(project_id: 1, participant_id: 2)
Builders::ProfilePage.new(project_id: project_id,
participant_id: participant_id)
end
end
require "project_decorator"
require "participant/profile_decorator"
require "participant/task_decorator"
require "participant/profile_page"
module Builders
class ProfilePage
def initialize(project_id:, participant_id:)
@project_id = project_id
@participant_id = participant_id
end
def build
Participant::ProfilePage.new(project: project,
participant: participant,
tasks: tasks,
latest_photos: latest_photos)
end
def project
ProjectDecorator.find(project_id)
end
def participant
Participant::ProfileDecorator.new(find_participant)
end
def tasks
Participant::TaskDecorator.decorate_collection(project.tasks)
end
def latest_photos
Participant::LatestPhotos.new(project_id: project_id,
participant_id: participant_id).call
end
private
attr_reader :project_id, :participant_id
def find_participant
Participant::ProjectParticipant.find_within_project(
project_id: project_id, participant_id: participant_id
)
end
end
end
module Participant
# Extracts the concerns of the Participant Profile page
class ProfilePage
def initialize(project:, participant:, tasks:, latest_photos:)
@project = project
@participant = participant
@tasks = tasks
@latest_photos = latest_photos
end
attr_reader :project, :participant, :tasks, :latest_photos
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment