Class: Shoppy::AccountsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/shoppy/accounts_controller.rb

Direct Known Subclasses

AccountsController

Instance Method Summary collapse

Instance Method Details

#adminsObject



10
11
12
# File 'app/controllers/shoppy/accounts_controller.rb', line 10

def admins
  @admins = Admin.all
end

#customersObject



6
7
8
# File 'app/controllers/shoppy/accounts_controller.rb', line 6

def customers
  @customers = Customer.all
end

#deleteObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/shoppy/accounts_controller.rb', line 71

def delete
  @user = User.find_by(id: params[:user_id])
  if @user
    r = @user.type.downcase + "s"
    if @user != current_admin
      @user.destroy
      Log.newEvent("Users",  "User #{@user.name} was deleted", current_admin.name)
      flash[:notice] = @user.type +  " has been deleted."
    else
      flash[:warning] = "You can't delete yourself, you punk 😡"
    end
    if params[:view] == "show"
      redirect_to '/accounts/#{@user.id}'
    else
      redirect_to '/accounts/' + r
    end
  else
    page_not_found
  end
end

#editObject



61
62
63
64
65
66
67
68
69
# File 'app/controllers/shoppy/accounts_controller.rb', line 61

def edit
  @user = User.find_by(id: params[:user_id])
  if @user
    @user.update_attributes(@user.type == "Admin" ? admin_params : customer_params)
    Log.newEvent("Users", "Customer with email #{@user.email} was updated", current_admin.name)
    redirect_to '/accounts/' + params[:user_id]
    flash[:notice] = "User has been updated."
  end
end

#newAdminObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/shoppy/accounts_controller.rb', line 33

def newAdmin
  @admin                          = Admin.new
  @admin.name                     = params[:name]
  @admin.phone                    = params[:phone]
  @admin.password                 = params[:password]
  @admin.password_confirmation    = params[:password_confirmation]
  @admin.email                    = params[:email]
  @admin.gender                   = params[:gender]
  if @admin.save
    Log.newEvent("Users", "Admin with email #{@admin.email} was created", current_admin.name)
    redirect_to '/accounts/admins'
    flash[:notice] = "Admin has been created."
  else
    ## TODO: Handle Errors
    redirect_to '/accounts/admins'
    flash[:error] = @admin.errors.messages
  end
end

#newCustomerObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/shoppy/accounts_controller.rb', line 14

def newCustomer
  @customer                       = Customer.new
  @customer.name                  = params[:name]
  @customer.phone                 = params[:phone]
  @customer.password              = params[:password]
  @customer.password_confirmation = params[:password_confirmation]
  @customer.email                 = params[:email]
  @customer.gender                = params[:gender]
  if @customer.save
    Log.newEvent("Users", "Customer with email #{@customer.email} was created", current_admin.name)
    redirect_to '/accounts/customers'
    flash[:notice] = "User has been created."
  else
    ## TODO: Handle Errors
    redirect_to '/accounts/customers'
    flash[:error] = @customer.errors.messages
  end
end

#showObject



52
53
54
55
56
57
58
59
# File 'app/controllers/shoppy/accounts_controller.rb', line 52

def show
  user = User.find_by(id: params[:user_id])
  if user
    @user = user
  else
    page_not_found
  end
end