Commit d84cb7a5 by Hoang Phuc Do

Custom Devise's user

parent f970f64a
......@@ -62,6 +62,8 @@ group :development, :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '~> 2.13'
gem 'selenium-webdriver'
# When mail is sent from your application, Letter Opener will open a preview in the browser instead of sending.
gem 'letter_opener', '~> 1.4', '>= 1.4.1'
end
group :development do
......
......@@ -145,6 +145,10 @@ GEM
activerecord
kaminari-core (= 1.0.1)
kaminari-core (1.0.1)
launchy (2.4.3)
addressable (~> 2.3)
letter_opener (1.4.1)
launchy (~> 2.2)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
......@@ -290,6 +294,7 @@ DEPENDENCIES
jbuilder (~> 2.5)
jquery-rails
kaminari (~> 1.0, >= 1.0.1)
letter_opener (~> 1.4, >= 1.4.1)
listen (>= 3.0.5, < 3.2)
mini_magick
mysql2 (>= 0.3.18, < 0.5)
......
......@@ -2,6 +2,7 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include CartsHelper
before_action :set_cart
before_action :configure_permitted_parameters, if: :devise_controller?
def authenticate_active_admin_user!
authenticate_user!
......@@ -10,4 +11,12 @@ class ApplicationController < ActionController::Base
redirect_to root_path
end
end
protected
def configure_permitted_parameters
added_attrs = [:username, :first_name, :last_name, :email, :password, :password_confirmation]
devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
devise_parameter_sanitizer.permit :account_update, keys: added_attrs
end
end
......@@ -16,6 +16,7 @@ class OrdersController < ApplicationController
respond_to do |format|
if @order.save
destroy_cart_session
OrderMailer.order_detail(current_user).deliver_now
format.html { redirect_to root_url }
format.json { render :show }
else
......
class OrderMailer < ApplicationMailer
def order_detail(user)
@user = user
mail to: user.email, subject: "Order detail"
end
end
......@@ -2,7 +2,20 @@ class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
:recoverable, :rememberable, :trackable,
:validatable, :authentication_keys => [:login]
attr_accessor :login
has_many :products, dependent: :destroy
has_many :orders, dependent: :destroy
validates :username, format: { with: /^[a-zA-Z0-9_\.]*$/, multiline: true }
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", {value: login.strip.downcase}]).first
end
def validate_username
errors.add(:username, :invalid) if User.where(email: username).exists?
end
end
......@@ -14,8 +14,8 @@
</h3>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, class: "form-control" %>
<%= f.label :login %>
<%= f.email_field :login, autofocus: true, class: "form-control" %>
</div>
<div class="form-group">
......
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1><%= @user.email %></h1>
<p>
Your order has been created
</p>
</body>
</html>
\ No newline at end of file
<%= @user.email %>
Your order has been created
\ No newline at end of file
......@@ -51,4 +51,6 @@ Rails.application.configure do
# 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
end
......@@ -34,7 +34,7 @@ Devise.setup do |config|
# session. If you need permissions, you should implement that in a before filter.
# You can also supply a hash where the value is a boolean determining whether
# or not authentication should be aborted when the value is not present.
# config.authentication_keys = [:email]
config.authentication_keys = [:login]
# Configure parameters from the request object used for authentication. Each entry
# given should be a request method and it will automatically be passed to the
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment