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

added party list feature

parent 4013142a
......@@ -34,7 +34,7 @@ class CandidatesController < ApplicationController
end
def show
@candidate = Candidate.find(params[:id])
@party_list = PartyList.where(candidate_id: (@candidate.id))
@votes = Vote.where(candidate_id: (@candidate.id))
@votes_male = Array.new
@votes_female = Array.new
......@@ -48,7 +48,6 @@ class CandidatesController < ApplicationController
end
def destroy
@candidate = Candidate.find(params[:id])
@votes = Vote.where(candidate_id: (@candidate.id))
@votes.destroy_all
@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,6 +36,7 @@ class PositionsController < ApplicationController
render "admin/positions/show.html.erb"
end
def destroy
@position = Position.find(params[:id])
@candidates = Candidate.where(position_id: (@position.id))
......
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
def index
@positions = Position.all
@party_lists = PartyList.all
render "pages/index.html.erb"
end
def about
render "pages/about.html.erb"
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
before_action :authenticate_user!
def index
@positions = Position.all
@votes = current_user.votes
@candidates = Candidate.all
@positions = Position.all
render "votes/index.html.erb"
end
def new
if Vote.exists?(user: current_user)
redirect_to root_path, alert: "You have already voted."
else
@user = current_user
@user.votes.build
@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
render "votes/new.html.erb"
end
end
@all_candidates.each do |c1|
@candidates.push(Candidate.find(c1))
end
if @candidates.empty?
redirect_to root_path, notice: "You have already voted all candidates"
end
end
def create
@candidate = Candidate.find(vote_params[:candidate_id])
@vote = Vote.new(vote_params)
@vote.user = current_user
if @vote.save
def checkout
if current_user.update(vote_params)
redirect_to root_path
else
render :action =>'show'
@user = current_user
@positions = Position.all
redirect_to vote_path, alert: "Please accomplish all required fields."
end
end
def update
def profile
if user_signed_in?
@user = current_user
@votes = Vote.where(user_id: (@user.id))
else
redirect_to root_path
end
def show
@positions = Position.all
@votes = current_user.votes
@candidates = Candidate.all
end
private
def vote_params
params.require(:vote).permit!
end
......
......@@ -3,6 +3,7 @@ class Candidate < ActiveRecord::Base
validates :last_name, presence: true
validates :slogan, uniqueness: true, presence: true
belongs_to :position
belongs_to :party_list
has_many :votes
def to_s
"#{first_name} #{last_name}"
......
class PartyList < ActiveRecord::Base
has_many :candidates
validates :name, presence: true, uniqueness: true
end
......@@ -2,6 +2,7 @@
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :nickname %>
<%= f.association :party_list, include_blank: false %>
<%= f.association :position, include_blank: false %>
<%= f.input :slogan %>
<%= f.submit %>
......
<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>
<li>Votes: <%= @votes_male.size + @votes_female.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>
<% if user_signed_in? %>
<h2>Please take a look at our candidates!</h2>
<hr>
<% if current_user.email == "admin_email@yahoo.com" %>
<%= link_to "Manage Candidates", admin_candidates_path %> ||
<%= link_to "Manage Positions", admin_positions_path %> ||
<%= link_to "Vote", votes_path %>
<% else %>
<%= link_to "Vote", votes_path %>
<% end %>
<% if current_user.email == "admin_email@yahoo.com" %>
<%= link_to "Manage Candidates", admin_candidates_path %> ||
<%= link_to "Manage Positions", admin_positions_path %> ||
<%= link_to "Manage Party Lists", admin_party_lists_path %> ||
<%= link_to "Vote", votes_new_path %> ||
<%= link_to "About", pages_about_path %> ||
<%= 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>
<h1>List of Candidates</h1>
<% @positions.each do |p| %>
<h2><%= p.name %></h2>
<ul>
<% p.candidates.each do |c| %>
<li><%= link_to c.nickname, admin_candidate_path(c.id) %></li>
<table>
<tr>
<th>Full Name</th>
<th>Number of Votes</th>
</tr>
<% p.candidates.each do |candidate| %>
<td><%= candidate.first_name %> <%= candidate.last_name %></td>
<td><%= candidate.votes.count %></td>
<% end %>
</ul>
<%= link_to "View all Candidates", "/positions/#{p.id}" %>
</table>
<% end %>
<hr>
<%= link_to "View all Candidates", candidates_path %>
<%= link_to "View all Party Lists", party_lists_index_path %>
<% 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>
<% 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>
<%= simple_form_for(@vote) do |v| %>
<% @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>
<%= render partial: "form" %>
<%= v.button :submit, "Vote" %>
<% end %>
<% end %>
<%= link_to "Back to Homepage", root_path %>
<h1>My Vote Record</h1>
<h1>Vote Record</h1>
<table class="table">
<thead>
<th>Candidate</th>
......@@ -27,5 +27,6 @@
<% end %>
</tbody>
</table>
<a href="http://localhost:3000/votes/new" class="btn btn-primary" role="button">Add Vote</a>
\ No newline at end of file
<hr>
<%= link_to "Vote", votes_path %>
<%= link_to "Back to Homepage", root_path %>
\ No newline at end of file
<h1>Profile Page</h1>
<h2><%= @user.first_name %> <%= @user.last_name %></h2>
<hr>
<table class="table table-striped">
<thead>
<th>Vote ID</th>
......@@ -16,3 +17,5 @@
<% end %>
</tbody>
</table>
<hr>
......@@ -4,11 +4,18 @@ Rails.application.routes.draw do
namespace :admin do
resources :positions
resources :candidates
resources :party_lists
end
resources :candidates
resources :positions
resources :votes
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
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 @@
#
# 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|
t.string "first_name"
......@@ -21,6 +21,17 @@ ActiveRecord::Schema.define(version: 20160720053151) do
t.integer "position_id"
t.datetime "created_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
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