Skip to content

Commit

Permalink
Merge pull request #29 from FireflyArtsCollective/retire-admins-voters
Browse files Browse the repository at this point in the history
Retire `/admins/voters`, use `/voters`
  • Loading branch information
Katee authored Apr 5, 2017
2 parents 6bd9ace + 55295f1 commit 31ff10f
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 76 deletions.
25 changes: 0 additions & 25 deletions app/controllers/admins_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,6 @@ def create
render 'new'
end

# TODO: endpoint does not belong here
def verify
@voter = Voter.find(params[:id])
authorize! :verify, @voter

@voter.verified = params[:verify]

if @voter.save
send_email = params[:send_email] == "true"
if send_email
begin
UserMailer.voter_verified(@voter, event_year).deliver_now
logger.info "email: voter verification sent to #{@voter.email}"
rescue
flash[:warning] = "Error sending email"
redirect_to action: "voters"
return
end
flash[:info] = "Voter notified by email"
end
end

redirect_to action: "voters"
end

def send_fund_emails
submissions = GrantSubmission.where(id: params[:ids].split(','))
sent = 0
Expand Down
23 changes: 23 additions & 0 deletions app/controllers/voters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ def update
redirect_to after_update_path(@voter)
end

def verify
@send_email = params[:send_email] == 'true'
@voter.verified = params[:verify] != '0'

if @voter.save
flash[:info] = 'Voter verification status updated'

if @send_email && @voter.verified?
begin
UserMailer.voter_verified(@voter, event_year).deliver_now
flash[:info] = 'Voter verified and notified by email'
logger.info "email: voter verification sent to #{@voter.email}"
rescue
flash[:warning] = 'Error sending email'
redirect_to action: 'index'
return
end
end
end

redirect_to voters_path
end

private

def initialize_grants
Expand Down
4 changes: 2 additions & 2 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def initialize(user)
end

if user.is_a?(Voter)
can :manage, Voter, id: user.id
can [:show, :new, :create, :edit, :update], Voter, id: user.id # cannot index or destroy
can :manage, VoterSurvey, voter_id: user.id

cannot :index, [Voter, VoterSurvey]
cannot :index, VoterSurvey

if user.activated?
can :manage, Vote, voter_id: user.id
Expand Down
1 change: 1 addition & 0 deletions app/models/voter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Voter < ActiveRecord::Base
has_secure_password

has_many :grants_voters
has_many :grants, through: :grants_voters
has_many :votes
has_one :voter_survey, inverse_of: :voter
has_many :voter_submission_assignments
Expand Down
43 changes: 0 additions & 43 deletions app/views/admins/voters.html.erb

This file was deleted.

44 changes: 42 additions & 2 deletions app/views/voters/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,54 @@
<tr>
<th>Name</th>
<th>Email</th>
<th>Grants</th>
<% if can? :verify, Voter %>
<th class="text-center">Verified</th>
<% end %>
<th class="text-right">
<% if can? :verify, Voter %>
<form name="send_email_form">
<input type="checkbox" id="send_verify_email_checkbox" checked="true">
send email when verifying
</form>
<% end %>
</th>
</tr>
</thead>
<tbody>
<% @voters.each do |voter| %>
<tr>
<td><%= link_to voter.name, voter_path(voter) %></td>
<td><%= voter.name %></td>
<td><%= voter.email %></td>
<td><%= voter.grants.pluck(:name).join(", ") %></td>
<% if can? :verify, voter %>
<td class="text-center"><%if voter.verified? %>Yes<% else %>No<% end %></td>
<% end %>
<td class="text-right">
<div class="btn-group">
<% if can? :verify, Voter %>
<% if voter.verified? %>
<%= link_to "Unverify", verify_voter_path(voter, verify: 0), method: :post, class: 'btn btn-warning' %>
<% else %>
<%= link_to "Verify", verify_voter_path(voter, verify: 'true'), method: :post, class: 'btn btn-primary', onclick: "do_verify(event, #{voter.id});" %>
<% end %>
<% end %>
<% if can? :edit, voter %>
<%= link_to 'Edit', voter_path(voter), class: "btn btn-default" %>
<% end %>
</div>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<% if can? :verify, Voter %>
<script>
function do_verify(event, id) {
event.preventDefault();
var send_email = document.getElementById('send_verify_email_checkbox').checked;
$.post("/voters/"+ id +"/verify?send_email=" + send_email);
return false;
}
</script>
<% end %>
2 changes: 1 addition & 1 deletion app/views/voters/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<ol class="breadcrumb">
<li><%= link_to 'Voters', admins_voters_path %></li>
<li><%= link_to 'Voters', voters_path %></li>
<li class="active"><%= @voter.name %></li>
</ol>

Expand Down
7 changes: 5 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

post 'admins/assign' => 'admins#assign'
post 'admins/clear_assignments' => 'admins#clear_assignments'
post 'admins/verify' => 'admins#verify'
post 'admins/send_fund_emails' => 'admins#send_fund_emails'
post 'admins/send_question_emails' => 'admins#send_question_emails'

Expand All @@ -33,7 +32,11 @@

resources :proposals, only: [:destroy]

resources :voters, only: [:new, :create, :update, :index, :show]
resources :voters, only: [:new, :create, :update, :index, :show] do
member do
post 'verify'
end
end

resources :votes, only: [:index]
resource :votes, only: [:update]
Expand Down
56 changes: 56 additions & 0 deletions spec/controllers/voters_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,60 @@ def go!
end
end
end

describe '#verify' do
let!(:voter) { FactoryGirl.create(:voter) }

before { sign_in user }

def go!
post 'verify', id: voter.id
end

context 'when voter signed in' do
let!(:user) { voter }

it { go!; is_expected.to be_forbidden }
end

context 'when admin logged in' do
let!(:user) { FactoryGirl.create(:admin) }

it 'verifies voter' do
expect { go! }.to change { voter.reload.verified }.to true
end

context 'with send_email param' do
def go!
post 'verify', id: voter.id, send_email: 'true'
end

it 'verifies voter' do
expect { go! }.to change { voter.reload.verified }.to true
expect(flash[:info]).to be_present
end

it 'sends email' do
expect(UserMailer).to receive(:voter_verified)
go!
end
end

context 'with params verify: 0' do
def go!
post 'verify', id: voter.id, verify: '0', send_email: 'true'
end

it 'unverifies voter' do
expect { go! }.to change { voter.reload.verified }.to false
expect(flash[:info]).to be_present
end

it 'does not send email' do
go!
expect(UserMailer).not_to receive(:voter_verified)
end
end
end
end
end
7 changes: 6 additions & 1 deletion spec/models/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@

it { is_expected.to be_able_to(:vote, GrantSubmission.new) }

[:show, :new, :create, :edit, :update].each do |action|
it { is_expected.to be_able_to(action, user) }
end
it { is_expected.to be_able_to(:manage, vote) }
it { is_expected.to be_able_to(:manage, voter_survey) }
it { is_expected.to be_able_to(:read, voter_submission_assignment) }
Expand All @@ -132,7 +135,9 @@
context 'when not activated' do
let(:user) { FactoryGirl.create(:voter) }

it { is_expected.to be_able_to(:manage, user) }
[:show, :new, :create, :edit, :update].each do |action|
it { is_expected.to be_able_to(action, user) }
end
it { is_expected.to be_able_to(:manage, voter_survey) }

it { is_expected.not_to be_able_to(:vote, GrantSubmission.new) }
Expand Down

0 comments on commit 31ff10f

Please sign in to comment.