Skip to content

Commit

Permalink
Chore/move ownership creation (#881)
Browse files Browse the repository at this point in the history
* Move ownership creation to an operation

* refactor create ownership

* refactor code formatting

* fix transaction formatting
  • Loading branch information
sergioosouzaa authored Oct 16, 2023
1 parent 512d0b3 commit 9fccb92
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ def create
authorize @project
@project.users << current_user

result = ProjectOperations::Create.call(project: @project, current_user: current_user)
result = ProjectOperations::Create.call(project: @project, current_user: current_user,
current_team: current_team)

respond_to do |format|
match_result(result) do |on|
on.success do |project|
# TODO: move from here
current_team.ownerships.create(project: project, is_owner: true)
format.html do
redirect_to(project, notice: t('projects.project was successfully created'))
end
Expand Down
10 changes: 8 additions & 2 deletions app/operations/project_operations/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ module ProjectOperations
class Create
include Operation

def initialize(project:, current_user:)
def initialize(project:, current_user:, current_team:)
@project = project
@current_user = current_user
@current_team = current_team
end

def call
ActiveRecord::Base.transaction do
yield save_project
yield create_activity
yield create_ownership

Success(project)
end
Expand All @@ -20,7 +22,7 @@ def call

private

attr_reader :project, :current_user
attr_reader :project, :current_user, :current_team

def save_project
if project.save
Expand All @@ -37,5 +39,9 @@ def create_activity
action: 'create'
)
end

def create_ownership
Success(current_team.ownerships.create!(project: project, is_owner: true))
end
end
end
15 changes: 13 additions & 2 deletions spec/operations/project_operations/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

describe ProjectOperations::Create do
describe '#call' do
subject { -> { ProjectOperations::Create.call(project: project, current_user: user) }}
subject { -> { ProjectOperations::Create.call(project: project, current_user: user, current_team: current_team) }}

let(:user) { create(:user) }
let(:project) { user.projects.build(project_params) }

let(:team) { create(:team) }
# let(:session) { { user_id: user.id, current_team_slug: team.slug } }
let(:current_team) { create(:team) }
context 'with valid project' do
let(:project_params) do
{ name: 'Project', start_date: Date.current }
Expand All @@ -20,6 +22,10 @@
expect { subject.call }.to change { Activity.count }.by(1)
end

it 'creates ownership' do
expect { subject.call }.to change { Ownership.count }.by(1)
end

it 'returns success' do
expect(subject.call.success?).to be(true)
end
Expand All @@ -42,6 +48,11 @@
expect { subject.call }.to_not change { Activity.count }
end


it 'does not create ownership' do
expect { subject.call }.to_not change { Ownership.count }
end

it 'returns failure' do
expect(subject.call.failure?).to be(true)
end
Expand Down

0 comments on commit 9fccb92

Please sign in to comment.