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

Make admin controllers restful #836

Merged
merged 8 commits into from
Jan 26, 2021
Merged
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
47 changes: 0 additions & 47 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
@@ -1,57 +1,10 @@
# frozen_string_literal: true

class AdminController < ApplicationController
before_action :authenticate_user!
before_action :ensure_authorized_as_admin
before_action :set_system_settings, only: [:glossary_edit, :glossary_index, :glossary_update]

def landing_page
@submission_count = Submission.count
@match_count = Match.any? ? Match.pluck(:receiver_id).uniq.length : 1
@ask_count = Ask.any? ? Ask.all.length : 1
@offer_count = Offer.any? ? Offer.all.length : 1
@percent_offers_fulfilled = ((@match_count.to_f / @offer_count.to_f).to_f * 100).to_i
@percent_asks_fulfilled = ((@match_count.to_f / @ask_count.to_f).to_f * 100).to_i
@asks_unmatched_count = Ask.unmatched.length
@offers_unmatched_count = Offer.unmatched.length
@announcements_pending_count = Announcement.pending_review.length
@community_resources_pending_count = CommunityResource.pending_review.length
end

def dispatch_steps; end

def form_admin; end

# FIXME: extract glossary related code into their own controller
def glossary_edit
@system_settings = SystemSetting.current_settings
render 'admin/glossary/form'
end

def glossary_index
@glossary_content = @system_settings.glossary_content
render 'admin/glossary/index'
end

def glossary_update
@system_settings.update(glossary_content: glossary_params[:glossary_content])
redirect_to glossary_admin_path
end

def yearbook
@positions = Position.all
end

private

def glossary_params
params.require(:system_setting).permit(:glossary_content)
end

def set_system_settings
@system_settings = SystemSetting.current_settings
end

def ensure_authorized_as_admin
unless current_user.admin_role? || current_user.sys_admin_role?
fail Pundit::NotAuthorizedError, "Sorry, only admins are authorized to do that."
Expand Down
14 changes: 14 additions & 0 deletions app/controllers/admin_dashboard_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class AdminDashboardController < AdminController
def show
@submission_count = Submission.count
@match_count = Match.any? ? Match.pluck(:receiver_id).uniq.length : 1
@ask_count = Ask.any? ? Ask.all.length : 1
@offer_count = Offer.any? ? Offer.all.length : 1
@percent_offers_fulfilled = ((@match_count.to_f / @offer_count.to_f).to_f * 100).to_i
@percent_asks_fulfilled = ((@match_count.to_f / @ask_count.to_f).to_f * 100).to_i
@asks_unmatched_count = Ask.unmatched.length
@offers_unmatched_count = Offer.unmatched.length
@announcements_pending_count = Announcement.pending_review.length
@community_resources_pending_count = CommunityResource.pending_review.length
end
end
2 changes: 2 additions & 0 deletions app/controllers/dispatch_steps_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class DispatchStepsController < AdminController
end
26 changes: 24 additions & 2 deletions app/controllers/glossary_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
class GlossaryController < PublicController
include NotUsingPunditYet
class GlossaryController < ApplicationController
skip_before_action :authenticate_user!, only: [:show]
before_action :authorize_glossary
before_action :set_system_settings

def show; end
def edit; end

def update
@system_settings.update!(glossary_params)
redirect_to glossary_path
end

private

def authorize_glossary
authorize :glossary
end

def glossary_params
params.require(:system_setting).permit(:glossary_content)
end

def set_system_settings
@system_settings = SystemSetting.current_settings
end
end
2 changes: 1 addition & 1 deletion app/controllers/submission_response_imports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ def create
)

flash[:notice] = 'Your file has been uploaded and is being imported'
redirect_to landing_page_admin_path
redirect_to admin_dashboard_path
end
end
2 changes: 2 additions & 0 deletions app/controllers/volunteer_admin_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class VolunteerAdminController < AdminController
end
5 changes: 5 additions & 0 deletions app/controllers/yearbook_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class YearbookController < AdminController
def show
@positions = Position.all
end
end
9 changes: 9 additions & 0 deletions app/policies/glossary_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class GlossaryPolicy < ApplicationPolicy
def read?
true
end

def change?
acting_user && (acting_user.admin_role? || acting_user.sys_admin_role? )
end
end
11 changes: 0 additions & 11 deletions app/views/admin/form_admin.html.erb

This file was deleted.

4 changes: 0 additions & 4 deletions app/views/admin/glossary/index.html.erb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
<div class="column is-4">
<%= link_to "Glossary!", glossary_path %>
<br>
<%= link_to "Dispatch steps", dispatch_steps_admin_path %>
<%= link_to "Dispatch steps", dispatch_steps_path %>
<br>
<%= link_to "Yearbook", yearbook_admin_path %>
<%= link_to "Yearbook", yearbook_path %>
<br>
<%= link_to "Org chart", org_chart_path %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/announcements/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
</table>
</div>
<br><br>
<%= link_to "Admin home", landing_page_admin_path %>
<%= link_to "Admin home", admin_dashboard_path %>
2 changes: 1 addition & 1 deletion app/views/community_resources/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
</table>
</div>
<br><br>
<%= link_to "Admin home", landing_page_admin_path %>
<%= link_to "Admin home", admin_dashboard_path %>
2 changes: 1 addition & 1 deletion app/views/contributions/respond.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="title is-5">
Respond to Contribution: <%= link_to "go to Contributions page", contributions_path %>
<br>
<span class="subtitle is-7 is-italic"><%= link_to "(1. Triage (or log Communication), then 2. Add tentative Match)", dispatch_steps_admin_path %></span>
<span class="subtitle is-7 is-italic"><%= link_to "(1. Triage (or log Communication), then 2. Add tentative Match)", dispatch_steps_path %></span>
</div>

<br><br>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<%= render partial: 'admin/glossary/header' %>
<%= link_to "View Glossary", glossary_admin_path %>
<%= render partial: 'glossary/header' %>
<%= link_to "View Glossary", glossary_path %>
<hr>
<div class="glossary_form">
<%= form_with model: @system_settings, url: glossary_admin_edit_path, local: true do |f| %>
<%= form_with model: @system_settings, url: glossary_path, local: true do |f| %>
<div class="field">
<%= f.rich_text_area :glossary_content %>
</div>
Expand Down
99 changes: 6 additions & 93 deletions app/views/glossary/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,93 +1,6 @@
<div class="columns">
<div class="column is-12">
<div class="title">Glossary</div>
<hr>
</div>
</div>

<div class="row columns">
<div class="columns column is-12">
<div class="column is-12">
<%= link_to "Contributions", contributions_path %>: All public-generated content, like, Asks, Offers, Community Resources, Announcements

<hr>

<%= link_to "Community Resources", community_resources_path(is_approved: false) %>: Local offerings and organizations that are always available, e.g. Food Pantry, Diaper Bank. Can be links or videos
<br>
<%= link_to "Announcements", announcements_path(is_approved: false) %>: References and notes to share with the public about specific events and offerings, e.g. food drive Jun 12

<hr>


<%= link_to "Asks", listings_path(contribution_type: "Ask") %>: Request for support/exchange from the public
<br>
<%= link_to "Offers", listings_path(contribution_type: "Offer") %> Offer of support/exchange from the public
<br>
<%= link_to "Donations", donations_path %>: Log of Donations to the mutual aid group, but this form could connect to Stripe, etc, later

<hr>

<%= link_to "Locations", locations_path %>: Address-related info submitted by People or Organizations when they fill out Forms. Also used to store Service Area info
<br>
<%= link_to "Location Types", location_types_path %>: Kind of Location, so, home, business, cross street, etc
<br>
<%= link_to "Submissions", submissions_path %>: Form "responses" (This is a log of all results from all Forms: Ask, Offer, (CommunityResource, Annoucement, Volunteer), etc)
<br>
<%= link_to "Form Questions", custom_form_questions_path %>: Questions you want to be on your Forms
<br>
<%= link_to "Submission Responses", submission_responses_path %>: People's answers to specific Form Questions

<hr>


<%= link_to "Matches", matches_path %>: Connection between a receiver (Ask/Profile) and a provider (Offer/Community Resource/Profile) (Profile matches are TBD)
<br>
<%= link_to "Feedback", feedbacks_path %>: Each Match will generate an email request to receivers and providers to request Feedback about their experience. (We are considering showing stats on Profiles re thumbsup/starts/rating, TBD)
<br>
<%= link_to "Communication Log", communication_logs_path %>: Log of all phone calls, texts, etc, to People. Any system-generated emails will be saved as such.

<hr>

<%= link_to "People", people_path %>: All the human People in the system -- receiver/provider/leadership team member/partner organization point of contact/etc
<br>
<%= link_to "Volunteers", volunteer_admin_path %>: People who are part of the group (different than Providers who submit Offers)
<br>
<%= link_to "Positions", positions_path %>: Title/Role/Membership a Person has within an Organization, e.g. Leadership Team member/Dispatch Team member/Point of Contact

<hr>

<%= link_to "User accounts", users_path %>: For People who log in to this system. People will still have Profiles even if they don't have a User Account. (Still need to decide depending on login status.)
<br>
<%= link_to "Shared Accounts", shared_accounts_path %>: List of accounts the mutual aid group keeps, along w descriptions (but NOT passwords!), so there's one list of all the gmail addresses, payment gateways, etc. (Again, NOT the actual passwords!)
<br>
<%= link_to "Software Feedback", software_feedbacks_path %>: Place for Users (Admins only?) to store ideas/feedback for this system. (Y'all can decide which of these you want to request of coders...)
<br>

<%= link_to "Categories", categories_path %>: Services offered/facilitated by the mutual aid group. Can be nested. Categories will appear on Form checkboxes and dropdowns, and Contribution page filters
<br>
<%= link_to "Service Areas", service_areas_path %>: Geographic "areas" served by the mutual aid group. Can be nested. Can start at state, and drill down to Pods. Service Areas will appear on Form checkboxes and dropdowns, and Contribution page filters
<br>
<%= link_to "Contact Methods", contact_methods_path %>: Mechanism for contacting People, e.g. Text, Call, Email, WhatsApp. Contact Methods will appear on Form checkboxes and dropdowns, and on Contribution page filters

<hr>
<%= link_to "Translations", mobility_string_translations_path %>: Working up to multilanguage support
<br>
<%= link_to "System Locales", system_locales_path %>: Languages your group supports. We populated this list, but you can change it to whatever you want!

<hr>

<%= link_to "Yearbook", yearbook_admin_path %>: List of everyone who has a Position. (Later we can have Profile photos.)

<hr>

<%= link_to "Respond", respond_contribution_path(Listing.first) %>: Preview a Contribution and prepare to "Respond" to the person by adding a Tentative Match
<br>
<%= link_to "Triage", triage_contribution_path(Listing.first) %>: Update Contribution -- Add short summary, change description, indicate if 'Inexhaustible'
<br>
<span class="has-text-weight-bold">Tentative Match:</span> This is a real Match. The first point of contact is to choose a potential match, and then to contact both parties.
<br>
<span class="has-text-weight-bold">Inexhaustible:</span> This Contribution can be matched many times, will continue to show up in dropdowns, etc

</div>
</div>
</div>
<%= render partial: 'glossary/header' %>
<% if policy(:glossary).change? %>
<%= link_to "Edit Glossary", edit_glossary_path, class: "btn" %>
<% end %>
<hr>
<%= @system_settings.glossary_content %>
4 changes: 2 additions & 2 deletions app/views/layouts/_without_navbar.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if current_user %>
<div class="has-text-centered">
<%= link_to "Return to admin", landing_page_admin_path, class: "button is-primary is-outlined" %>
<%= link_to "Return to admin", admin_dashboard_path, class: "button is-primary is-outlined" %>
</div>
<% end %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/submissions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@
</table>
</div>
<br><br>
<%= link_to "Admin home", landing_page_admin_path %>
<%= link_to "Admin home", admin_dashboard_path %>
2 changes: 1 addition & 1 deletion app/views/system_settings/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@
<hr>

<%= link_to('Edit', edit_system_setting_path(@system_setting)) + " | " if current_user.sys_admin_role? %>
<%= link_to 'Admin', landing_page_admin_path %>
<%= link_to 'Admin', admin_dashboard_path %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<br>
<%= link_to "Add Shifts", shifts_path %>
<br>
<%= link_to "Review Dispatch steps", dispatch_steps_admin_path %>
<%= link_to "Review Dispatch steps", dispatch_steps_path %>
<br>
<%= link_to "View training status (NOT IMPLEMENTED)", people_path(position_type: "Volunteer") %>
<br>
<%= link_to "Filter by workload (NOT IMPLEMENTED)", people_path(position_type: "Volunteer") %>
<br>

<br>
<%= link_to "Admin home", landing_page_admin_path %>
<%= link_to "Admin home", admin_dashboard_path %>
File renamed without changes.
17 changes: 8 additions & 9 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
# FIXME: drop controller override when the pundit work is ready, #514
devise_for :users, controllers: { registrations: 'registrations_sans_signup' }

get '/admin', to: 'admin#landing_page', as: 'landing_page_admin'
get '/admin/forms', to: 'admin#form_admin', as: 'form_admin'
get '/admin/volunteers', to: 'admin#volunteer_admin', as: 'volunteer_admin'
get '/admin/dispatch', to: 'admin#dispatch_steps', as: 'dispatch_steps_admin'
get '/admin/glossary', to: 'admin#glossary_index', as: 'glossary_admin'
get '/admin/glossary_edit', to: 'admin#glossary_edit', as: 'glossary_admin_edit'
patch '/admin/glossary_edit', to: 'admin#glossary_update', as: 'glossary_admin_update'
get '/admin/yearbook', to: 'admin#yearbook', as: 'yearbook_admin'
scope '/admin' do
root to: 'admin_dashboard#show', as: 'admin_dashboard'

resource :volunteers, only: [:show], controller: :volunteer_admin, as: 'volunteer_admin'
resource :dispatch_steps, only: [:show]
resource :yearbook, only: [:show], controller: :yearbook
end

get '/public', to: 'public_pages#landing_page', as: 'landing_page_public'
get '/about', to: 'public_pages#about', as: 'about_public'
Expand Down Expand Up @@ -77,7 +76,7 @@
resources :teams
resources :users

resource :glossary, controller: :glossary, only: [:show]
resource :glossary, controller: :glossary, only: [:show, :edit, :update]

root :to => 'public_pages#landing_page'
end
29 changes: 29 additions & 0 deletions spec/policies/glossary_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'rails_helper'

RSpec.describe GlossaryPolicy do
subject { GlossaryPolicy.new(user, :glossary) }

context "user can edit glossary if they are" do
context "an admin" do
let(:user) { build(:user, :admin) }
it { is_expected.to permit_action(:update) }
end

context "a sys_admin" do
let(:user) { build(:user, :sys_admin) }
it { is_expected.to permit_action(:update) }
end
end

context "user cannot edit glossary if they are" do
context "a neighbor" do
let(:user) { build(:user, :neighbor) }
it { is_expected.not_to permit_action(:update) }
end

context "not logged in" do
let(:user) { nil }
it { is_expected.not_to permit_action(:update) }
end
end
end
Loading