Skip to content
This repository has been archived by the owner on Aug 12, 2021. It is now read-only.

Commit

Permalink
Add minimal volunteer tracking (resolves #29)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobwhitelock committed Nov 16, 2019
1 parent d7eca06 commit b4f0b1b
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 6 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ gem 'jbuilder', '~> 2.5'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
gem "bulma-rails", "~> 0.7.4"
gem 'lodash-rails'
gem 'postcodes_io'
gem 'jquery-rails'
gem 'xkpassword'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ GEM
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
ruby_dep (~> 1.2)
lodash-rails (4.17.14)
railties (>= 3.1)
loofah (2.3.1)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand Down Expand Up @@ -273,6 +275,7 @@ DEPENDENCIES
jbuilder (~> 2.5)
jquery-rails
listen (>= 3.0.5, < 3.2)
lodash-rails
pg
postcodes_io
puma (~> 3.11)
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require lodash
//= require_tree .
//= require cookies_eu
47 changes: 47 additions & 0 deletions app/assets/javascripts/volunteer_control_panel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const debouncedUpdaters = {};

function increaseVolunteersControl(formIdentifier, updateUrl) {
volunteersControl(formIdentifier, updateUrl, function(value) {
return value + 1;
});
}

function decreaseVolunteersControl(formIdentifier, updateUrl) {
volunteersControl(formIdentifier, updateUrl, function(value) {
return value - 1;
});
}

function volunteersControl(formIdentifier, updateUrl, updateCallback) {
const valueElement = elementFor(formIdentifier, 'value');
const newValue = updateCallback(+valueElement.innerHTML);

if (newValue >= 0) {
valueElement.innerHTML = newValue;

let updateServer = debouncedUpdaters[formIdentifier];
if (!updateServer) {
updateServer = _.debounce(function() {
const savingElement = elementFor(formIdentifier, 'saving');
savingElement.classList.remove('is-invisible');

const data = {count: valueElement.innerHTML};
function onSuccess() {
savingElement.classList.add('is-invisible')
}

// XXX Add better error handling
$.post(updateUrl, data, onSuccess);
}, 500);

debouncedUpdaters[formIdentifier] = updateServer;
}

updateServer();
}
}

function elementFor(formIdentifier, type) {
const controlValueId = `${formIdentifier}-${type}`;
return document.getElementById(controlValueId);
}
20 changes: 20 additions & 0 deletions app/controllers/committee_rooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,28 @@ def create
redirect_to new_work_space_committee_room_path(work_space)
end

def canvassers
volunteers_observation_action(CanvassersObservation)
end

def cars
volunteers_observation_action(CarsObservation)
end

private

def volunteers_observation_action(observation_class)
committee_room = CommitteeRoom.find(params[:committee_room_id])
# XXX More ad-hoc authorization, should improve.
return if committee_room.work_space != find_work_space

observation_class.create!(
committee_room: committee_room,
count: params[:count],
user: @current_user
)
end

def committee_room_params
params.require(:committee_room).permit(:address, :organiser_name)
end
Expand Down
14 changes: 14 additions & 0 deletions app/models/committee_room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,18 @@ class CommitteeRoom < ApplicationRecord

validates_presence_of :address
validates_presence_of :organiser_name

def last_canvassers_observation
last_observation_for(canvassers_observations)
end

def last_cars_observation
last_observation_for(cars_observations)
end

private

def last_observation_for(observations)
observations.max_by(&:created_at) || UnobservedCommitteeRoomObservation.new
end
end
7 changes: 7 additions & 0 deletions app/models/unobserved_committee_room_observation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

# Null object for CommitteeRoomObservations.
class UnobservedCommitteeRoomObservation
def count
0
end
end
19 changes: 17 additions & 2 deletions app/views/work_spaces/_committee_room_info.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,23 @@
<%= committee_room.organiser_name %>
</p>

<%= render 'volunteer_control_panel', text: 'Canvassers: 30' %>
<%= render 'volunteer_control_panel', text: 'Cars: 2' %>
<%= render 'volunteer_control_panel',
identifier: "canvassers-#{committee_room.id}",
text: 'Canvassers',
count: committee_room.last_canvassers_observation.count,
update_count_url: work_space_committee_room_canvassers_path(
committee_room.work_space, committee_room
)
%>
<%= render 'volunteer_control_panel',
identifier: "cars-#{committee_room.id}",
text: 'Cars',
count: committee_room.last_cars_observation.count,
update_count_url: work_space_committee_room_cars_path(
committee_room.work_space, committee_room
)
%>
<% else %>

<p class="is-size-6 is-italic">
Expand Down
13 changes: 10 additions & 3 deletions app/views/work_spaces/_volunteer_control_panel.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<div class="field has-addons is-marginless">
<p class="control">
<button class="button">
<button class="button" onclick="decreaseVolunteersControl('<%= identifier %>', '<%= update_count_url %>')">
<span class="icon">
<i class="fa fa-minus"></i>
</span>
Expand All @@ -10,12 +10,19 @@

<p class="control">
<div class="fake-button is-fullwidth">
<span><%= text %></span>
<span id="<%= identifier %>-saving" class="is-invisible">
<span class="fa fa-spinner fa-spin"></span>
</span>
&nbsp;
<span><%= text %>:
<%# XXX Make count also editable directly, if needed %>
<span id="<%= identifier %>-value"><%= count %></span>
</span>
</div>
</p>

<p class="control">
<button class="button">
<button class="button" onclick="increaseVolunteersControl('<%= identifier %>', '<%= update_count_url %>')">
<span class="icon">
<i class="fa fa-plus"></i>
</span>
Expand Down
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
end
end

resources :committee_rooms, path: 'committee-room', except: :index
resources :committee_rooms, path: 'committee-room', except: :index do
post :canvassers
post :cars
end
end

resources :users, only: :update
Expand Down

0 comments on commit b4f0b1b

Please sign in to comment.