Skip to content
Snippets Groups Projects
Commit 2232d073 authored by Tyler Lemburg's avatar Tyler Lemburg
Browse files

Admin REsources

parent 8e80dec5
No related branches found
No related tags found
No related merge requests found
......@@ -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
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
<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
<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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment