Commit 2b896259 authored by April Guian's avatar April Guian

Jasmine's Individual Task

parent 47ab9dcc
source 'https://rubygems.org'
source 'http://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
......
GEM
remote: https://rubygems.org/
remote: http://rubygems.org/
specs:
actioncable (5.0.0)
actionpack (= 5.0.0)
......@@ -190,4 +190,4 @@ DEPENDENCIES
web-console
BUNDLED WITH
1.12.5
1.10.4
......@@ -4,8 +4,8 @@ module Admin
def index
@positions = Position.all
@candidates = Candidate.all
@votes = Vote.all
@users = User.all
@votes = Vote.all
render "admin/users/index.html.erb"
end
......@@ -52,6 +52,16 @@ module Admin
render "admin/users/show.html.erb"
end
#def destroy
# @user = User.find(params[:id])
# @user.votes.each do |v|
# v.destroy!
# end
# @user.destroy!
# redirect_to admin_users_path
#end
def user_params
params.require(:user).permit!
......
class UsersController < ApplicationController
before_filter :authenticate_user!
before_filter :admin_only, :except => :show
def index
@users = User.all
end
def show
@user = User.find(params[:id])
unless current_user.admin?
unless @user == current_user
redirect_to :back, :alert => "Access denied."
end
end
end
def update
@user = User.find(params[:id])
if @user.update_attributes(secure_params)
redirect_to users_path, :notice => "User updated."
else
redirect_to users_path, :alert => "Unable to update user."
end
end
def destroy
user = User.find(params[:id])
user.destroy
redirect_to users_path, :notice => "User deleted."
end
private
def admin_only
unless current_user.admin?
redirect_to root_path, :alert => "Access denied."
end
end
def secure_params
params.require(:user).permit(:role)
end
end
\ No newline at end of file
......@@ -18,8 +18,8 @@ class Candidate < ApplicationRecord
votes.select { |vote| vote.user.gender == 'Female' }
end
def other_votes
votes.select { |vote| vote.user.gender == 'Other' }
def others_votes
votes.select { |vote| vote.user.gender == 'Others' }
end
end
class User < ApplicationRecord
enum role: {voter: 0, admin: 1}
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :voter
end
has_many :votes, dependent: :destroy
devise :database_authenticatable, :registerable,
......
......@@ -40,7 +40,7 @@
<tr>
<td><center> <%= @candidate.male_votes.size %> Votes <br> <progress value="<%= @candidate.male_votes.size %>" max="<%= @candidate.votes.count %>" ></center></td>
<td><center> <%= @candidate.female_votes.size %> Votes <br> <progress value="<%= @candidate.female_votes.size %>" max="<%= @candidate.votes.count %>" ></center></td>
<td><center><%= @candidate.other_votes.size %> Votes <br> <progress value="<%= @candidate.other_votes.size %>" max="<%= @candidate.votes.count %>" ></center></td>
<td><center><%= @candidate.others_votes.size %> Votes <br> <progress value="<%= @candidate.others_votes.size %>" max="<%= @candidate.votes.count %>" ></center></td>
<td><center><%= @candidate.votes.count %></center></td>
</tr>
</table>
......@@ -51,7 +51,7 @@
<ul>
<li>
<% @candidate.votes.each do |v|%>
Voter ID: <%= "#{v.user_id}"%>
<%= "#{v.user_id}"%>
<br>
<p>
Comments: <%=v.comment%>
......
<td>
<%= link_to user.email, user %>
</td>
<td>
<%= form_for(user) do |f| %>
<%= f.select(:role, User.roles.keys.map {|role| [role.titleize,role]}) %>
<%= f.submit 'Change Role', :class => 'button-xs' %>
<% end %>
</td>
<td>
<%= link_to("Delete user", user_path(user), :data => { :confirm => "Are you sure?" }, :method => :delete, :class => 'button-xs') unless user == current_user %>
</td>
\ No newline at end of file
<div class="container">
<div class="row">
<h3>Users</h3>
<div class="column">
<table class="table">
<tbody>
<% @users.each do |user| %>
<tr>
<%= render user %>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
\ No newline at end of file
<h3>User</h3>
<p>Name: <%= @user.name if @user.name %></p>
<p>Email: <%= @user.email if @user.email %></p>
......@@ -12,6 +12,7 @@ Rails.application.routes.draw do
get "/about/guian", to: "about#guian"
get "/about/principe", to: "about#principe"
get "/admin/users", to: "admin/users#index"
namespace :voting do
resources :votes
......
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