Skip to content

Commit

Permalink
Merge pull request #38 from CH1006/209
Browse files Browse the repository at this point in the history
209: edit and destroy member
  • Loading branch information
N-Viet-ruby-dev authored Aug 28, 2019
2 parents 1e69b1d + a7ef724 commit e07686c
Show file tree
Hide file tree
Showing 26 changed files with 286 additions and 85 deletions.
5 changes: 3 additions & 2 deletions app/assets/javascripts/manager/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ $(document).ready(function(){
$('#admin_table').DataTable({
scrollY: 500,
"pageLength": 25,
"aaSorting": [],
"columnDefs": [
{ "orderable": false, "targets": [5] },
]
Expand All @@ -21,6 +22,7 @@ $(document).ready(function(){
$('#member_table').DataTable({
scrollY: 500,
"pageLength": 25,
"aaSorting": [],
"columnDefs": [
{ "orderable": false, "targets": [5] },
]
Expand All @@ -42,10 +44,9 @@ $(document).ready(function(){

$('#admin-prices').DataTable({
scrollY: 500,
"order": [[ 0, 'DESC' ]],
"pageLength": 25,
"columnDefs": [
{ "orderable": false, "targets": [5] },
{ "orderable": false, "targets": [4] },
]
});

Expand Down
25 changes: 21 additions & 4 deletions app/assets/stylesheets/manager/custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ i.fa.fa-caret-down {
}

.avatar-default-member {
height: 160px;
width: 160px;
margin-left: 16%;
height: 90px;
width: 90px;
border-radius: 50%;
}

.custom-dropdown {
Expand All @@ -224,7 +224,7 @@ i.fa.fa-caret-down {
border-radius: 6px;
}

.custom-dropdown li a {
.custom-dropdown li a {
color: #555555;
display: block;
font-family: arial;
Expand Down Expand Up @@ -301,6 +301,23 @@ a.account:hover {
text-decoration: none;
}

#member_avatar {
margin-top: 27px;
padding: 3px;
}

.show-avatar-index {
height: 15px;
width: 15px;
border-radius: 50%;
margin-right: 10px;
margin-top: -4px;
}

.align-btn{
padding: 0.1rem .75rem !important;
}

.new-price {
float: right;
}
Expand Down
51 changes: 28 additions & 23 deletions app/controllers/manager/admins_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,37 @@
module Manager
class AdminsController < BaseController
before_action :check_default_admin
before_action :all_admin, only: %i[index create update]
before_action :load_admin, only: %i[edit update destroy]

def index
@admins = Admin.all
end
def index; end

def new
@admin = Admin.new
end

def create
@admin = Admin.new admin_params
@admin.avatar = params[:admin][:avatar]
if @admin.save
flash[:success] = t "messages.success.admins.create"
flash.now[:success] = t "messages.success.admins.create"
else
flash[:danger] = t "messages.failed.admins.create"
end
respond_to do |format|
format.js
@admin.avatar.purge_later
flash.now[:danger] = t "messages.failed.admins.create"
end
end

def edit; end

def update
support_update(@admin)
support_update @admin
if @admin.changed? || !params[:admin][:avatar].blank?
update_admin @admin
else
flash[:notice] = t("messages.notice.admins.not_edit", id: @admin.id.to_s)
flash.now[:notice] = t("messages.notice.admins.not_edit", id: @admin.id)
end
end

def destroy
if !@admin.flag? && @admin.destroy
flash[:success] = t("messages.success.admins.delete", id: @admin.id.to_s)
else
flash[:danger] = t("messages.failed.admins.delete", id: @admin.id.to_s)
end
destroy_admin @admin
redirect_to manager_admins_path
end

Expand All @@ -53,16 +45,16 @@ def load_admin

def admin_params
params.require(:admin).permit :email, :name, :address,
:password, :password_confirmation, :avatar
:password, :password_confirmation
end

def update_admin(admin)
if admin.save
flash[:success] = t("messages.success.admins.update", id: admin.id.to_s)
if admin.save && admin.update_avatar(params[:admin][:avatar])
flash.now[:success] =
t("messages.success.admins.update", id: admin.id)
else
respond_to do |format|
format.js { flash[:danger] = t("messages.failed.admins.update", id: admin.id.to_s) }
end
flash.now[:danger] =
t("messages.failed.admins.update", id: admin.id)
end
end

Expand All @@ -76,5 +68,18 @@ def support_update(admin)
admin.not_update_password = true if admin_params[:password].blank? && admin_params[:password_confirmation].blank?
admin.assign_attributes admin_params
end

def destroy_admin(admin)
if !admin.flag? && admin.destroy
admin.avatar.purge_later
flash[:success] = t("messages.success.admins.delete", id: admin.id)
else
flash[:danger] = t("messages.failed.admins.delete", id: admin.id)
end
end

def all_admin
@admins = Admin.newest
end
end
end
56 changes: 49 additions & 7 deletions app/controllers/manager/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,72 @@

module Manager
class MembersController < BaseController
def index
@members = Member.all
end
before_action :all_member, only: %i[index create update]
before_action :load_member, only: %i[edit update destroy]

def index; end

def new
@member = Member.new
end

def create
@member = Member.new member_params
@member.avatar = params[:member][:avatar]
if @member.save
flash[:success] = t "messages.success.members.create"
redirect_to manager_members_path
flash.now[:success] = t "messages.success.members.create"
else
@member.avatar.purge_later
flash.now[:danger] = t "messages.failed.members.create"
render :new
end
end

def update
support_update @member
if @member.changed? || !params[:member][:avatar].blank?
update_member @member
else
flash.now[:notice] = t("messages.notice.members.not_edit", id: @member.id)
end
end

def destroy
if @member.destroy
@member.avatar.purge_later
flash[:success] = t("messages.success.members.delete", id: @member.id)
else
flash[:danger] = t("messages.failed.members.delete", id: @member.id)
end
redirect_to manager_members_path
end

private

def member_params
params.require(:member).permit :email, :name, :address,
:password, :password_confirmation, :avatar
:password, :password_confirmation
end

def load_member
@member = Member.find(params[:id])
end

def update_member(member)
if member.save && member.update_avatar(params[:member][:avatar])
flash.now[:success] = t("messages.success.members.update", id: member.id)
else
flash.now[:danger] = t("messages.failed.members.update", id: member.id)
end
end

def support_update(member)
member.not_update_password = true if member_params[:password]
.blank? && member_params[:password_confirmation].blank?
member.assign_attributes member_params
end

def all_member
@members = Member.newest
end
end
end
1 change: 1 addition & 0 deletions app/models/admin.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

class Admin < User
scope :newest, -> { order updated_at: :desc }
end
1 change: 1 addition & 0 deletions app/models/member.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

class Member < User
scope :newest, -> { order updated_at: :desc }
end
27 changes: 26 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,35 @@ class User < ApplicationRecord
validates :name, presence: true, length: { maximum: 45 }
validates :address, presence: true, length: { maximum: 255 }
validates :type, presence: true
validate :avatar_validate, if: -> { avatar.attached? }

has_one_attached :avatar

def timeout_in
30.minutes
Settings.time_out.minutes
end

def avatar_validate
return if avatar.blob.content_type.starts_with?(Settings.file_path) && avatar.blob.byte_size < Settings.max_size

avatar.purge
errors.add(:avatar, :not_valid)
end

def update_avatar(value)
return true unless value.present?

if value.size > Settings.max_size
errors.add(:avatar, :size)
false
elsif !value.content_type.starts_with?(Settings.file_path)
errors.add(:avatar, :format)
false
else
avatar.purge_later
update_attribute(:avatar, value)
true
end
end

protected
Expand Down
2 changes: 2 additions & 0 deletions app/views/layouts/_flashmessages.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
- type = "warning"
- when "warning"
- type = "warning"
- when "timedout"
-type = ""
- else
- type = 'success'
javascript:
Expand Down
13 changes: 10 additions & 3 deletions app/views/manager/admins/_admin.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ tr.data-user
td
= admin.id
td
- if admin.avatar.attached?
= image_tag admin.avatar, class: "show-avatar-index"
- else
= image_tag "defaultavatar.jpeg", class: "show-avatar-index"
= admin.name
td
= admin.email
td
= admin.address
td
= admin.type
- if admin.created_at < admin.updated_at
= t(".updated", time: time_ago_in_words(admin.updated_at))
- else
= t(".created", time: time_ago_in_words(admin.created_at))
td id="admin-#{admin.id}"
= link_to t(".edit"), edit_manager_admin_path(admin), remote: true, class: "btn info"
= link_to t(".edit"), edit_manager_admin_path(admin), remote: true, class: "btn info align-btn"
- unless admin.flag?
= link_to t(".delete"), manager_admin_path(admin), method: :delete, \
data: { confirm: "You sure?" }, class: "btn danger"
data: { confirm: "You sure?" }, class: "btn danger align-btn"
4 changes: 3 additions & 1 deletion app/views/manager/admins/create.js.slim
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
$(".modal").animate({scrollTop : 0},500);
- else
|
location.reload();
$("#admin-modal").modal("hide")
$(".data-admin").html("#{j(render @admins)}")
toastr.success("#{flash[:success]}");
4 changes: 2 additions & 2 deletions app/views/manager/admins/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ table#admin_table.table.table-striped.table-bordered style=("width: 100%")
th = t(".name")
th = t(".email")
th = t(".address")
th = t(".type")
th = t(".access")
th = t(".action")
tbody
tbody.data-admin
= render @admins
#admin-modal.modal.fade
10 changes: 8 additions & 2 deletions app/views/manager/admins/update.js.slim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
$("#message_errors").html("#{j(render 'shared/manager/error_messages', object: @admin)}");
toastr.error("#{flash[:danger]}");
$(".modal").animate({scrollTop : 0},500);
- else
- elsif flash[:notice]
|
location.reload();
$("#admin-modal").modal("hide")
toastr.info("#{flash[:notice]}");
- elsif flash[:success]
|
$("#admin-modal").modal("hide")
$(".data-admin").html("#{j(render @admins)}")
toastr.success("#{flash[:success]}");
3 changes: 3 additions & 0 deletions app/views/manager/members/_edit.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- provide(:title, t(".edit_member"))
- provide(:btn_name, t(".update"))
= render "form", member: @member
Loading

0 comments on commit e07686c

Please sign in to comment.