Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ group :development do
gem 'rack-mini-profiler', '~> 2.0'
gem 'listen', '~> 3.3'
gem 'bullet'
gem 'letter_opener'
gem 'letter_opener_web'
end

group :test do
Expand Down
11 changes: 11 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ GEM
activerecord
kaminari-core (= 1.2.1)
kaminari-core (1.2.1)
launchy (2.5.0)
addressable (~> 2.7)
letter_opener (1.7.0)
launchy (~> 2.2)
letter_opener_web (2.0.0)
actionmailer (>= 5.2)
letter_opener (~> 1.7)
railties (>= 5.2)
rexml
listen (3.7.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
Expand Down Expand Up @@ -334,6 +343,8 @@ DEPENDENCIES
jbuilder (~> 2.7)
js-routes
kaminari
letter_opener
letter_opener_web
listen (~> 3.3)
newrelic_rpm
pg (~> 1.1)
Expand Down
15 changes: 12 additions & 3 deletions app/controllers/api/v1/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,30 @@ def show

def create
task = current_user.my_tasks.new(task_params)
task.save

if task.save
UserMailer.with({ user: current_user, task: task }).task_created.deliver_now
end

respond_with(task, serializer: TaskSerializer, location: nil)
end

def update
task = Task.find(params[:id])
task.update(task_params)

if task.update(task_params)
UserMailer.with({ task: task }).task_updated.deliver_now
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

где user?

end

respond_with(task, serializer: TaskSerializer)
end

def destroy
task = Task.find(params[:id])
task.destroy

if task.destroy
UserMailer.with({ task: task }).task_destroyed.deliver_now
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user?

end

respond_with(task)
end
Expand Down
48 changes: 48 additions & 0 deletions app/controllers/web/password_resets_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Web::PasswordResetsController < Web::ApplicationController
def new
@password_reset = PasswordResetForm.new
end

def create
@password_reset = PasswordResetForm.new(password_reset_params)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

password_reset_form. Глобальные переменные лучше не использовать

@email = @password_reset.email

if @password_reset.valid?
@user = @password_reset.user
UserService.reset_password!(@user)
end

redirect_to(root_path)
end

def edit
set_user
end

def update
set_user

if @user.update(user_params)
UserService.clear_reset_digest(@user)
end

redirect_to(new_session_path)
end

private

def set_user
@user = User.find_by(reset_digest: params[:user][:reset_digest])
if @user.blank? || !UserService.password_reset_period_valid?(@user)
redirect_to(new_session_path)
end
end

def user_params
params.require(:user).permit(:password, :password_confirmation)
end

def password_reset_params
params.require(:password_reset_form).permit(:email)
end
end
18 changes: 18 additions & 0 deletions app/forms/password_reset_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class PasswordResetForm
include ActiveModel::Model

attr_accessor(
:email,
)

validates :email, presence: true, format: { with: /\A\S+@.+\.\S+\z/ }
validate :user_exists?

def user
@user = User.find_by(email: email)
end

def user_exists?
errors.add(:email, :user_does_not_exist) if user.blank?
end
end
30 changes: 30 additions & 0 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class UserMailer < ApplicationMailer
default from: 'noreply@taskmanager.com'

def task_created
user = params[:user]
@task = params[:task]

mail(to: user.email, subject: 'New Task Created')
end

def task_updated
@task = params[:task]
author = @task.author

mail(to: author.email, subject: 'Task Updated')
end

def task_destroyed
@task = params[:task]
author = @task.author

mail(to: author.email, subject: 'Task Destroyed')
end

def reset_password
@user = params[:user]

mail(to: @user.email, subject: 'Reset Password')
end
end
16 changes: 16 additions & 0 deletions app/services/user_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class UserService
def self.reset_password!(user)
token = SecureRandom.hex(10)
user.update!(reset_digest: token, reset_sent_at: Time.current)

UserMailer.with({ user: user }).reset_password.deliver_now
end

def self.password_reset_period_valid?(user)
user.reset_sent_at.present? && (Time.current - user.reset_sent_at) <= 1.days
end

def self.clear_reset_digest(user)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!

user.update!(reset_digest: nil, reset_sent_at: nil)
end
end
42 changes: 33 additions & 9 deletions app/views/layouts/mailer.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
<!DOCTYPE html>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

нет необходимости использовать erb и slim. Давай остановимся на слим

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
/* Email styles need to be inline */
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<title>Email template</title>
</head>

<body>
<%= yield %>
<body bgcolor="#efefef" style="padding: 0; margin: 0;">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="center">
<table width="600" border="0" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#3F52B5" align="center" style="padding: 30px 0;">
<span style="color:#ffffff; font-size: 20px; font-family:Arial, Helvetica, sans-serif;">
<b>Task Board Project</b>
</span>
</td>
<tr>
<td bgcolor="#ffffff" align="center" style="padding: 30px 0;">
<p style="font-size: 16px; font-family:Arial, Helvetica, sans-serif;">
<%= yield %>
</p>
</td>
</tr>
<tr>
<td bgcolor="#efefef" align="center" style="padding: 30px 0;">
<p style="color:#808080; font-size: 14px; font-family:Arial, Helvetica, sans-serif;">
© 2022, TaskBoard Project
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
</html>
3 changes: 3 additions & 0 deletions app/views/layouts/mailer.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
html
body
= yield
1 change: 1 addition & 0 deletions app/views/layouts/mailer.text.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= yield
2 changes: 2 additions & 0 deletions app/views/user_mailer/reset_password.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
p Follow link for set new password
= link_to 'Reset password URL', edit_password_reset_url(user: { reset_digest: @user.reset_digest, email: @user.email }), target: '_blank'
1 change: 1 addition & 0 deletions app/views/user_mailer/task_created.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| Task #{@task.id} was created
1 change: 1 addition & 0 deletions app/views/user_mailer/task_destroyed.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| Task #{@task.id} was destroyed
1 change: 1 addition & 0 deletions app/views/user_mailer/task_updated.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| Task #{@task.id} was updated
12 changes: 12 additions & 0 deletions app/views/web/password_resets/edit.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
h3 New Password
= simple_form_for @user, url: password_reset_path, as: :user do |f|
p
= f.input :password
p
= f.input :password_confirmation
p
= f.button :submit, 'Submit'
p
= f.hidden_field :reset_digest, :value => @user.reset_digest
p
= f.hidden_field :email, :value => @user.email
6 changes: 6 additions & 0 deletions app/views/web/password_resets/new.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
h4 Password Reset
= simple_form_for @password_reset, url: password_reset_path do |f|
p
= f.input :email
p
= f.button :submit, "Reset"
3 changes: 2 additions & 1 deletion app/views/web/sessions/new.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ h4 Log in
= f.input :email
= f.input :password
p
= f.button :submit, "Sign in"
= f.button :submit, "Sign in"
= link_to "(forgot password)", new_password_reset_path
5 changes: 4 additions & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false

config.action_mailer.perform_caching = false
config.action_mailer.delivery_method = :letter_opener_web
config.action_mailer.perform_caching = true

config.action_mailer.default_url_options = { host: 'localhost:3000' }

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
Expand Down
11 changes: 11 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,15 @@
# config.active_record.database_selector = { delay: 2.seconds }
# config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver
# config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
user_name: ENV['MAILER_USERNAME'],
password: ENV['MAILER_PASSWORD'],
address: ENV['MAILER_ADDRESS'],
port: ENV['MAILER_PORT'],
domain: ENV['MAILER_DOMAIN'],
authentication: ENV['MAILER_AUTHENTICATION'],
enable_starttls_auto: true,
}
end
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

config.action_mailer.perform_caching = false

config.action_mailer.default_url_options = { host: 'example.com' }

# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Rails.application.routes.draw do
mount LetterOpenerWeb::Engine, at: "/letter_opener" if Rails.env.development?

root :to => "web/boards#show"

namespace :admin do
Expand All @@ -16,5 +18,7 @@
resource :board, only: :show
resource :session, only: [:new, :create, :destroy]
resources :developers, only: [:new, :create]
resource :password_reset, only: [:new, :create, :edit, :update]
Copy link
Copy Markdown

@YJorge YJorge Aug 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

будет достаточно create для сброса

resource :password, only: [:edit, :update]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

password_controller?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

достаточно update для обновления

end
end
6 changes: 6 additions & 0 deletions db/migrate/20220116111008_add_reset_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddResetToUsers < ActiveRecord::Migration[6.1]
def change
add_column :users, :reset_digest, :string
add_column :users, :reset_sent_at, :datetime
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше expired_at

end
end
4 changes: 3 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions test/controllers/api/v1/tasks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ class Api::V1::TasksControllerTest < ActionController::TestCase
test 'should post create' do
author = create(:user)
sign_in(author)

assignee = create(:user)
task_attributes = attributes_for(:task).
merge({ assignee_id: assignee.id })
post :create, params: { task: task_attributes, format: :json }
task_attributes = attributes_for(:task).merge({ assignee_id: assignee.id })

assert_emails 1 do
post :create, params: { task: task_attributes, format: :json }
end
assert_response :created

data = JSON.parse(response.body)
created_task = Task.find(data['task']['id'])

assert created_task.present?
assert_equal task_attributes.stringify_keys, created_task.slice(*task_attributes.keys)
assert created_task.assignee == assignee
assert created_task.author == author
end

test 'should put update' do
Expand All @@ -37,7 +41,9 @@ class Api::V1::TasksControllerTest < ActionController::TestCase
merge({ author_id: author.id, assignee_id: assignee.id }).
stringify_keys

patch :update, params: { id: task.id, format: :json, task: task_attributes }
assert_emails 1 do
patch :update, params: { id: task.id, format: :json, task: task_attributes }
end
assert_response :success

task.reload
Expand Down
Loading