Skip to content

Commit

Permalink
Add practice for lesson 9 (fs#92)
Browse files Browse the repository at this point in the history
* add practice

* remove bang
  • Loading branch information
DenisZackharov authored Nov 3, 2023
1 parent 1e9cba1 commit 0e162d2
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 7 deletions.
10 changes: 10 additions & 0 deletions app/interactors/projects/destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ class Destroy

delegate :project, to: :context

before do
context.users = project.users
end

def call
project.destroy
end

after do
context.users.each do |user|
ProjectMailer.project_destroyed(user).deliver_later
end
end
end
end
6 changes: 6 additions & 0 deletions app/interactors/projects/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ class Update
def call
context.fail!(error: "Invalid data") unless project.update(project_params)
end

after do
project.users.each do |user|
ProjectMailer.project_updated(project, user).deliver_later
end
end
end
end
30 changes: 30 additions & 0 deletions app/interactors/tasks/create_default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Tasks
class CreateDefault
include Interactor

delegate :project_id, to: :context

def call
context.fail!(error: "Invalid data") unless Task.create(task_params)
end

private

def task_params
[
{
project_id: project_id,
name: "АНЖУМАНИЯ",
status: :unstarted,
deadline_at: 1.week.from_now
},
{
project_id: project_id,
name: "БЕГИТ",
status: :unstarted,
deadline_at: 1.week.from_now
}
]
end
end
end
8 changes: 1 addition & 7 deletions app/jobs/projects/create_default_tasks_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ class CreateDefaultTasksJob
sidekiq_options queue: :default, retry: 3

def perform(project_id)
project = Project.find(project_id)

project.tasks.create(
[
{ name: "Your first task", status: :unstarted, deadline_at: 1.week.from_now }
]
)
Tasks::CreateDefault.call!(project_id: project_id)
end
end
end
10 changes: 10 additions & 0 deletions app/mailers/project_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ def project_created(project, user)

mail(to: user.email)
end

def project_updated(project, user)
@project = project

mail(to: user.email)
end

def project_destroyed(user)
mail(to: user.email)
end
end
1 change: 1 addition & 0 deletions app/views/project_mailer/project_destroyed.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Your project destroyed</h1>
1 change: 1 addition & 0 deletions app/views/project_mailer/project_destroyed.txt.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Your project destroyed
6 changes: 6 additions & 0 deletions app/views/project_mailer/project_updated.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Your project updated</h1>
<% @project.attributes.each do |attr| %>
<ul>
<li><%= attr.join(': ') %></li>
</ul>
<% end %>
4 changes: 4 additions & 0 deletions app/views/project_mailer/project_updated.txt.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Your project updated
<% @project.attributes.each |attr| do %>
<%= attr.join(': ') %>
<% end %>

0 comments on commit 0e162d2

Please sign in to comment.