From 250714d249627909cf2055cb008a92f886ad2857 Mon Sep 17 00:00:00 2001 From: Ryan Foster Date: Wed, 4 Jan 2017 11:01:50 -0700 Subject: [PATCH 1/2] Move recipients index to separate controller --- app/controllers/locations_controller.rb | 4 -- .../region_admin/recipients_controller.rb | 38 ++++++++++++++ app/views/layouts/application.html.erb | 2 +- .../region_admin/recipients/index.html.erb | 52 +++++++++++++++++++ config/routes.rb | 5 +- 5 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 app/controllers/region_admin/recipients_controller.rb create mode 100644 app/views/region_admin/recipients/index.html.erb diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 85d0c885..cc4a22fe 100755 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -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) diff --git a/app/controllers/region_admin/recipients_controller.rb b/app/controllers/region_admin/recipients_controller.rb new file mode 100644 index 00000000..f41c5c55 --- /dev/null +++ b/app/controllers/region_admin/recipients_controller.rb @@ -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 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 76b316bb..be3fa2d2 100755 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -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 %> <% if current_volunteer.main_region.has_sellers? %> <%= menu_item "Sellers", sellers_locations_path %> <% end %> diff --git a/app/views/region_admin/recipients/index.html.erb b/app/views/region_admin/recipients/index.html.erb new file mode 100644 index 00000000..0a88d6bf --- /dev/null +++ b/app/views/region_admin/recipients/index.html.erb @@ -0,0 +1,52 @@ +

Recipients

+ +
+ <%= 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 %> +
+ +<% if @locations.empty? %> +

No locations.

+<% else %> + + + + + + + + + + + <% @locations.each do |location| %> + + + + + + <% end %> + +
NameAddressLink
+ <%= link_to location.name.presence || "Unnamed location", location_url(location) %> <%= link_to "(website)", location.website if location.website.present? %> + + <%= location.address.gsub("\n","
").html_safe unless location.address.nil? %> +
+ <% if location.receipt_key.present? %> + <%= link_to 'Hud', "/locations/hud?id=#{location.id}&key=#{location.receipt_key}" %>
+ <% end %> + <%= link_to "Edit", edit_location_url(location) %>
+ <%= link_to "Delete", location_url(location), :confirm => "Are you sure?", :method => :delete %> +
+<% end %> + +<% content_for :scripts do %> + +<% end %> diff --git a/config/routes.rb b/config/routes.rb index bc28957f..117df567 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -75,7 +75,6 @@ resources :locations do collection do - get :recipients get :hubs get :sellers get :buyers @@ -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 @@ -134,5 +134,4 @@ root to: 'sessions#new', as: :unauthenticated_root end end - end From ab7e1352585fdd6ca83ad59505b5ef62402ba54f Mon Sep 17 00:00:00 2001 From: Ryan Foster Date: Fri, 6 Jan 2017 13:09:16 -0700 Subject: [PATCH 2/2] Add integration spec for region admin recipients --- spec/features/region_admin/recipients_spec.rb | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 spec/features/region_admin/recipients_spec.rb diff --git a/spec/features/region_admin/recipients_spec.rb b/spec/features/region_admin/recipients_spec.rb new file mode 100644 index 00000000..e7c3bedb --- /dev/null +++ b/spec/features/region_admin/recipients_spec.rb @@ -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