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
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ group :development do
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.2'
gem 'bullet'
gem 'letter_opener'
gem 'letter_opener_web'
end

group :test do
Expand Down Expand Up @@ -65,4 +67,4 @@ gem 'active_model_serializers'
gem 'webpacker-react', '~> 0.3.2'
gem 'js-routes'
gem 'rollbar'
gem 'newrelic_rpm'
gem 'newrelic_rpm'
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ 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 (1.4.0)
actionmailer (>= 3.2)
letter_opener (~> 1.0)
railties (>= 3.2)
listen (3.2.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
Expand Down Expand Up @@ -293,6 +301,8 @@ DEPENDENCIES
jbuilder (~> 2.7)
js-routes
kaminari
letter_opener
letter_opener_web
listen (~> 3.2)
newrelic_rpm
pg (>= 0.18, < 2.0)
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 @@ -19,21 +19,30 @@ def create
p = task_params
p['author_id'] = current_user.id
task = current_user.my_tasks.new(p)
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_deleted.deliver_now
end

respond_with(task)
end
Expand Down
24 changes: 12 additions & 12 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class Api::V1::UsersController < Api::V1::ApplicationController
def show
user = User.find(params[:id])
respond_with(user, serializer: UserSerializer)
end
def index
users = User.ransack(ransack_params).result.page(page).per(per_page)
respond_with(users, each_serializer: UserSerializer, meta: build_meta(users), root: 'items')
end
end
def show
user = User.find(params[:id])

respond_with(user, serializer: UserSerializer)
end

def index
users = User.ransack(ransack_params).result.page(page).per(per_page)

respond_with(users, each_serializer: UserSerializer, meta: build_meta(users), root: 'items')
end
end
32 changes: 32 additions & 0 deletions app/controllers/web/recovery_passwords_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Web::RecoveryPasswordsController < Web::ApplicationController
def create
user = User.find_by_email(recovery_password_params[:email])
user.send_password_reset if user
redirect_to(new_session_path)
end

def edit
@user = User.find_by_password_reset_token!(params[:id])
end

def update
@user = User.find_by_password_reset_token!(params[:id])
if @user.password_reset_sent_at < 24.hour.ago
redirect_to(new_recovery_password_path)
elsif @user.update(user_params)
redirect_to(new_session_path)
else
render(:edit)
end
end

private

def user_params
params.require(:admin).permit(:password)
end

def recovery_password_params
params.require(:recovery_passwords).permit(:email)
end
end
1 change: 0 additions & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
layout 'mailer'
end
27 changes: 27 additions & 0 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class UserMailer < ApplicationMailer
layout 'task_mailer'

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

mail(from: 'noreply@taskmanager.com', to: user.email, subject: 'New Task Created')
end

def task_updated
@task = params[:task]

mail(from: 'noreply@taskmanager.com', to: @task.author.email, subject: 'Task Updated')
end

def task_deleted
@task = params[:task]

mail(from: 'noreply@taskmanager.com', to: @task.author.email, subject: 'Task Deleted')
end

def forgot_password
@user = params
mail(from: 'noreply@taskmanager.com', to: @user.email, subject: 'Reset password instructions')
end
end
13 changes: 13 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,17 @@ class User < ApplicationRecord
validates :first_name, length: { minimum: 2 }, presence: true
validates :last_name, length: { minimum: 2 }, presence: true
validates :email, format: { with: /\A\S+@.+\.\S+\z/ }, uniqueness: true, presence: true

def send_password_reset
generate_token(:password_reset_token)
self.password_reset_sent_at = Time.zone.now
save!
UserMailer.with(self).forgot_password.deliver_now
end

def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end
end
4 changes: 2 additions & 2 deletions app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class UserSerializer < ApplicationSerializer
attributes :id, :first_name, :last_name, :email
end
attributes :id, :first_name, :last_name, :email
end
13 changes: 0 additions & 13 deletions app/views/layouts/mailer.html.erb

This file was deleted.

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
22 changes: 22 additions & 0 deletions app/views/layouts/task_mailer.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
doctype html
html lang="en"
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8"
title Email template
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: 24px; font-family:Arial, Helvetica, sans-serif;"
b Task Board Project
tr
td bgcolor="#FFFFFF" align="center" style="padding: 30px 0;"
span style="color:#000000; font-size: 18px; font-family:Arial, Helvetica, sans-serif;"
create =yield
tr
td align="center" style="padding: 30px 0;"
span style="color:#b9b9b9; font-size: 14px; font-family:Arial, Helvetica, sans-serif;"
create &copy; 2020, TaskBoard Project
6 changes: 6 additions & 0 deletions app/views/user_mailer/forgot_password.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
p Hi =@user.first_name
p To reset your password, click the URL below:

p =edit_recovery_password_url(@user.password_reset_token)

p If you did not request your password to be reset, just ignore this e-mail and your password will stay the same.
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_deleted.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| Task #{@task.id} was deleted
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
15 changes: 15 additions & 0 deletions app/views/web/recovery_passwords/edit.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
h4 Reset Password

= simple_form_for @user, url: recovery_password_path(params[:id]) do |f|
= if @user.errors.any?
div class='error_messages'
h4 Form is invalid
ul
= for message in @user.errors.full_messages
li
= message
p
= f.input :password
= f.input :password_confirmation
p
= f.submit 'Update password'
7 changes: 7 additions & 0 deletions app/views/web/recovery_passwords/new.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
h3 Reset Password

= simple_form_for :recovery_passwords, url: recovery_passwords_path do |f|
p
= f.input :email
p
= f.button :submit, "Reset Password"
4 changes: 3 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,6 @@ h4 Log in
= f.input :email
= f.input :password
p
= f.button :submit, "Sign in"
= f.button :submit, "Sign in"
p
= link_to 'Forgot password?', new_recovery_password_path
6 changes: 6 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,10 @@
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker

config.action_mailer.delivery_method = :letter_opener_web

config.action_mailer.perform_caching = true

config.action_mailer.default_url_options = {:host =>'localhost:3000'}
end
15 changes: 13 additions & 2 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
config.log_level = :debug

# Prepend all log lines with the following tags.
config.log_tags = [ :request_id ]
config.log_tags = [:request_id]

# Use a different cache store in production.
# config.cache_store = :mem_cache_store
Expand Down Expand Up @@ -80,7 +80,7 @@
# require 'syslog/logger'
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')

if ENV["RAILS_LOG_TO_STDOUT"].present?
if ENV['RAILS_LOG_TO_STDOUT'].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
Expand Down Expand Up @@ -109,4 +109,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
1 change: 1 addition & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@

# Raises error for missing translations.
# config.action_view.raise_on_missing_translations = true
config.action_mailer.delivery_method = :test
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
Rails.application.routes.draw do
mount LetterOpenerWeb::Engine, at: "/letter_opener" if Rails.env.development?
root :to => "web/boards#show"

scope module: :web do
resource :board, only: :show
resource :session, only: [:new, :create, :destroy]
resources :developers, only: [:new, :create]
resources :recovery_passwords, only: [:new, :create, :edit, :update]
end

namespace :admin do
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20210615161358_add_password_reset_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddPasswordResetToUsers < ActiveRecord::Migration[6.0]
def change
add_column :users, :password_reset_token, :string
add_column :users, :password_reset_sent_at, :datetime
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_04_22_083316) do
ActiveRecord::Schema.define(version: 2021_06_15_161358) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -35,6 +35,8 @@
t.string "type"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "password_reset_token"
t.datetime "password_reset_sent_at"
end

end
Loading