From 3d97e92eb7dd034bba078dc96035fb1dee016910 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Fri, 14 Feb 2014 00:02:11 -0700 Subject: [PATCH] Adding resource category edit abilities --- .../resource_categories_controller.rb | 46 +++++++++++++++++++ app/controllers/resources_controller.rb | 2 + app/models/ability.rb | 4 +- app/models/resource.rb | 8 ++-- app/models/resource_category.rb | 10 ++++ app/views/layouts/application.html.erb | 1 + app/views/layouts/resources.html.erb | 17 +++++++ app/views/resource_categories/_form.html.erb | 22 +++++++++ .../_resource_category.html.erb | 10 ++++ app/views/resource_categories/edit.html.erb | 4 ++ app/views/resource_categories/index.html.erb | 33 +++++++++++++ app/views/resource_categories/new.html.erb | 4 ++ app/views/resources/_form.html.erb | 4 +- app/views/resources/index.html.erb | 31 ++----------- app/views/resources/show.html.erb | 2 +- app/views/users/index.html.erb | 2 +- config/routes.rb | 1 + ...ory_id_to_resource_resource_category_id.rb | 5 ++ db/schema.rb | 4 +- 19 files changed, 173 insertions(+), 37 deletions(-) create mode 100755 app/controllers/resource_categories_controller.rb create mode 100644 app/views/resource_categories/_form.html.erb create mode 100644 app/views/resource_categories/_resource_category.html.erb create mode 100644 app/views/resource_categories/edit.html.erb create mode 100755 app/views/resource_categories/index.html.erb create mode 100644 app/views/resource_categories/new.html.erb create mode 100644 db/migrate/20140214055051_rename_resource_category_id_to_resource_resource_category_id.rb diff --git a/app/controllers/resource_categories_controller.rb b/app/controllers/resource_categories_controller.rb new file mode 100755 index 0000000..e1c26fb --- /dev/null +++ b/app/controllers/resource_categories_controller.rb @@ -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 diff --git a/app/controllers/resources_controller.rb b/app/controllers/resources_controller.rb index f111c45..0bbfa4f 100755 --- a/app/controllers/resources_controller.rb +++ b/app/controllers/resources_controller.rb @@ -1,6 +1,7 @@ class ResourcesController < ApplicationController load_and_authorize_resource before_filter :load_users + layout 'resources' def index @featured_resource = @resources.where("picture_file_name IS NOT NULL").sample @@ -58,4 +59,5 @@ class ResourcesController < ApplicationController @users = [current_user] end end + end diff --git a/app/models/ability.rb b/app/models/ability.rb index e2efe81..ef0f185 100755 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -5,6 +5,7 @@ class Ability can :read, Mac # Anonymous can read mac can :scan, Mac # Need anonymous so CRON can scan can :read, Resource + can :read, ResourceCategory if !user.nil? @@ -36,7 +37,8 @@ class Ability unless user.orientation.blank? can [:read,:new_member_report,:activity], User, :hidden => [nil,false] 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 # Accountants can manage payments diff --git a/app/models/resource.rb b/app/models/resource.rb index f10b457..d829a25 100755 --- a/app/models/resource.rb +++ b/app/models/resource.rb @@ -1,9 +1,9 @@ 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 :user - belongs_to :category, :class_name => "ResourceCategory" + belongs_to :resource_category has_attached_file :picture, #TODO: move to local storage :styles => { :medium => "300x300>", :thumb => "100x100>", @@ -13,7 +13,7 @@ class Resource < ActiveRecord::Base :path => ":attachment/:id/:style.:extension", :bucket => 'Toolshare' - def category_name - self.category.name if self.category + def resource_category_name + resource_category.name if resource_category end end diff --git a/app/models/resource_category.rb b/app/models/resource_category.rb index afc9435..7457bb1 100755 --- a/app/models/resource_category.rb +++ b/app/models/resource_category.rb @@ -1,3 +1,13 @@ class ResourceCategory < ActiveRecord::Base 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 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index f62fec5..d030f79 100755 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -16,6 +16,7 @@