From 2232d073c4ca9df5b42f4de7127e995049e2c300 Mon Sep 17 00:00:00 2001 From: Tyler Lemburg <trlemburg@gmail.com> Date: Fri, 17 Jun 2016 13:46:57 -0500 Subject: [PATCH] Admin REsources --- models/service_space.rb | 4 ++ routes/admin/resources.rb | 90 +++++++++++++++++++++++++++++++++++ views/admin/edit_resource.erb | 26 ++++++++++ views/admin/resources.erb | 41 ++++++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 routes/admin/resources.rb create mode 100644 views/admin/edit_resource.erb create mode 100644 views/admin/resources.erb diff --git a/models/service_space.rb b/models/service_space.rb index c3ccd2c..23ee9d2 100644 --- a/models/service_space.rb +++ b/models/service_space.rb @@ -22,4 +22,8 @@ class ServiceSpace < ActiveRecord::Base def admin_events_href "/#{self.url_name}/admin/events/" end + + def admin_resources_href + "/#{self.url_name}/admin/resources/" + end end \ No newline at end of file diff --git a/routes/admin/resources.rb b/routes/admin/resources.rb new file mode 100644 index 0000000..1c09841 --- /dev/null +++ b/routes/admin/resources.rb @@ -0,0 +1,90 @@ +require 'models/resource' +require 'models/permission' + +before '/:service_space_url_name/admin/resources*' do + unless @user.has_permission?(Permission::MANAGE_RESOURCES, @space) + raise Sinatra::NotFound + end +end + +get '/:service_space_url_name/admin/resources/?' do + @breadcrumbs << {:text => 'Admin Resources'} + + resources = Resource.where(:service_space_id => @space.id).order(:name).all + erb :'admin/resources', :layout => :fixed, :locals => { + :resources => resources + } +end + +get '/:service_space_url_name/admin/resources/create/?' do + @breadcrumbs << {:text => 'Admin Resources', :href => '/admin/resources/'} << {:text => 'Create Resource'} + + erb :'admin/edit_resource', :layout => :fixed, :locals => { + :resource => Resource.new + } +end + +post '/:service_space_url_name/admin/resources/create/?' do + resource = Resource.new + resource.name = params[:name] + resource.model = params[:model] + resource.description = params[:description] + resource.service_space_id = @space.id + resource.needs_authorization = true + resource.is_reservable = params.checked?('is_reservable') + resource.minutes_per_reservation = params[:minutes_per_reservation] + resource.needs_approval = false + resource.max_reservations_per_slot = 5 + resource.save + + flash(:success, 'Resource Created', "Your resource #{resource.name} has been created.") + redirect @space.admin_resources_href +end + +get '/:service_space_url_name/admin/resources/:resource_id/edit/?' do + @breadcrumbs << {:text => 'Admin Resources', :href => '/admin/resources/'} << {:text => 'Edit Resource'} + + # check that this is a valid resource + resource = Resource.find_by(:id => params[:resource_id], :service_space_id => @space.id) + if resource.nil? + flash(:alert, 'Not Found', 'That resource does not exist.') + redirect @space.admin_resources_href + end + + erb :'admin/edit_resource', :layout => :fixed, :locals => { + :resource => resource + } +end + +post '/:service_space_url_name/admin/resources/:resource_id/edit/?' do + # check that this is a valid resource + resource = Resource.find_by(:id => params[:resource_id], :service_space_id => @space.id) + if resource.nil? + flash(:alert, 'Not Found', 'That resource does not exist.') + redirect @space.admin_resources_href + end + + resource.name = params[:name] + resource.model = params[:model] + resource.description = params[:description] + resource.is_reservable = params.checked?('is_reservable') + resource.minutes_per_reservation = params[:minutes_per_reservation] + resource.save + + flash(:success, 'Resource Updated', "Your resource #{resource.name} has been updated.") + redirect @space.admin_resources_href +end + +post '/:service_space_url_name/admin/resources/:resource_id/delete/?' do + # check that this is a valid resource + resource = Resource.find_by(:id => params[:resource_id], :service_space_id => @space.id) + if resource.nil? + flash(:alert, 'Not Found', 'That resource does not exist.') + redirect @space.admin_resources_href + end + + resource.destroy + + flash(:success, 'Resource Deleted', "Your resource #{resource.name} has been deleted. All reservations and permissions on this resource have also been removed.") + redirect @space.admin_resources_href +end \ No newline at end of file diff --git a/views/admin/edit_resource.erb b/views/admin/edit_resource.erb new file mode 100644 index 0000000..720c247 --- /dev/null +++ b/views/admin/edit_resource.erb @@ -0,0 +1,26 @@ +<div id="pagetitle"> +<h3><%= resource.id.nil? ? 'Create Resource' : "Edit #{resource.name}" %></h3> +</div> + +<form action="" method="POST"> + <label for="name">Name</label> + <input type="text" name="name" id="name" value='<%= resource.name %>'/> + + <label for="model">Model</label> + <input type="text" name="model" id="model" value='<%= resource.model %>'/> + + <label for="description">Description</label> + <textarea id="description" name="description"><%= resource.description %></textarea> + + <div> + <input type="checkbox" <%= 'checked="checked"' if resource.is_reservable || resource.id.nil? %> name='is_reservable' id="is-reservable"> + <label for="is-reservable">Requires Reservation?</label> + </div> + <div> + <label for="minutes-per-reservation">Minutes Per Reservation</label> + <input type="number" name="minutes_per_reservation" id="minutes-per-reservation" value="<%= resource.minutes_per_reservation %>"> + </div> + <br> + + <button type="submit" class="wdn-button wdn-button-brand">Save Resource</button> +</form> \ No newline at end of file diff --git a/views/admin/resources.erb b/views/admin/resources.erb new file mode 100644 index 0000000..d3c9a67 --- /dev/null +++ b/views/admin/resources.erb @@ -0,0 +1,41 @@ +<div id="pagetitle"> +<h3>All Resources</h3> +</div> + +<a class="wdn-button wdn-button-brand" href="/<%= @space.url_name %>/admin/resources/create/">Create Resource</a><br><br> + +<table> + <thead> + <tr> + <th>Resource</th> + <th>Actions</th> + </tr> + </thead> + <tbody> + <% resources.each do |resource| %> + <tr> + <td> + <%= resource.name %> + </td> + <td class="table-actions"> + <a href="/<%= @space.url_name %>/admin/resources/<%= resource.id %>/edit/" class="wdn-button wdn-button-brand">Edit</a> + <form method="POST" action="/<%= @space.url_name %>/admin/resources/<%= resource.id %>/delete/" class="delete-form delete-resource"> + <button class="wdn-button" type="submit">Delete</button> + </form> + </td> + </tr> + <% end %> + </tbody> +</table> + +<script type="text/javascript"> +require(['jquery'], function($) { + $(document).ready(function() { + $('.delete-resource').submit(function (submit) { + if (!window.confirm('Are you sure you want to delete this resource?')) { + submit.preventDefault(); + } + }); + }); +}); +</script> \ No newline at end of file -- GitLab