Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipients controller & spec #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions app/controllers/locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ def sellers
index(Location::LOCATION_TYPES.invert["Seller"],"Sellers")
end

def recipients
index(Location::LOCATION_TYPES.invert["Recipient"],"Recipients")
end

def index(location_type=nil,header="Locations")
unless location_type.nil?
@locations = Location.regional(current_volunteer.region_ids).where("location_type = ?",location_type)
Expand Down
38 changes: 38 additions & 0 deletions app/controllers/region_admin/recipients_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module RegionAdmin
class RecipientsController < ApplicationController
before_filter :authenticate_volunteer!
before_filter :authorize_region_admin!

def index
@locations = regional_recipients
@regions = available_regions
end

private

def regional_recipients
if current_volunteer.super_admin?
Location.active.recipients
else
Location.active.recipients.regional(available_regions.map(&:id))
end
end

def available_regions
if current_volunteer.super_admin?
Region.all
else
current_volunteer.assignments.collect{ |a| a.admin ? a.region : nil }.compact
end
end

def authorize_region_admin!
return if region_admin?
redirect_to root_url, alert: "Unauthorized"
end

def region_admin?
current_volunteer.any_admin?
end
end
end
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@

<%= drop_down "Region Admin" do %>
<%= menu_item "Donors", region_admin_donors_url %>
<%= menu_item "Recipients", recipients_locations_path %>
<%= menu_item "Recipients", region_admin_recipients_url %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the change from _path to _url here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The real change is adding the region_admin prefix. I switched the _path to _url as a matter of preference

<% if current_volunteer.main_region.has_sellers? %>
<%= menu_item "Sellers", sellers_locations_path %>
<% end %>
Expand Down
52 changes: 52 additions & 0 deletions app/views/region_admin/recipients/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<h2>Recipients</h2>

<div style="text-align: right;">
<%= form_tag("/locations/new", :method => "get") do %>
New Recipient Location For <%= select_tag(:region_id, options_for_select(@regions.collect{ |r| [r.name,r.id] })) %>
<%= submit_tag("Go") %>
<% end %>
</div>

<% if @locations.empty? %>
<p>No locations.</p>
<% else %>
<table id="data_table" class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>Link</th>
</tr>
</thead>

<tbody>
<% @locations.each do |location| %>
<tr>
<td>
<%= link_to location.name.presence || "Unnamed location", location_url(location) %> <%= link_to "(website)", location.website if location.website.present? %>
</td>
<td>
<%= location.address.gsub("\n","<br>").html_safe unless location.address.nil? %>
</td>
<td>
<% if location.receipt_key.present? %>
<%= link_to 'Hud', "/locations/hud?id=#{location.id}&key=#{location.receipt_key}" %><br>
<% end %>
<%= link_to "Edit", edit_location_url(location) %><br>
<%= link_to "Delete", location_url(location), :confirm => "Are you sure?", :method => :delete %>
</td>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this view is missing a few table columns that were originally shown when this list was rendered with locations/index.html.erb. Was this intentional?

https://github.com/boulder-food-rescue/food-rescue-robot/blob/rf/recipients-controller-spec/app/views/locations/index.html.erb#L40-L49

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was intentional - there were too many fields in a row. All the data is visible on the #show page.

</tr>
<% end %>
</tbody>
</table>
<% end %>

<% content_for :scripts do %>
<script type="text/javascript">
$(function () {
$('#data_table').dataTable({
'iDisplayLength' : 50,
});
});
</script>
<% end %>
5 changes: 2 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@

resources :locations do
collection do
get :recipients
get :hubs
get :sellers
get :buyers
Expand Down Expand Up @@ -122,7 +121,8 @@
resource :waiver, only: [:new, :create]

namespace :region_admin do
resources :donors, only: [:index]
resources :donors, only: [:index]
resources :recipients, only: [:index]
end

devise_scope :volunteer do
Expand All @@ -134,5 +134,4 @@
root to: 'sessions#new', as: :unauthenticated_root
end
end

end
73 changes: 73 additions & 0 deletions spec/features/region_admin/recipients_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require 'rails_helper'

RSpec.describe "Region Admin Recipients" do
feature "Viewing the list of recipients" do
let(:boulder) { create(:region) }
let(:denver) { create(:region) }

let!(:boulder_recipient) { create(:recipient, region: boulder, name: "Boulder recipient site") }
let!(:denver_recipient) { create(:recipient, region: denver, name: "Denver recipient site") }

context "as a region admin" do
let(:volunteer) { create(:volunteer, regions: [], assigned: true) }

before do
create(:assignment, :admin, volunteer: volunteer, region: boulder)
create(:assignment, volunteer: volunteer, region: denver)

login volunteer
end

it "can see the list of recipients in my administrated regions" do
visit "/region_admin/recipients"

expect(page).to have_content("Boulder recipient site")
end

it "cannot see recipients from unadministered regions" do
visit "/region_admin/recipients"

expect(page).to_not have_content("Denver recipient site")
end
end

context "as a super admin" do
let(:volunteer) { create(:volunteer, regions: [], assigned: true, admin: true) }

before do
create(:assignment, volunteer: volunteer, region: boulder)

login volunteer
end

it "can see the list of recipients in all regions" do
visit "/region_admin/recipients"

expect(page).to have_content("Boulder recipient site")
expect(page).to have_content("Denver recipient site")
end
end

context "as a visitor" do
it "redirects to sign in" do
visit "/region_admin/recipients"

expect(page.current_path).to eq("/volunteers/sign_in")
end
end

context "as a volunteer" do
let(:volunteer) { create(:volunteer, regions: [boulder], assigned: true) }

before do
login volunteer
end

it "redirects to home" do
visit "/region_admin/recipients"

expect(page.current_path).to eq("/")
end
end
end
end