wip: vote

This commit is contained in:
Kiara Grouwstra 2023-01-23 22:30:26 +01:00
parent 2082cace93
commit 16d2ec77ed
9 changed files with 121 additions and 3 deletions

View File

@ -32,6 +32,11 @@ class MainController < ApplicationController
end
end
def stemmen
@votes = Vote.where(room_id: @room.id, user_id: @user.id)
render :votes
end
private
def set_user_room
@user = User.find_by token: params[:token]

View File

@ -0,0 +1,48 @@
class UsersController < ApplicationController
http_basic_authenticate_with name: Rails.application.config.admin_name,
password: Rails.application.config.admin_password
before_action :set_room, only: [:bulk]
# GET /rooms/:room_id/users/bulk
def bulk
@sample = "voter_email,short_name,voter_login_id,voter_password\ninfo@bij1.org,mijn-stemming,1,abcdABCD1234\n"
end
# POST /rooms/:room_id/users/bulk
# POST /rooms/:room_id/users/bulk.json
def create_bulk
room_id = params[:room_id]
votes_csv = params[:votes_csv]
require 'csv'
CSV.parse(votes_csv, :headers => %i[voter_email short_name voter_login_id voter_password]) do |row|
csv_fields = row.to_hash
email = csv_fields[:voter_email]
user = User.find_by(room_id: room_id, email: email)
vote_fields = {
:room_id => room_id,
:user_id => user.id,
:election_slug => csv_fields[:short_name],
:voter_login_id => csv_fields[:voter_login_id],
:voter_password => csv_fields[:voter_password],
}
Vote.create!(vote_fields)
end
respond_to do |format|
format.html { redirect_to room_users_url(room_id), notice: 'Votes were successfully created.' }
format.json { render :show, status: :created, location: @room }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_room
@room = Room.find(params[:room_id])
end
end

3
app/models/vote.rb Normal file
View File

@ -0,0 +1,3 @@
class Vote < ApplicationRecord
belongs_to :user
end

View File

@ -0,0 +1,18 @@
<p id="notice"><%= notice %></p>
<h1>Stemmingen</h1>
<ul>
<% @votes.each do |vote| %>
<li>
<a href="https://stemmen.bij1.org/helios/e/<%= vote.short_name %>/vote?voter_id=<%= vote.voter_login_id %>&password=<%= vote.voter_password %>">
<%= vote.short_name %>
</a>
</li>
<% end %>
</url>
<br>
<%= link_to 'Stream', user_stream_path %> |
<%= link_to 'Inbellen', join_room_path %> via BigBlueButton (voor toelichtingen)

View File

@ -50,4 +50,5 @@
<%= link_to 'Present Users', room_present_users_path(id: params[:room_id], v: 1) %> |
<%= link_to 'Absent Users', room_present_users_path(id: params[:room_id], v: 0) %> |
<%= link_to 'Destroy all', destroy_room_users_path(params[:room_id]), method: :delete %> |
<%= link_to 'Import Votes', bulk_new_room_votes_path(params[:room_id]), method: :get %> |
<%= link_to 'Back', room_path(params[:room_id]) %>

View File

@ -1,6 +1,8 @@
Rails.application.routes.draw do
resources :rooms do
resources :users, shallow: true
resources :rooms, shallow: true do
resources :users do
resources :votes
end
end
get 'rooms/:room_id/users/bulk', to: 'users#bulk', as: 'bulk_new_room_users'
post 'rooms/:room_id/users/bulk', to: 'users#create_bulk', as: 'bulk_create_room_users'
@ -10,9 +12,12 @@ Rails.application.routes.draw do
delete 'rooms/:room_id/users/invite', to: 'users#uninvite', as: 'uninvite_room_users'
post 'rooms/:room_id/users/mark_invited', to: 'users#mark_invited', as: 'mark_invited_room_users'
post 'rooms/:room_id/users/mark_presence', to: 'users#mark_presence', as: 'mark_presence_room_users'
get 'rooms/:room_id/votes/bulk', to: 'votes#bulk', as: 'bulk_new_room_votes'
post 'rooms/:room_id/votes/bulk', to: 'votes#create_bulk', as: 'bulk_create_room_votes'
get 'rooms/:id/voters.csv', to: 'rooms#voters', as: 'room_export_voters'
get 'rooms/:id/aanwezig.csv', to: 'rooms#present', as: 'room_present_users'
get ':token/stream', to: 'main#stream'
get ':token/stemmen', to: 'main#stemmen', as: 'user_elections'
get ':token/stream', to: 'main#stream', as: 'user_stream'
get ':token', to: 'main#join', as: 'join_room'
root 'main#index'
end

View File

@ -0,0 +1,14 @@
class CreateVotes < ActiveRecord::Migration[6.0]
def change
create_table :votes do |t|
t.primary_key :id
t.references :room, null: false, foreign_key: true
t.references :user, null: false, foreign_key: true
t.string :election_slug
t.string :voter_login_id
t.string :voter_password
t.timestamps
end
end
end

17
test/fixtures/votes.yml vendored Normal file
View File

@ -0,0 +1,17 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
id:
room_id: 1
user_id: 1
election_slug: MyString
voter_login_id: MyString
voter_password: MyString
two:
id:
room_id: 1
user_id: 1
election_slug: MyString
voter_login_id: MyString
voter_password: MyString

7
test/models/vote_test.rb Normal file
View File

@ -0,0 +1,7 @@
require 'test_helper'
class VoteTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end