-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/action mailer #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
3a2936d
6cec3b3
bf81c48
5c645cf
8179580
da3f512
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| end | ||
|
|
||
| respond_with(task) | ||
| end | ||
|
|
||
| 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| 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 |
| 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 |
| 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ! |
||
| user.update!(reset_digest: nil, reset_sent_at: nil) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,37 @@ | ||
| <!DOCTYPE html> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| html | ||
| body | ||
| = yield |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| = yield |
| 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' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| | Task #{@task.id} was created |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| | Task #{@task.id} was destroyed |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| | Task #{@task.id} was updated |
| 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 |
| 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" |
| 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 | ||
|
|
@@ -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] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. будет достаточно |
||
| resource :password, only: [:edit, :update] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. password_controller? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. достаточно |
||
| end | ||
| end | ||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше expired_at |
||
| end | ||
| end | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
где
user?