Adding resource category edit abilities

This commit is contained in:
Will Bradley 2014-02-14 00:02:11 -07:00
parent 3f5a7012bd
commit 3d97e92eb7
19 changed files with 173 additions and 37 deletions

View File

@ -0,0 +1,46 @@
class ResourceCategoriesController < ApplicationController
load_and_authorize_resource
layout 'resources'
def create
authorize! :create, @resource_category
respond_to do |format|
if @resource_category.save
format.html { redirect_to resource_categories_path, :notice => "Category was successfully created." }
format.json { head :no_content }
else
format.html { render :action => "new" }
format.json { render :json => @resource_category.errors, :status => :unprocessable_entity }
end
end
end
def update
authorize! :update, @resource_category
respond_to do |format|
if @resource_category.update_attributes(params[:resource_category])
format.html { redirect_to resource_categories_path, :notice => "Category was successfully updated." }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => @resource_category.errors, :status => :unprocessable_entity }
end
end
end
def destroy
respond_to do |format|
if @resource_category.destroy
format.html { redirect_to resource_categories_path, :notice => "Category was deleted." }
format.json { head :ok }
else
format.html { redirect_to resource_categories_path, :notice => "Category could not be deleted. #{@resource_category.errors.full_messages.first}." }
format.json { render :json => @resource_category.errors, :status => :unprocessable_entity }
end
end
end
end

View File

@ -1,6 +1,7 @@
class ResourcesController < ApplicationController class ResourcesController < ApplicationController
load_and_authorize_resource load_and_authorize_resource
before_filter :load_users before_filter :load_users
layout 'resources'
def index def index
@featured_resource = @resources.where("picture_file_name IS NOT NULL").sample @featured_resource = @resources.where("picture_file_name IS NOT NULL").sample
@ -58,4 +59,5 @@ class ResourcesController < ApplicationController
@users = [current_user] @users = [current_user]
end end
end end
end end

View File

@ -5,6 +5,7 @@ class Ability
can :read, Mac # Anonymous can read mac can :read, Mac # Anonymous can read mac
can :scan, Mac # Need anonymous so CRON can scan can :scan, Mac # Need anonymous so CRON can scan
can :read, Resource can :read, Resource
can :read, ResourceCategory
if !user.nil? if !user.nil?
@ -36,7 +37,8 @@ class Ability
unless user.orientation.blank? unless user.orientation.blank?
can [:read,:new_member_report,:activity], User, :hidden => [nil,false] can [:read,:new_member_report,:activity], User, :hidden => [nil,false]
can :read, UserCertification can :read, UserCertification
can [:create,:update,:destroy], Resource, :user_id => [nil,user.id] can [:create,:update], Resource, :user_id => [nil,user.id]
can [:create,:update,:destroy], ResourceCategory
end end
# Accountants can manage payments # Accountants can manage payments

View File

@ -1,9 +1,9 @@
class Resource < ActiveRecord::Base class Resource < ActiveRecord::Base
attr_accessible :supercategory, :user_id, :category_id, :name, :serial, :specs, :status, :donatable, :picture, :picture_file_name, :picture_content_type, :picture_file_size, :picture_updated_at, :notes, :estimated_value, :disposed_at, :modified_by attr_accessible :supercategory, :user_id, :resource_category_id, :name, :serial, :specs, :status, :donatable, :picture, :picture_file_name, :picture_content_type, :picture_file_size, :picture_updated_at, :notes, :estimated_value, :disposed_at, :modified_by
belongs_to :owner, :class_name => "ToolshareUser" #TODO: remove owner belongs_to :owner, :class_name => "ToolshareUser" #TODO: remove owner
belongs_to :user belongs_to :user
belongs_to :category, :class_name => "ResourceCategory" belongs_to :resource_category
has_attached_file :picture, #TODO: move to local storage has_attached_file :picture, #TODO: move to local storage
:styles => { :medium => "300x300>", :styles => { :medium => "300x300>",
:thumb => "100x100>", :thumb => "100x100>",
@ -13,7 +13,7 @@ class Resource < ActiveRecord::Base
:path => ":attachment/:id/:style.:extension", :path => ":attachment/:id/:style.:extension",
:bucket => 'Toolshare' :bucket => 'Toolshare'
def category_name def resource_category_name
self.category.name if self.category resource_category.name if resource_category
end end
end end

View File

@ -1,3 +1,13 @@
class ResourceCategory < ActiveRecord::Base class ResourceCategory < ActiveRecord::Base
has_many :resources has_many :resources
attr_accessible :name
before_destroy :has_resources?
private
def has_resources?
errors.add(:base, "Cannot delete category with associated resources") unless resources.count == 0
errors.blank? #return false, to not destroy the element, otherwise, it will delete.
end
end end

View File

@ -16,6 +16,7 @@
<div id="header"> <div id="header">
<a id="home_nav_link" href="/" title="Home"><img src="/assets/logo.png" id="logo" /></a> <a id="home_nav_link" href="/" title="Home"><img src="/assets/logo.png" id="logo" /></a>
<%= link_to 'People', users_path if can? :read, User %> <%= link_to 'People', users_path if can? :read, User %>
<%= link_to 'Tools & Resources', resources_path if can? :read, Resource %>
<%= link_to 'Access Cards', cards_path if can? :manage, Card %> <%= link_to 'Access Cards', cards_path if can? :manage, Card %>
<% if can? :create, UserCertification %> <% if can? :create, UserCertification %>
<%= link_to 'Cert Classes', certifications_path if can? :read, Certification %> <%= link_to 'Cert Classes', certifications_path if can? :read, Certification %>

View File

@ -10,6 +10,23 @@
<link href="/bootstrap/css/bootstrap-theme.min.css" type="text/css" rel="stylesheet" /> <link href="/bootstrap/css/bootstrap-theme.min.css" type="text/css" rel="stylesheet" />
<script src="/bootstrap/js/bootstrap.min.js" type="text/javascript" ></script> <script src="/bootstrap/js/bootstrap.min.js" type="text/javascript" ></script>
<style>
.thumbnail { height: 150px; position: relative; }
.thumbnail .ownership,
.thumbnail .disposed
{ position: absolute; top: 1px;
text-shadow:
-1px -1px 0 #fff,
1px -1px 0 #fff,
-1px 1px 0 #fff,
1px 1px 0 #fff;
font-weight: bold; left: 10px; }
.thumbnail .disposed { color: red; }
.thumbnail h4 { position: absolute; bottom: 1px; margin-bottom: 1px; }
</style>
<style> <style>
#logo { #logo {
height: 40px; height: 40px;

View File

@ -0,0 +1,22 @@
<%= form_for @resource_category, :html => { :multipart => true } do |f| %>
<% if @resource_category.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@resource_category.errors.count, "error") %> prohibited this resource from being saved:</h2>
<ul>
<% @resource_category.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.submit nil, class: "btn btn-primary" %>
</p>
<% end %>

View File

@ -0,0 +1,10 @@
<li class="list-group-item row">
<h4 class="col-xs-6">
<%= resource_category.name %>
<small>(<%= resource_category.resources.count %> associated)</small>
</h4>
<span class="col-xs-6 text-right">
<%= link_to "Edit", [:edit, resource_category], class: "btn btn-primary" if can? :edit, resource_category %>
<%= link_to 'Delete', resource_category, {:confirm => 'Are you sure you want to delete this forever?', :method => :delete, :class => "btn btn-danger"} if can? :destroy, resource_category %>
</span>
</li>

View File

@ -0,0 +1,4 @@
<h1>Edit Category
<%= link_to 'Back', resource_categories_path, class: "btn btn-default" %>
</h1>
<%= render 'form' %>

View File

@ -0,0 +1,33 @@
<link href="/bootstrap/css/bootstrap-theme.min.css" type="text/css" />
<link href="/bootstrap/css/bootstrap.min.css" type="text/css" />
<script src="/bootstrap/js/bootstrap.min.js" type="text/javascript" ></script>
<style>
.thumbnail { height: 150px; position: relative; }
.thumbnail .ownership,
.thumbnail .disposed
{ position: absolute; top: 1px;
text-shadow:
-1px -1px 0 #fff,
1px -1px 0 #fff,
-1px 1px 0 #fff,
1px 1px 0 #fff;
font-weight: bold; left: 10px; }
.thumbnail .disposed { color: red; }
.thumbnail h4 { position: absolute; bottom: 1px; margin-bottom: 1px; }
</style>
<h1 class="col-sm-9">Resource Categories
<%= link_to 'Back', resources_path, :class => "btn btn-default" if can? :read, Resource %>
<%= link_to 'Add Category', new_resource_category_path, :class => "btn btn-success" if can? :create, ResourceCategory %>
</h1>
<div class="col-sm-12">
<ul class="list-group">
<%= render @resource_categories.sort_by(&:name) %>
</ul>
</div>
<br />

View File

@ -0,0 +1,4 @@
<h1>New Category
<%= link_to 'Back', resource_categories_path, class: "btn btn-default" %>
</h1>
<%= render 'form' %>

View File

@ -12,8 +12,8 @@
<% end %> <% end %>
<p> <p>
<%= f.label :category_id %><br /> <%= f.label :resource_category_id, "Category" %><br />
<%= select(:resource, :category_id, options_from_collection_for_select(ResourceCategory.all.sort_by(&:name), :id, :name, @resource.category_id ) ) %> <%= select(:resource, :resource_category_id, options_from_collection_for_select(ResourceCategory.all.sort_by(&:name), :id, :name, @resource.resource_category_id ) ) %>
</p> </p>
<p> <p>
<%= f.label :name %><br /> <%= f.label :name %><br />

View File

@ -1,27 +1,6 @@
<link href="/bootstrap/css/bootstrap-theme.min.css" type="text/css" />
<link href="/bootstrap/css/bootstrap.min.css" type="text/css" />
<script src="/bootstrap/js/bootstrap.min.js" type="text/javascript" ></script>
<style>
.thumbnail { height: 150px; position: relative; }
.thumbnail .ownership,
.thumbnail .disposed
{ position: absolute; top: 1px;
text-shadow:
-1px -1px 0 #fff,
1px -1px 0 #fff,
-1px 1px 0 #fff,
1px 1px 0 #fff;
font-weight: bold; left: 10px; }
.thumbnail .disposed { color: red; }
.thumbnail h4 { position: absolute; bottom: 1px; margin-bottom: 1px; }
</style>
<h1 class="col-sm-4">Lab Resources <h1 class="col-sm-4">Lab Resources
<%= link_to 'Add Resource', new_resource_path, :class => "btn btn-success" if can? :create, Resource %> <%= link_to 'Add Resource', new_resource_path, :class => "btn btn-success" if can? :create, Resource %>
<%= link_to 'Categories', resource_categories_path, :class => "btn btn-primary" if can? :read, ResourceCategory %>
</h1> </h1>
<h3 class="col-sm-2">Featured item:</h3> <h3 class="col-sm-2">Featured item:</h3>
@ -30,14 +9,14 @@
<div class="row"> <div class="row">
<% unless @resources.blank? %> <% unless @resources.blank? %>
<div class="panel-group col-sm-8" id="accordion"> <div class="panel-group col-sm-8" id="accordion">
<% @resources.sort_by{|r| [r.category_name,r.name] }.group_by(&:category).each do |category,resources| %> <% @resources.sort_by{|r| [r.resource_category_name,r.name] }.group_by(&:resource_category).each do |resource_category,resources| %>
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading" data-toggle="collapse" data-parent="#accordion" href="#collapse_cat_<%= category.id %>"> <div class="panel-heading" data-toggle="collapse" data-parent="#accordion" href="#collapse_cat_<%= resource_category.id %>">
<h4 class="panel-title"> <h4 class="panel-title">
<%= category.name %> <%= resource_category.name %>
</h4> </h4>
</div> </div>
<div id="collapse_cat_<%= category.id %>" class="panel-collapse collapse"> <div id="collapse_cat_<%= resource_category.id %>" class="panel-collapse collapse">
<div class="panel-body"> <div class="panel-body">
<%= render resources %> <%= render resources %>
</div> </div>

View File

@ -12,7 +12,7 @@
<% end %> <% end %>
<p> <p>
<b>Category:</b> <b>Category:</b>
<%=h @resource.category.name if !@resource.category.blank? %> <%=h @resource.resource_category.name if @resource.resource_category %>
</p> </p>
<p> <p>

View File

@ -53,7 +53,7 @@
<% if !@users.blank? %> <% if !@users.blank? %>
<% @users.each do |user| %> <% @users.each do |user| %>
<tr<%= " class='hidden'" if user.hidden? %>> <tr<%= ' class=hidden' if user.hidden? %>>
<td><%= image_tag user.gravatar_url(:default => "http://members.heatsynclabs.org/assets/nil.png"), :class => :avatar %></td> <td><%= image_tag user.gravatar_url(:default => "http://members.heatsynclabs.org/assets/nil.png"), :class => :avatar %></td>
<td><%= link_to user.name, user %></td> <td><%= link_to user.name, user %></td>
<td><%= raw(user.member_status_symbol) %></td> <td><%= raw(user.member_status_symbol) %></td>

View File

@ -9,6 +9,7 @@ Dooraccess::Application.routes.draw do
resources :payments resources :payments
resources :resources resources :resources
resources :resource_categories, except: :show
match 'statistics' => 'statistics#index', :as => :statistics match 'statistics' => 'statistics#index', :as => :statistics
match 'statistics/mac_log' => 'statistics#mac_log', :as => :mac_statistics match 'statistics/mac_log' => 'statistics#mac_log', :as => :mac_statistics

View File

@ -0,0 +1,5 @@
class RenameResourceCategoryIdToResourceResourceCategoryId < ActiveRecord::Migration
def change
rename_column :resources, :category_id, :resource_category_id
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20140209120648) do ActiveRecord::Schema.define(:version => 20140214055051) do
create_table "cards", :force => true do |t| create_table "cards", :force => true do |t|
t.string "card_number" t.string "card_number"
@ -126,7 +126,7 @@ ActiveRecord::Schema.define(:version => 20140209120648) do
create_table "resources", :force => true do |t| create_table "resources", :force => true do |t|
t.string "supercategory" t.string "supercategory"
t.integer "owner_id" t.integer "owner_id"
t.integer "category_id" t.integer "resource_category_id"
t.string "name" t.string "name"
t.string "serial" t.string "serial"
t.string "specs" t.string "specs"