Commit 2cc6b35b authored by Bianca Tarun's avatar Bianca Tarun

added party list feature

parent 4013142a
...@@ -34,7 +34,7 @@ class CandidatesController < ApplicationController ...@@ -34,7 +34,7 @@ class CandidatesController < ApplicationController
end end
def show def show
@candidate = Candidate.find(params[:id]) @candidate = Candidate.find(params[:id])
@party_list = PartyList.where(candidate_id: (@candidate.id))
@votes = Vote.where(candidate_id: (@candidate.id)) @votes = Vote.where(candidate_id: (@candidate.id))
@votes_male = Array.new @votes_male = Array.new
@votes_female = Array.new @votes_female = Array.new
...@@ -48,7 +48,6 @@ class CandidatesController < ApplicationController ...@@ -48,7 +48,6 @@ class CandidatesController < ApplicationController
end end
def destroy def destroy
@candidate = Candidate.find(params[:id]) @candidate = Candidate.find(params[:id])
@votes = Vote.where(candidate_id: (@candidate.id)) @votes = Vote.where(candidate_id: (@candidate.id))
@votes.destroy_all @votes.destroy_all
@candidate.destroy @candidate.destroy
......
module Admin
class PartyListsController < ApplicationController
before_action :authenticate_user!
def index
@party_lists = PartyList.all
render "admin/party_lists/index.html.erb"
end
def new
@party_list = PartyList.new
render "admin/party_lists/new.html.erb"
end
def create
@party_list = PartyList.new(party_list_params())
if @party_list.save
redirect_to admin_party_lists_path(@party_list.id)
else
render "admin/party_lists/new.html.erb"
end
end
def edit
@party_list = PartyList.find(params[:id])
end
def update
@party_list = PartyList.find(params[:id])
if @party_list.update(party_list_params())
redirect_to admin_party_list_path(@party_list.id)
else
render "admin/party_lists/edit.html.erb"
end
end
def show
@party_list = PartyList.find(params[:id])
@candidates = Candidate.all
render "admin/party_lists/show.html.erb"
end
def destroy
@party_list = PartyList.find(params[:id])
@candidates = Candidate.where(party_list_id: (@party_list.id))
@candidates.each do |candidate|
candidate.party_list == ""
end
@party_list.destroy
redirect_to admin_party_lists_path
end
def party_list_params
params.require(:party_list).permit!
end
end
end
\ No newline at end of file
...@@ -36,12 +36,13 @@ class PositionsController < ApplicationController ...@@ -36,12 +36,13 @@ class PositionsController < ApplicationController
render "admin/positions/show.html.erb" render "admin/positions/show.html.erb"
end end
def destroy def destroy
@position = Position.find(params[:id]) @position = Position.find(params[:id])
@candidates = Candidate.where(position_id: (@position.id)) @candidates = Candidate.where(position_id: (@position.id))
@candidates.each do |candidate| @candidates.each do |candidate|
@votes = Vote.where(candidate_id: (candidate.id)) @votes = Vote.where(candidate_id: (candidate.id))
@votes.destroy_all @votes.destroy_all
end end
@position.destroy @position.destroy
......
class CandidatesController < ApplicationController
before_action :authenticate_user!
def index
@positions = Position.all
render "candidates/index.html.erb"
end
end
\ No newline at end of file
class PagesController < ApplicationController class PagesController < ApplicationController
def index def index
@positions = Position.all @positions = Position.all
@party_lists = PartyList.all
render "pages/index.html.erb" render "pages/index.html.erb"
end end
def about
render "pages/about.html.erb"
end
end end
\ No newline at end of file
class PartyListsController < ApplicationController
before_action :authenticate_user!
def index
@party_lists = PartyList.all
@candidates = Candidate.all
render "party_lists/index.html.erb"
end
end
\ No newline at end of file
class VotesController < ApplicationController class VotesController < ApplicationController
def index before_action :authenticate_user!
@votes = current_user.votes def index
@candidates = Candidate.all @positions = Position.all
@positions = Position.all @votes = current_user.votes
render "votes/index.html.erb" @candidates = Candidate.all
end
def new
@positions = Position.all
@vote = Vote.new
@candidates = []
@all_candidates= Candidate.pluck(:id)
@candidate_voted = current_user.votes.pluck(:candidate_id).uniq
@all_candidates.each do |candidate|
@candidate_voted.each do |voted|
if candidate = voted
@all_candidates.delete(candidate)
end
end
end
@all_candidates.each do |c1|
@candidates.push(Candidate.find(c1))
end end
def new
if @candidates.empty? if Vote.exists?(user: current_user)
redirect_to root_path, notice: "You have already voted all candidates" redirect_to root_path, alert: "You have already voted."
else
@user = current_user
@user.votes.build
@positions = Position.all
render "votes/new.html.erb"
end end
end end
def checkout
def create if current_user.update(vote_params)
@candidate = Candidate.find(vote_params[:candidate_id])
@vote = Vote.new(vote_params)
@vote.user = current_user
if @vote.save
redirect_to root_path redirect_to root_path
else else
render :action =>'show' @user = current_user
@positions = Position.all
redirect_to vote_path, alert: "Please accomplish all required fields."
end end
end end
def update def profile
end if user_signed_in?
@user = current_user
def show @votes = Vote.where(user_id: (@user.id))
@positions = Position.all else
@votes = current_user.votes redirect_to root_path
@candidates = Candidate.all end
end end
private
def vote_params
def vote_params
params.require(:vote).permit! params.require(:vote).permit!
end end
end end
\ No newline at end of file
...@@ -3,6 +3,7 @@ class Candidate < ActiveRecord::Base ...@@ -3,6 +3,7 @@ class Candidate < ActiveRecord::Base
validates :last_name, presence: true validates :last_name, presence: true
validates :slogan, uniqueness: true, presence: true validates :slogan, uniqueness: true, presence: true
belongs_to :position belongs_to :position
belongs_to :party_list
has_many :votes has_many :votes
def to_s def to_s
"#{first_name} #{last_name}" "#{first_name} #{last_name}"
......
class PartyList < ActiveRecord::Base
has_many :candidates
validates :name, presence: true, uniqueness: true
end
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
<%= f.input :first_name %> <%= f.input :first_name %>
<%= f.input :last_name %> <%= f.input :last_name %>
<%= f.input :nickname %> <%= f.input :nickname %>
<%= f.association :party_list, include_blank: false %>
<%= f.association :position, include_blank: false %> <%= f.association :position, include_blank: false %>
<%= f.input :slogan %> <%= f.input :slogan %>
<%= f.submit %> <%= f.submit %>
......
<h2>Candidate: <%= @candidate.first_name %> <%= @candidate.last_name %> </h2> <h2>Candidate: <%= @candidate.first_name %> <%= @candidate.last_name %> </h2>
<h3><%= @candidate.slogan %></h3> <h4>Party List: <%= @candidate.party_list.name %></h4>
<h3><%= @candidate.last_name %>'s Slogan: <%= @candidate.slogan %></h3>
<ul> <ul>
<li>Votes: <%= @votes_male.size + @votes_female.size %></li> <li>Votes: <%= @votes_male.size + @votes_female.size %></li>
<li>Male Voters: <%= @votes_male.size %></li> <li>Male Voters: <%= @votes_male.size %></li>
......
<%= simple_form_for([:admin, @party_list]) do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.submit %>
<% end %>
\ No newline at end of file
<h1>Admin Edit Party List</h1>
<%= render partial: "form" %>
<%= link_to "Back to Party Lists", admin_party_lists_path %>
\ No newline at end of file
<h1>Admin Party Lists</h1>
<hr>
<%= link_to("New Party List", new_admin_party_list_path) %>
<hr>
<table width="100%">
<tr>
<th>Name</th>
</tr>
<% @party_lists.each do |p| %>
<tr>
<td><%= p.name %></td>
<td>
<%= link_to "Show", admin_party_list_path(p.id) %>
<%= link_to "Edit", edit_admin_party_list_path(p.id) %>
<%= link_to "Delete", admin_party_list_path(p.id), method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
</tr>
</table>
<ul><% p.candidates.each do |candidate| %>
<li>
<%= candidate.first_name %> <%= candidate.last_name %>
</li>
<% end %>
</ul>
<% end %>
<h1>Admin New Party List</h1>
<%= render partial: "form" %>
<%= link_to "Back to Party Lists", admin_party_lists_path %>
\ No newline at end of file
<h1>Party List: <%= @party_list.name %></h1>
<hr>
<h3>Party List Description </h3>
<%= @party_list.description %>
<% @party_list.candidates.each do |candidate| %>
<ul><%= candidate.first_name %> <%= candidate.last_name %>
<% end %>
<hr>
<%= link_to "Back to Party Lists", admin_party_lists_path %>
\ No newline at end of file
<h1>List of Candidates</h1>
<% @positions.each do |p| %>
<h2><%= p.name %></h2>
<table>
<tr>
<th>Full Name</th>
</tr>
<% p.candidates.each do |candidate| %>
<td><%= candidate.first_name %> <%= candidate.last_name %></td>
<% end %>
</table>
<% end %>
<h1>Official Election Ballot</h1> <h1>Official Election Ballot</h1>
<% if user_signed_in? %> <% if user_signed_in? %>
<h2>Please take a look at our candidates!</h2>
<hr> <hr>
<% if current_user.email == "admin_email@yahoo.com" %> <% if current_user.email == "admin_email@yahoo.com" %>
<%= link_to "Manage Candidates", admin_candidates_path %> || <%= link_to "Manage Candidates", admin_candidates_path %> ||
<%= link_to "Manage Positions", admin_positions_path %> || <%= link_to "Manage Positions", admin_positions_path %> ||
<%= link_to "Vote", votes_path %> <%= link_to "Manage Party Lists", admin_party_lists_path %> ||
<% else %> <%= link_to "Vote", votes_new_path %> ||
<%= link_to "Vote", votes_path %> <%= link_to "About", pages_about_path %> ||
<% end %> <%= link_to "User Profile", votes_user_profile_path %>
<% else %>
<%= link_to "About", pages_about_path %> ||
<%= link_to "Vote", votes_new_path %> ||
<%= link_to "User Profile", votes_user_profile_path %>
<% end %>
<hr> <hr>
<h1>List of Candidates</h1>
<% @positions.each do |p| %> <% @positions.each do |p| %>
<h2><%= p.name %></h2> <h2><%= p.name %></h2>
<ul> <table>
<% p.candidates.each do |c| %> <tr>
<li><%= link_to c.nickname, admin_candidate_path(c.id) %></li> <th>Full Name</th>
<% end %> <th>Number of Votes</th>
</ul> </tr>
<%= link_to "View all Candidates", "/positions/#{p.id}" %> <% p.candidates.each do |candidate| %>
<td><%= candidate.first_name %> <%= candidate.last_name %></td>
<td><%= candidate.votes.count %></td>
<% end %>
</table>
<% end %> <% end %>
<hr>
<%= link_to "View all Candidates", candidates_path %>
<%= link_to "View all Party Lists", party_lists_index_path %>
<% else %> <% else %>
<h4>You are not signed in. Please <%= link_to "Login", new_user_session_path %> or <%= link_to "Sign Up", new_user_registration_path %> to proceed with voting.</h4> <h4>You are not signed in. Please <%= link_to "Login", new_user_session_path %> or <%= link_to "Sign Up", new_user_registration_path %> to proceed with voting.</h4>
<% end %> <% end %>
\ No newline at end of file
<h1>Party Lists</h1>
<% @party_lists.each do |p| %>
<%= p.name %>
<hr>
<h3>Party List Description</h3>
<%= p.description %>
<h3>Party List Candidates</h3>
<% p.candidates.each do |candidate| %>
<%= candidate.first_name %> <%= candidate.last_name %>
<% end %>
<% end %>
<hr>
<%= link_to "Back to Homepage", root_path %>
\ No newline at end of file
<h1>Vote for your candidates.</h1>
<%= simple_form_for(@user, url: checkout_path, method: :post) do |v| %>
<% @positions.each do |p| %>
<%= v.simple_fields_for :votes do |f| %>
<h2><%= p.name %></h2>
<div class="row">
<div class="col-md-6">
<%= f.association :candidate, :collection => Candidate.where(position_id: (p.id)), include_blank: false, :label_method => lambda {|candidate| "#{candidate.last_name}, #{candidate.first_name}"}, as: :radio_buttons , label: "Candidates: "%>
<% Candidate.where(position_id: (p.id)).each do |candidate| %>
<p><div class="btn btn-primary btn-xs" data-toggle="popover" data-content="<%= candidate.slogan %>" style="margin-top: 2px; margin-bottom: 1px", data-trigger="focus", role="button", tabindex="0", title="<%= candidate.first_name %> <%= candidate.last_name %>"> View <%= candidate.last_name %>'s Slogan</div></p>
<% end -%>
</div>
<div class="col-md-6">
<br><br><br>
</div>
</div>
<%= f.input :comments %>
<% end -%>
<% end -%>
<%= v.button :submit, "Vote", class: "btn btn-primary" %>
<%= link_to "Back to Homepage", root_path, class: "btn btn-default" %>
<%= link_to "Vote Record", votes_index_path
<% end %>
<script>
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
</script>
\ No newline at end of file
<h1>Votes</h1>
<%= link_to "Vote Now!", new_vote_path %>
<table width="100%">
<tr>
<th>#</th>
<th>User ID</th>
<th>Candidate</th>
<th>Position</th>
</tr>
<% @votes.each do |vote| %>
<tr>
<td><%= link_to "#{vote.id}", vote_path(vote.id) %></td>
<td><%= vote.user_id %></td>
<td>
<%= vote.candidate.first_name %> "<%= vote.candidate.nickname %>" <%= vote.candidate.last_name %>
</td>
<td>
<%= vote.candidate.position.name %>
</td>
</tr>
<% end %>
</table>
\ No newline at end of file
<h1>Vote</h1> <h1>Vote</h1>
<%= simple_form_for(@vote) do |v| %> <%= render partial: "form" %>
<% @positions.each do |p| %>
<h2><%= p.name %></h2>
<%= v.association :candidate, :collection => Candidate.where(position_id: (p.id)), include_blank: false %>
<%= v.input :comments %><br>
<%= v.button :submit, "Vote" %> <%= link_to "Back to Homepage", root_path %>
<% end %>
<% end %>
<h1>My Vote Record</h1> <h1>Vote Record</h1>
<table class="table"> <table class="table">
<thead> <thead>
<th>Candidate</th> <th>Candidate</th>
...@@ -27,5 +27,6 @@ ...@@ -27,5 +27,6 @@
<% end %> <% end %>
</tbody> </tbody>
</table> </table>
<hr>
<a href="http://localhost:3000/votes/new" class="btn btn-primary" role="button">Add Vote</a> <%= link_to "Vote", votes_path %>
\ No newline at end of file <%= link_to "Back to Homepage", root_path %>
\ No newline at end of file
<h1>Profile Page</h1> <h1>Profile Page</h1>
<h2><%= @user.first_name %> <%= @user.last_name %></h2>
<hr>
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
<th>Vote ID</th> <th>Vote ID</th>
...@@ -15,4 +16,6 @@ ...@@ -15,4 +16,6 @@
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>
</table> </table>
\ No newline at end of file <hr>
...@@ -4,11 +4,18 @@ Rails.application.routes.draw do ...@@ -4,11 +4,18 @@ Rails.application.routes.draw do
namespace :admin do namespace :admin do
resources :positions resources :positions
resources :candidates resources :candidates
resources :party_lists
end end
resources :candidates resources :candidates
resources :positions resources :positions
resources :votes
root to: "pages#index" root to: "pages#index"
get'/pages/about', to: "pages#about"
get'/votes/new', to: "votes#new"
get'/party_lists/index', to: "party_lists#index"
get'/votes/user_profile', to: "votes#profile"
get'/votes/index', to: "votes#index"
post'/checkout', to: 'votes#checkout', as: :checkout
end end
class CreatePartyLists < ActiveRecord::Migration
def change
create_table :party_lists do |t|
t.string :name
t.timestamps null: false
end
end
end
class AddPartyListToCandidates < ActiveRecord::Migration
def change
add_reference :candidates, :party_list, index: true, foreign_key: true
end
end
class AddDescriptionToPartyLists < ActiveRecord::Migration
def change
add_column :party_lists, :description_text, :string
remove_column :party_lists, :description_text, :string
end
end
class AddDescriptionToPartyLists < ActiveRecord::Migration
def change
add_column :party_lists, :description, :text
end
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160720053151) do ActiveRecord::Schema.define(version: 20160720134047) do
create_table "candidates", force: :cascade do |t| create_table "candidates", force: :cascade do |t|
t.string "first_name" t.string "first_name"
...@@ -19,8 +19,19 @@ ActiveRecord::Schema.define(version: 20160720053151) do ...@@ -19,8 +19,19 @@ ActiveRecord::Schema.define(version: 20160720053151) do
t.string "nickname" t.string "nickname"
t.text "slogan" t.text "slogan"
t.integer "position_id" t.integer "position_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "party_list_id"
end
add_index "candidates", ["party_list_id"], name: "index_candidates_on_party_list_id"
create_table "party_lists", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "description_text"
t.text "description"
end end
create_table "positions", force: :cascade do |t| create_table "positions", force: :cascade do |t|
......
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
two:
name: MyString
require 'test_helper'
class PartyListTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
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