Skip to content

Commit

Permalink
do not need to edit users
Browse files Browse the repository at this point in the history
  • Loading branch information
Jennifer Konikowski committed Jun 14, 2019
1 parent e6db88c commit 6589e3a
Show file tree
Hide file tree
Showing 20 changed files with 181 additions and 316 deletions.
67 changes: 22 additions & 45 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class UsersController < ApplicationController
before_action :set_user, only: %i[edit promote demote update destroy]
before_action :set_user, only: %i[promote demote deactivate reactivate]
load_and_authorize_resource
before_action :authenticate_user!

Expand All @@ -18,49 +18,31 @@ def index
@users = @filterrific.find.page(params[:page])
end

# GET /users/new
def new
@user = User.new
end

def edit; end

# POST /users
# POST /users.json
def create
@user = User.new(user_params)

respond_to do |format|
if @user.save
format.html { redirect_to users_url, notice: "User #{@user.email} was successfully created." }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# def edit; end

# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to users_url, notice: 'User was successfully updated.' }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
# def update
# respond_to do |format|
# if @user.update(user_params)
# format.html { redirect_to users_url, notice: 'User was successfully updated.' }
# else
# format.html { render :edit }
# format.json { render json: @user.errors, status: :unprocessable_entity }
# end
# end
# end

# PUT /users/1/deactivate
def deactivate
@user.update(deactivated: true)
redirect_to users_url, notice: "#{@user.email} was successfully deactivated."
end

# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
# PUT /users/1/reactivate
def reactivate
@user.update(deactivated: false)
redirect_to users_url, notice: "#{@user.email} was successfully reactivated."
end

# PUT /users/1/promote
Expand All @@ -79,12 +61,7 @@ def demote

# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id]) if params[:id]
# @user = User.find(params[:id]) if params[:id]
@user = User.find(params[:user_id]) if params[:user_id]
end

# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :email, :years_experience, :is_admin)
end
end
4 changes: 1 addition & 3 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ def initialize(user)
can :read, Course
can :read, Review
return if user.blank?
return if user.deactivated

can :read, Course
can :read, School
can :create, Review
can [:update, :delete], Review do |r|
r.user == user
end
can [:read, :update, :delete], User do |u|
u == user
end
return unless user.admin?

can :manage, :all
Expand Down
12 changes: 6 additions & 6 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class User < ApplicationRecord

validates :email, presence: true, uniqueness: { case_sensitive: false }

has_many :reviews, dependent: :destroy

filterrific(
default_filter_params: { sorted_by: 'email_desc' },
available_filters: %i[
Expand Down Expand Up @@ -53,10 +55,10 @@ class User < ApplicationRecord
case sort_option.to_s
when /^email_/
order(Arel.sql("LOWER(users.email) #{direction}"))
when /^years_experience_/
order(Arel.sql("users.years_experience #{direction}"))
when /^admin_/
order(Arel.sql("users.is_admin #{direction}"))
when /^deactivated_/
order(Arel.sql("users.deactivated #{direction}"))
else
raise(ArgumentError, "Invalid sort option: #{sort_option.inspect}")
end
Expand All @@ -75,8 +77,6 @@ def self.options_for_sorted_by
[
['Email (a-z)', 'email_asc'],
['Email (z-a)', 'email_desc'],
['Years Experience (lowest first)', 'years_experience_asc'],
['Years Experience (highest first)', 'years_experience_desc'],
['Admin? (false first)', 'admin_asc'],
['Admin? (true first)', 'admin_desc']
]
Expand All @@ -93,17 +93,17 @@ def self.options_for_sorted_by
# confirmed_at :datetime
# current_sign_in_at :datetime
# current_sign_in_ip :inet
# deactivated :boolean default(FALSE)
# email :string default(""), not null
# encrypted_password :string default(""), not null
# is_admin :boolean
# is_admin :boolean default(FALSE)
# last_sign_in_at :datetime
# last_sign_in_ip :inet
# remember_created_at :datetime
# reset_password_sent_at :datetime
# reset_password_token :string
# sign_in_count :integer default(0), not null
# unconfirmed_email :string
# years_experience :integer
# created_at :datetime not null
# updated_at :datetime not null
#
Expand Down
5 changes: 0 additions & 5 deletions app/views/devise/registrations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
<%= f.text_field :email, class: 'form-control', autofocus: true, autocomplete: "email", placeholder: 'Email Address' %>
</div>

<div class="form-group">
<%= f.label :years_experience %>
<%= f.number_field :years_experience, class: 'form-control' %>
</div>

<div class="form-group">
<%= f.label :password %>
<% if @minimum_password_length %>
Expand Down
3 changes: 0 additions & 3 deletions app/views/layouts/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
<li class="nav-item">
<%= link_to 'Update Password', edit_user_registration_path(current_user), class: 'nav-link' %>
</li>
<li class="nav-item">
<%= link_to 'Update Profile', edit_user_path(current_user), class: 'nav-link' %>
</li>
<li class="nav-item">
<%= link_to('Sign out', destroy_user_session_path, method: 'delete', class: 'nav-link destructive') %>
</li>
Expand Down
35 changes: 0 additions & 35 deletions app/views/users/_form.html.erb

This file was deleted.

13 changes: 8 additions & 5 deletions app/views/users/_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<thead class="thead-light">
<tr>
<th scope="col"><%= filterrific_sorting_link(@filterrific, :email) %></th>
<th scope="col"><%= filterrific_sorting_link(@filterrific, :years_experience) %></th>
<th scope="col"><%= filterrific_sorting_link(@filterrific, :admin) %></th>
<th scope="col"><%= filterrific_sorting_link(@filterrific, :deactivated) %></th>
<th scope="col" colspan="3"></th>
</tr>
</thead>
Expand All @@ -27,15 +27,18 @@
<% @users.each do |user| %>
<tr>
<td><%= user.email %></td>
<td><%= user.years_experience %></td>
<td><%= user.is_admin %></td>
<td><%= user.is_admin ? "👍" : "" %></td>
<td><%= user.deactivated ? "👍" : "" %></td>
<% if user.admin? %>
<td><%= link_to 'Demote', user_demote_path(user), method: :put %></td>
<% else %>
<td><%= link_to 'Promote', user_promote_path(user), method: :put %></td>
<% end %>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% if user.deactivated %>
<td><%= link_to 'Reactivate', user_reactivate_path(user), method: :put, data: { confirm: 'Are you sure?' } %></td>
<% else %>
<td><%= link_to 'Deactivate', user_deactivate_path(user), method: :put, data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
<% end %>
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/_user.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true

json.extract! user, :id, :name, :email, :years_experience, :is_admin, :created_at, :updated_at
json.extract! user, :id, :email, :is_admin, :deactivated, :created_at, :updated_at
json.url user_url(user, format: :json)
3 changes: 0 additions & 3 deletions app/views/users/edit.html.erb

This file was deleted.

1 change: 0 additions & 1 deletion app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<% if can? :manage, User %>
<h1>Users</h1>
<%= link_to 'New User', new_user_path %>
<br/>
<div class="card card-body bg-light">
<%= form_for_filterrific @filterrific do |f| %>
Expand Down
3 changes: 0 additions & 3 deletions app/views/users/new.html.erb

This file was deleted.

4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
end
end

resources :users, only: %i[index new edit create update destroy] do
resources :users, only: %i[index] do
put '/promote', to: 'users#promote'
put '/demote', to: 'users#demote'
put '/deactivate', to: 'users#deactivate'
put '/reactivate', to: 'users#reactivate'
end
devise_for :users, path: ''
end
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20190614114620_default_value_to_is_admin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class DefaultValueToIsAdmin < ActiveRecord::Migration[5.2]
def change
change_column :users, :is_admin, :boolean, default: false
end
end
6 changes: 6 additions & 0 deletions db/migrate/20190614114700_remove_years_experience.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RemoveYearsExperience < ActiveRecord::Migration[5.2]
def change
remove_column :users, :years_experience
add_column :users, :deactivated, :boolean, default: false
end
end
Loading

0 comments on commit 6589e3a

Please sign in to comment.