Skip to content

Commit

Permalink
Lesson 8 (fs#69)
Browse files Browse the repository at this point in the history
Added lesson 8 materials
  • Loading branch information
AndreyBartnovskiy authored Oct 24, 2023
1 parent d5184c3 commit 3f8140c
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 10 deletions.
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ Rails/NotNullColumn:
I18n/GetText/DecorateFunctionMessage:
Exclude:
- 'app/controllers/concerns/authentication.rb'
Enabled: false
Enabled: false

Metrics/MethodLength:
Max: 20
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ruby "2.7.4"

gem "action_policy"
gem "enumerize"
gem "interactor", "~> 3.0"
# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 6.1.7"
# Use postgresql as the database for Active Record
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ GEM
activesupport (>= 6.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
interactor (3.1.2)
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
Expand Down Expand Up @@ -291,6 +292,7 @@ DEPENDENCIES
byebug
capybara (>= 3.26)
enumerize
interactor (~> 3.0)
jbuilder (~> 2.7)
kaminari
listen (~> 3.3)
Expand Down
16 changes: 8 additions & 8 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ def new
def edit; end

def create
@project = Project.new(project_params)
@project_membership = ProjectMembership.new(project_membership_params)
@project = create_project.project

if @project.save && @project_membership.save
redirect_to projects_path, notice: "Created Successful"
if create_project.success?
redirect_to @project, notice: "Created Successful"
else
render :new, status: :unprocessable_entity
end
Expand All @@ -47,11 +46,12 @@ def set_project
@project = Project.find_by(id: params[:id])
end

def project_membership_params
{ project: @project, user: current_user, role: :owner }
end

def project_params
params.require(:project).permit(:name, :description)
end

def create_project
@create_project ||= ::Projects::Create.call(project_params: project_params,
user: current_user)
end
end
8 changes: 8 additions & 0 deletions app/interactors/projects/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Projects
class Create
include Interactor::Organizer

organize Projects::Save,
Projects::Create::CreateOwner
end
end
32 changes: 32 additions & 0 deletions app/interactors/projects/create/create_owner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Projects
class Create
class CreateOwner
include Interactor

delegate :project, :user, to: :context

def call
raise_error unless owner_membership.save
end

private

def owner_membership
@owner_membership ||= ProjectMembership.new(prepared_owner_membership_params)
end

def prepared_owner_membership_params
{
project: project,
user: user,
role: :owner
}
end

def raise_error
project.destroy
context.fail!(error: "Invalid data")
end
end
end
end
19 changes: 19 additions & 0 deletions app/interactors/projects/save.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Projects
class Save
include Interactor

delegate :project_params, to: :context

def call
context.project = project

context.fail!(error: "Invalid data") unless project.save
end

private

def project
@project ||= Project.new(project_params)
end
end
end
2 changes: 1 addition & 1 deletion app/views/projects/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<p><%= project.id %>. <%= link_to project.name, project_tasks_path(project) %></p>
<p><%= project.description %> </p>
<p><%= link_to "Edit", edit_project_path(project) %></p>
<p><%= button_to "Destroy", project, method: :delete %></p>
<p><%= button_to "Destroy", project, method: :delete, data: {confirm: "Are you sure?"} %></p>
</div>
<% end %>

Expand Down

0 comments on commit 3f8140c

Please sign in to comment.