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' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
......
GEM GEM
remote: https://rubygems.org/ remote: http://rubygems.org/
specs: specs:
actioncable (5.0.0) actioncable (5.0.0)
actionpack (= 5.0.0) actionpack (= 5.0.0)
...@@ -190,4 +190,4 @@ DEPENDENCIES ...@@ -190,4 +190,4 @@ DEPENDENCIES
web-console web-console
BUNDLED WITH BUNDLED WITH
1.12.5 1.10.4
...@@ -3,9 +3,9 @@ module Admin ...@@ -3,9 +3,9 @@ module Admin
before_action :authenticate_user! before_action :authenticate_user!
def index def index
@positions = Position.all @positions = Position.all
@candidates = Candidate.all @candidates = Candidate.all
@votes = Vote.all
@users = User.all @users = User.all
@votes = Vote.all
render "admin/users/index.html.erb" render "admin/users/index.html.erb"
end end
...@@ -52,6 +52,16 @@ module Admin ...@@ -52,6 +52,16 @@ module Admin
render "admin/users/show.html.erb" render "admin/users/show.html.erb"
end 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 def user_params
params.require(:user).permit! 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 ...@@ -18,8 +18,8 @@ class Candidate < ApplicationRecord
votes.select { |vote| vote.user.gender == 'Female' } votes.select { |vote| vote.user.gender == 'Female' }
end end
def other_votes def others_votes
votes.select { |vote| vote.user.gender == 'Other' } votes.select { |vote| vote.user.gender == 'Others' }
end end
end end
class User < ApplicationRecord 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 has_many :votes, dependent: :destroy
......
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
<tr> <tr>
<td><center> <%= @candidate.male_votes.size %> Votes <br> <progress value="<%= @candidate.male_votes.size %>" max="<%= @candidate.votes.count %>" ></center></td> <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.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> <td><center><%= @candidate.votes.count %></center></td>
</tr> </tr>
</table> </table>
...@@ -51,7 +51,7 @@ ...@@ -51,7 +51,7 @@
<ul> <ul>
<li> <li>
<% @candidate.votes.each do |v|%> <% @candidate.votes.each do |v|%>
Voter ID: <%= "#{v.user_id}"%> <%= "#{v.user_id}"%>
<br> <br>
<p> <p>
Comments: <%=v.comment%> Comments: <%=v.comment%>
......
...@@ -2,13 +2,13 @@ ...@@ -2,13 +2,13 @@
<div class ="container"> <div class ="container">
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
<h2>Hey <%=current_user.first_name%>!</h2> <h2>Hey <%=current_user.first_name%>!</h2>
<br> <br>
<h4>You Voted for:</h4> <h4>You Voted for:</h4>
<br> <br>
<ul> <ul>
<% if @positions.size > 0 %> <% if @positions.size > 0 %>
<ul> <ul>
<%@positions.each do |p|%> <%@positions.each do |p|%>
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
<%if v.user_id == current_user.id%> <%if v.user_id == current_user.id%>
<h4><%= v.candidate.full_name %></h4> <h4><%= v.candidate.full_name %></h4>
<p> <p>
<h5>Comments: <%=v.comment%></h5> <h5>Comments: <%=v.comment%></h5>
<%voted_for = true%> <%voted_for = true%>
<% end %> <% end %>
<br> <br>
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
<%else%> <%else%>
<h5></h5> <h5></h5>
<%end%> <%end%>
</ul> </ul>
</div> </div>
</div> </div>
</div> </div>
\ No newline at end of file
<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 ...@@ -12,6 +12,7 @@ Rails.application.routes.draw do
get "/about/guian", to: "about#guian" get "/about/guian", to: "about#guian"
get "/about/principe", to: "about#principe" get "/about/principe", to: "about#principe"
get "/admin/users", to: "admin/users#index"
namespace :voting do namespace :voting do
resources :votes 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