Commit c241defd authored by Galen Evilla's avatar Galen Evilla

trial2

parent 8318e661
......@@ -9,6 +9,9 @@ gem 'sqlite3'
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
gem 'bootstrap-sass'
gem 'autoprefixer-rails'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
......@@ -22,6 +25,8 @@ gem 'jquery-rails'
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
gem 'sdoc', '~> 0.4.0', group: :doc
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
......@@ -43,6 +48,6 @@ end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'simple_form'
gem 'devise'
gem 'devise'
\ No newline at end of file
gem 'simple_form'
\ No newline at end of file
......@@ -39,7 +39,12 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (7.0.0)
autoprefixer-rails (6.3.6.2)
execjs
bcrypt (3.1.11-x86-mingw32)
bootstrap-sass (3.3.6)
autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4)
builder (3.2.2)
coffee-rails (4.2.1)
coffee-script (>= 2.2.0)
......@@ -68,6 +73,7 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.4)
......@@ -85,7 +91,7 @@ GEM
pkg-config (~> 1.1.7)
orm_adapter (0.5.0)
pkg-config (1.1.7)
puma (3.4.0)
puma (3.5.0)
rack (2.0.1)
rack-test (0.6.3)
rack (>= 1.0)
......@@ -113,6 +119,8 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (11.2.2)
rdoc (4.2.2)
json (~> 1.4)
responders (2.2.0)
railties (>= 4.2.0, < 5.1)
sass (3.4.22)
......@@ -122,6 +130,9 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
simple_form (3.2.1)
actionpack (> 4, < 5.1)
activemodel (> 4, < 5.1)
......@@ -160,6 +171,8 @@ PLATFORMS
x86-mingw32
DEPENDENCIES
autoprefixer-rails
bootstrap-sass
byebug
coffee-rails (~> 4.2)
devise
......@@ -168,6 +181,7 @@ DEPENDENCIES
puma (~> 3.0)
rails (~> 5.0.0)
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
simple_form
sqlite3
turbolinks (~> 5)
......
......@@ -13,4 +13,5 @@
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .
@import "bootstrap-sprockets";
@import "bootstrap";
module Admin
class CandidatesController < ApplicationController
before_action :authenticate_user!
def index
@positions = Position.all
@candidates = Candidate.all
render "admin/candidates/index.html.erb"
end
def show
def new
@candidate = Candidate.new
render "admin/candidates/new.html.erb"
end
def edit
@candidate = Candidate.find(params[:id])
render "admin/candidates/show.html.erb"
end
def update
@candidate = Candidate.find(params[:id])
if @candidate.update(candidate_params())
redirect_to admin_candidates_path(@candidate.id)
else
render "admin/candidates/edit.html.erb"
end
end
def destroy
@candidate = Candidate.find(params[:id])
@candidate.votes.each do |v|
v.destroy!
end
@candidate.destroy!
redirect_to admin_candidates_path
end
def new
@candidate = Candidate.new
render "admin/candidates/new.html.erb"
end
def create
@candidate = Candidate.new(candidate_params())
if @candidate.save
flash[:success] = 'Candidate was created!'
redirect_to admin_candidate_path(@candidate.id)
else
render "admin/candidates/new.html.erb"
end
end
def edit
def show
@candidate = Candidate.find(params[:id])
render "admin/candidates/show.html.erb"
end
def update
@candidate = Candidate.find(params[:id])
if @candidate.update(candidate_params())
redirect_to admin_candidate_path(@candidate.id)
else
render "admin/candidates/edit.html.erb"
end
end
def candidate_params
params.require(:candidate).permit!
end
end
end
\ No newline at end of file
module Admin
class PositionsController < ApplicationController
before_action :authenticate_user!
def index
@positions = Position.all
@positions = Position.all
render "admin/positions/index.html.erb"
end
def show
def new
@position = Position.new
render "admin/positions/new.html.erb"
end
def edit
@position = Position.find(params[:id])
@candidates = @position.candidates
render "admin/positions/show.html.erb"
end
def destroy
@position = Positions.find(params[:id])
@position.destroy!
redirect_to admin_positions_path
def update
@position = Position.find(params[:id])
if @position.update(position_params())
redirect_to admin_positions_path(@position.id), notice: 'Success!'
else
render "admin/position/edit.html.erb"
end
end
def new
@position = Position.new
render "admin/positions/new.html.erb"
def destroy
@candidate = Position.find(params[:id])
@candidate.destroy!
redirect_to admin_positions_path, notice: 'Position and its candidates are deleted!'
end
def create
@position = Position.new(position_params())
if @position.save
redirect_to admin_position_path(@position.id)
redirect_to admin_position_path(@position.id), notice: 'Success!'
else
render "admin/positions/new.html.erb"
end
end
def edit
@position = Positions.find(params[:id])
def show
@position = Position.find(params[:id])
render "admin/positions/show.html.erb"
end
def update
@position = Positions.find(params[:id])
if @position.update(position_params())
redirect_to admin_position_path(@position.id)
else
render "admin_positions/edit.html.erb"
end
end
def position_params
params.require(:position).permit!
end
end
end
\ No newline at end of file
class ApplicationController < ActionController::Base
before_action :configure_devise_permitted_parameters, if: :devise_controller?
protect_from_forgery with: :exception
protected
def configure_devise_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :birthday, :gender])
end
end
\ No newline at end of file
class PagesController < ApplicationController
def index
@votes = Vote.all
@candidates = Candidate.all
@positions = Position.all
render "pages/index.html.erb"
end
end
\ No newline at end of file
module Users
class ProfileController < ApplicationController
before_action :authenticate_user!
def index
@positions = Position.all
@candidates = Candidate.all
@votes = Vote.all
render "pages/profile.html.erb"
end
end
end
module Voting
class VotesController < ApplicationController
before_action :authenticate_user!
def index
@positions = Position.all
@candidates = Candidate.all
@votes = Vote.all
render "voting/votes/index.html.erb"
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(post_params())
redirect_to admin_post_path(@post.id)
else
render "admin/posts/edit.html.erb"
end
end
def new
voted_for = false
@pos = params[:id]
Position.find(@pos).candidates.each do |c|
c.votes.each do |v|
if current_user.id == v.user_id
voted_for = true
end
end
end
if voted_for == false
@votes= Vote.all
@vote = Vote.new
render "voting/votes/new.html.erb"
else
redirect_to voting_votes_path
end
end
def create
@vote = current_user.votes.build(vote_params())
if @vote.save
redirect_to voting_votes_path(@vote.id)
else
render "voting/votes/new.html.erb"
end
end
def vote_params
params.require(:vote).permit!
end
end
end
\ No newline at end of file
class Candidate < ApplicationRecord
belongs_to :position
has_many :votes, dependent: :destroy
accepts_nested_attributes_for :votes
validates :last_name, presence: true
validates :first_name, presence: true
def full_name
"#{ self.first_name } #{ self.last_name }"
end
#def male_votes
# votes.select { |vote| vote.user.gender == 'male' }
#end
#def female_votes
# votes.select { |vote| vote.user.gender == 'female' }
#end
end
class Position < ApplicationRecord
has_many :candidates, dependent: :destroy
accepts_nested_attributes_for :candidates
validates :name, presence: true, uniqueness: {case_sensitive: false}
end
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
has_many :votes, dependent: :destroy
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
#def did_vote?(candidate)
# similar_votes = votes.select { |vote| vote.candidate == candidate }
# similar_votes.size > 0
#end
#has_many :candidates
def full_name
"#{self.first_name} #{self.last_name}"
end
end
class Vote < ApplicationRecord
belongs_to :user
belongs_to :candidate
end
<%= simple_form_for([:admin, @candidate]) do |f| %>
<%= f.input :first_name, label: 'First Name: '%>
<%= f.input :last_name, label: 'Last Name: '%>
<%= f.input :slogan, label: 'Slogan: '%>
<%= f.association :position, label: 'Position: '%>
<%= f.submit %>
<% end %>
<h1>Admin Edit Post</h1>
<%= render partial: "form" %>
<%= link_to "Back to Candidates", admin_candidates_path %>
\ No newline at end of file
<h1>Candidates</h1>
<%=link_to "Add a Candidate", new_admin_candidate_path%>
<hr>
<% @positions.each do |p| %>
<h5><%= p.name %></h5>
<ul >
<% p.candidates.each do |c|%>
<li><%=link_to c.full_name, admin_candidate_path(c.id)%></li>
<% end %>
</ul>
<% end %>
<h1>New Candidate</h1>
<%= render partial: "form" %>
<h1>Name of the Candidate: <%= @candidate.full_name%></h1>
<%=link_to "Edit", edit_admin_candidate_path(@candidate.id)%>
<br>
<%=link_to "Delete", admin_candidate_path(@candidate.id), method: :delete%>
<br>
<h3>Position: <%= @candidate.position.name %></h3>
<%
m=0
f=0
o=0
%>
<%
m=0
f=0
o=0
@candidate.votes.each do |v|
if v.user != nil
if v.user.gender=="Male"
m=m+1
else
if v.user.gender=="Female"
f=f+1
else
o=o+1
end
end
end
%>
<%end%>
<hr>
<h3>Slogan: <%= @candidate.slogan%></h3>
<hr>
<table width="100%">
<tr>
<th>Male Voters</th>
<th>Female Voters</th>
<th>Other Voters</th>
<th>Total Active Votes</th>
<th>Total Active and Inactive Casted Votes</th>
</tr>
<tr>
<td><%= m%></td>
<td><%= f%></td>
<td><%= o%></td>
<td><%= m+f+o%></td>
<td><%= @candidate.votes.count %></td>
</tr>
</table>
<hr>
<h3>Voters who voted for <%= @candidate.full_name%></h3>
<ul>
<li>
<% @candidate.votes.each do |v|%>
<%="#{v.user.full_name}"%>
<p>
Comments: <%=v.comment%>
</p>
</li>
<%end%>
</ul>
<hr>
<%= link_to "Back to Candidates", admin_candidates_path %>
\ No newline at end of file
<%= simple_form_for([:admin, @position]) do |f| %>
<%= f.input :name %>
<%= f.submit %>
<%= f.input :name, label: 'Position Name: ' %>
<%= f.submit %>
<% end %>
<h1>Admin Edit Post</h1>
<%= render partial: "form" %>
<%= link_to "Back to Candidates", admin_candidates_path %>
\ No newline at end of file
<h1>Positions</h1>
<%= link_to("Add New Position", new_position_path) %>
<%=link_to "Add New Position", new_admin_position_path%>
<hr>
<% @positions.each do |c| %>
<h2>(<%= c.id %>) <%= c.name %></h2>
<ul>
<% c.candidates.each do |p| %>
<li><%= link_to "#{p.first_name} #{p.last_name}" , candidate_path(p.id) %></li>
<li><%= link_to "#{p.first_name} #{p.last_name}" , admin_candidate_path(p.id) %></li>
<% end %>
</ul>
<%= link_to "View all Candidates under #{c.name}" , "/positions/#{c.id}" %>
<% end %>
<hr>
<%= link_to "View Candidates", candidates_path %>
<br>
<%= link_to "Home", root_path %>
<% end %>
\ No newline at end of file
<h1>New Position</h1>
<%= render partial: "form" %>
<h1><%= @position.name%></h1>
<%=link_to "Edit", edit_admin_position_path(@position.id)%>
<%=link_to "Delete", admin_position_path(@position.id), method: :delete%>
<br>
<h5>Candidates for <%=@position.name%></h5>
<ul>
<%@position.candidates.each do |c|%>
<li><%=link_to c.full_name, admin_candidate_path(c.id)%></li>
<%end%>
</ul>
<%= link_to "<Back to Positions", admin_positions_path %>
\ No newline at end of file
......@@ -7,7 +7,7 @@
<%= f.input :first_name, required: true, autofocus: true %>
<%= f.input :last_name, required: true %>
<%= f.input :birthday, :end_year => Date.today.year - 18, :start_year => 1920, :order => [ :day, :month, :year], required: true %>
<%= f.input :gender, as: :select, collection: ["Male", "Female"], required: true %>
<%= f.input :gender, as: :select, collection: ["Male", "Female", "Other"], required: true %>
<%= f.input :email, required: true %>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
......
......@@ -9,7 +9,7 @@
<%=
f.input :birthday, :start_year => Date.today.year - 18, :end_year => 1920, :order => [ :day, :month, :year], :required => true
%>
<%= f.input :gender, as: :select, collection: ["Male", "Female"], required: true %>
<%= f.input :gender, as: :select, collection: ["Male", "Female", "Other"], required: true %>
<%= f.input :email, required: true %>
<%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %>
<%= f.input :password_confirmation, required: true %>
......
<nav class="navbar navbar-default" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" ><b>HalalanPH</b></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="active"><%= link_to "Home", root_path %></li>
<li><%= link_to "Candidates", admin_candidates_path %></li>
<li><%= link_to "Positions", admin_positions_path %></li>
<li><a href="/voting/votes">Vote now</li>
<%if user_signed_in?%>
<li><%= link_to "Logout", destroy_user_session_path, method: :delete %></li>
<li> Hello, <%= current_user.first_name%>!</li>
<li> <a href="/profile">My Profile</a> </li>
<%else%>
<li><%= link_to "Log-in", new_user_session_path %></li>
<li><%= link_to "Register", new_user_registration_path %></li>
<%end%>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>HalalanPH</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= render '/layouts/navbar' %>
<div class="main-page" style="margin-top:40px;">
<%= yield %>
</div>
</body>
</html>
<h1>HalalanPH</h1>
<%@positions.each do |p|%>
<h4><%=p.name%></h4>
<table>
<thead>
<td>&nbsp;</td>
<td>Name</td>
<td>Slogan</td>
<td>Votes</td>
<td>&nbsp;</td>
</thead>
<%total = 0%>
<%p.candidates.each do |c|%>
<%total = total+c.votes.count%>
<%end%>
<%p.candidates.each do |c|%>
<tr>
<td></td>
<td><%=link_to c.full_name, admin_candidate_path(c.id)%></td>
<td><%=c.slogan%></td>
<td><%=c.votes.count%></td>
<td><progress value="<%=c.votes.count%>" max="<%=total%>" ></td>
</tr>
<%end%>
</table>
<%end%>
\ No newline at end of file
<h2>Hey <%=current_user.first_name%>!</h2>
<br>
You Voted for:
<br>
<ul>
<%current_user.votes.each do |v|%>
<li>
<%="#{v.candidate.position.name}: #{v.candidate.full_name}"%>
<p>
Comments: <%=v.comment%>
</p>
</li>
<%end%>
</ul>
\ No newline at end of file
<%
if @pos == nil
@pos = 0..99
else
end
%>
<%= simple_form_for([:voting, @vote]) do |f| %>
I cast my vote for
<%= f.association :candidate, collection: Candidate.where(position_id:@pos), label_method: :full_name, value_method: :id %>
<%= f.input :comment, label: 'Comment:'%>
<%= f.submit %>
<% end %>
<h1>Admin Edit Post</h1>
<%= render partial: "form" %>
<%= link_to "Back to Posts", admin_posts_path %>
\ No newline at end of file
<h1>Votes</h1>
<h2>Hello, <%= current_user.full_name %>!</h2>
<hr>
<h4>Vote now for:</h4>
<ul>
<%@positions.each do |p|%>
<li><%=p.name%><br>
<%voted_for = false%>
<% p.candidates.each do |c|%>
<%c.votes.each do |v|%>
<%if v.user_id == current_user.id%>
You have already voted for <%= v.candidate.full_name%>
<%voted_for = true%>
<% end %>
<%end%>
<% end %>
</ol>
<%if !voted_for%>
<%=link_to "Vote for a #{p.name}", "/voting/new/#{p.id}" %>
<%else%>
<%end%>
</li>
<%end%>
</ul>
<hr>
<br>
\ No newline at end of file
<h1>Vote</h1>
<%= render partial: "form" %>
<%= link_to "Back to Votes", voting_votes_path %>
<h1><%= @post.title %></h1>
<h3><%= @post.published_at %></h3>
<p>
<%= @post.content %>
</p>
<hr>
<%= link_to "Back to Posts", admin_posts_path %>
......@@ -6,7 +6,7 @@ require 'rails/all'
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Poller
module Halalan
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
......
......@@ -54,7 +54,7 @@ Rails.application.configure do
# Use a real queuing backend for Active Job (and separate queues per environment)
# config.active_job.queue_adapter = :resque
# config.active_job.queue_name_prefix = "Poller_#{Rails.env}"
# config.active_job.queue_name_prefix = "Halalan_#{Rails.env}"
config.action_mailer.perform_caching = false
# Ignore bad email addresses and do not raise email delivery errors.
......
......@@ -6,7 +6,7 @@ Devise.setup do |config|
# confirmation, reset password and unlock tokens in the database.
# Devise will use the `secret_key_base` as its `secret_key`
# by default. You can change it below and use your own secret key.
# config.secret_key = 'e005fd0494fcf6701b788857241622a3c0e503f706817ed0a7ef2b45966116ae166f0fe5bacee63c6f490fd3b804a33c5eb0ecb9d05a28cd76ced85c31b9a291'
# config.secret_key = '4d0ed2f5edaa904c424f15378a80db013fed17b738793d39b916f45b0fe7406b5f5c6b05ca04db20816944fe9762525faa1223e7c3bf783e4e9de757044d062b'
# ==> Mailer Configuration
# Configure the e-mail address which will be shown in Devise::Mailer,
......@@ -108,7 +108,7 @@ Devise.setup do |config|
config.stretches = Rails.env.test? ? 1 : 11
# Set up a pepper to generate the hashed password.
# config.pepper = '16d2b0c61cc20e454a605e101525687309d237b866e673e50d5037b78bccbff7bfaeb923b0b06712b759e1a4e388f103e1056fc3bd2acec5a54571516438584f'
# config.pepper = 'b3168207f12ca87dddd5f19c5de9a58c6b3ae027e7edd3f08eed4943585f9c363fd74d10688f05e3c17171439ccf2436a358b6b056c13cadd472bba56107790a'
# Send a notification email when the user's password is changed
# config.send_password_change_notification = false
......
# Be sure to restart your server when you modify this file.
Rails.application.config.session_store :cookie_store, key: '_Poller_session'
Rails.application.config.session_store :cookie_store, key: '_Halalan_session'
Rails.application.routes.draw do
root 'pages#index'
devise_for :users
resources :users
get "/profile", to: "users/profile#index"
get "/voting/new/:id", to: "voting/votes#new"
namespace :voting do
resources :votes
end
namespace :admin do
resources :candidates
resources :positions
end
end
......@@ -11,10 +11,10 @@
# if you're sharing your code publicly.
development:
secret_key_base: e325ec50f59f5d0702dc39ab4f84569661831a0173cf6ddc9287e79fd2f8552611c657200e3b8f6d775ea4ce0f781ca8b8786a3e2a14a7d593b939426508752c
secret_key_base: a120a184be8ba8273b612eff989817990858da2d03aa1067d95b199fa1fcd284a96896274307d0b1f17ef5570ce8a18de50bacc4d80f06dd77f995db00215189
test:
secret_key_base: c8100265b6849617b6fe673730e6a36d11bc6a6a517ec05e88767f37a6b6bac844e4e8aa81a916e27f9c17d92153a38822b2455960c1fbf3feb3c2cafdb5f5e2
secret_key_base: bcac3744318e5dbcb62954cfa6096a1d8bc67aacebe21683b14533d122575f050e6085f11cc445572f27af3dfda29a9b2063a4367df82ed72847e5dbd6205bf0
# Do not keep production secrets in the repository,
# instead read values from the environment.
......
......@@ -4,7 +4,7 @@ class CreateCandidates < ActiveRecord::Migration[5.0]
t.string :first_name
t.string :last_name
t.text :slogan
t.integer :position_id
t.references :position, foreign_key: true
t.timestamps
end
......
class CreateVotes < ActiveRecord::Migration[5.0]
def change
create_table :votes do |t|
t.integer :user_id
t.integer :candidate_id
t.text :comments
t.references :user, foreign_key: true
t.references :candidate, foreign_key: true
t.text :comment
t.timestamps
end
......
class AddFieldsToUsers < ActiveRecord::Migration[5.0]
class AddFieldsToUser < ActiveRecord::Migration[5.0]
def change
add_column :users, :first_name, :string
add_column :users, :last_name, :string
......
class EditFieldToUsers < ActiveRecord::Migration[5.0]
class EditFieldsToUser < ActiveRecord::Migration[5.0]
def change
remove_column :users, :birthdate
add_column :users, :birthday, :date
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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