Merge pull request #1261 from shlok007/resources

Ability to track resources
This commit is contained in:
Ana María Martínez Gómez 2017-02-09 10:45:51 +01:00 committed by GitHub
commit f42e4adc45
17 changed files with 265 additions and 2 deletions

View file

@ -0,0 +1,52 @@
module Admin
class ResourcesController < ApplicationController
load_and_authorize_resource :conference, find_by: :short_title
load_and_authorize_resource :resource, only: [:show, :edit, :update, :destroy]
def index; end
def edit; end
def new
@resource = @conference.resources.new
end
def create
@resource = @conference.resources.new(resource_params)
if @resource.save
redirect_to admin_conference_resources_path(conference_id: @conference.short_title),
notice: 'Resource successfully created.'
else
flash[:error] = "Creating resource failed: #{@resource.errors.full_messages.join('. ')}."
render :new
end
end
def update
if @resource.update_attributes(resource_params)
redirect_to admin_conference_resources_path(conference_id: @conference.short_title),
notice: 'Resource successfully updated.'
else
flash[:error] = "Resource update failed: #{@resource.errors.full_messages.join('. ')}."
render :edit
end
end
def destroy
if @resource.destroy
redirect_to admin_conference_resources_path(conference_id: @conference.short_title),
notice: 'Resource successfully destroyed.'
else
redirect_to admin_conference_resources_path(conference_id: @conference.short_title),
error: 'Resource was successfully destroyed.' \
"#{@resource.errors.full_messages.join('. ')}."
end
end
private
def resource_params
params.require(:resource).permit(:name, :description, :quantity, :used, :conference_id)
end
end
end

View file

@ -536,4 +536,8 @@ module ApplicationHelper
end
end
end
def quantity_left_of(resource)
"#{resource.quantity - resource.used}/#{resource.quantity}"
end
end

View file

@ -134,6 +134,7 @@ class Ability
# ids of all the conferences for which the user has the 'organizer' role
conf_ids_for_organizer = Conference.with_role(:organizer, user).pluck(:id)
can :manage, Resource, conference_id: conf_ids_for_organizer
can [:new, :create], Conference if user.has_role?(:organizer, :any)
can :manage, Conference, id: conf_ids_for_organizer
can :manage, Splashpage, conference_id: conf_ids_for_organizer
@ -187,6 +188,7 @@ class Ability
# ids of all the conferences for which the user has the 'cfp' role
conf_ids_for_cfp = Conference.with_role(:cfp, user).pluck(:id)
can [:index, :show, :update], Resource, conference_id: conf_ids_for_cfp
can :manage, Event, program: { conference_id: conf_ids_for_cfp }
can :manage, EventType, program: { conference_id: conf_ids_for_cfp }
can :manage, Track, program: { conference_id: conf_ids_for_cfp }
@ -223,6 +225,7 @@ class Ability
# ids of all the conferences for which the user has the 'info_desk' role
conf_ids_for_info_desk = Conference.with_role(:info_desk, user).pluck(:id)
can [:index, :show, :update], Resource, conference_id: conf_ids_for_info_desk
can :manage, Registration, conference_id: conf_ids_for_info_desk
can :manage, Question, conference_id: conf_ids_for_info_desk
can :manage, Question do |question|
@ -244,6 +247,7 @@ class Ability
# ids of all the conferences for which the user has the 'volunteers_coordinator' role
conf_ids_for_volunteers_coordinator = Conference.with_role(:volunteers_coordinator, user).pluck(:id)
can [:index, :show, :update], Resource, conference_id: conf_ids_for_volunteers_coordinator
can :manage, Vposition, conference_id: conf_ids_for_volunteers_coordinator
can :manage, Vday, conference_id: conf_ids_for_volunteers_coordinator

View file

@ -24,6 +24,7 @@ class Conference < ActiveRecord::Base
has_many :payments, dependent: :destroy
has_many :supporters, through: :ticket_purchases, source: :user
has_many :tickets, dependent: :destroy
has_many :resources, dependent: :destroy
has_many :lodgings, dependent: :destroy
has_many :registrations, dependent: :destroy

10
app/models/resource.rb Normal file
View file

@ -0,0 +1,10 @@
class Resource < ActiveRecord::Base
belongs_to :conference
validate :used_less_than_quantity
private
def used_less_than_quantity
errors.add(:used, 'can not be higher than total quantity') unless used <= quantity
end
end

View file

@ -0,0 +1,16 @@
.row
.col-md-12
.page-header
%h1
- if @resource.new_record?
New
Resource
.row
.col-md-8
= semantic_form_for(@resource, :url => (@resource.new_record? ? admin_conference_resources_path : admin_conference_resource_path(@conference.short_title, @resource))) do |f|
= f.input :name
= f.input :description, input_html: { rows: 5, data: { provide: "markdown-editable" } }
= f.input :used
= f.input :quantity
%p.text-right
= f.action :submit, as: :button, button_html: { class: 'btn btn-primary' }

View file

@ -0,0 +1,36 @@
.row
.col-md-12
.page-header
%h1 Resources
%p.text-muted
Manage resources in conference
- if @conference.resources.any?
.row
.col-md-12
%table.table.table-hover.datatable
%thead
%th Name
%th Left( Left/Total )
%th Used
%th Actions
%tbody
- @conference.resources.each do |resource|
%tr
%td
= link_to(admin_conference_resource_path(@conference.short_title, resource.id)) do
= resource.name
%td
= quantity_left_of(resource)
%td
= resource.used
%td
.btn-group
= link_to 'Edit', edit_admin_conference_resource_path(@conference.short_title, resource.id),
method: :get, class: 'btn btn-primary', disabled: !(can? :update, resource)
= link_to 'Delete', admin_conference_resource_path(@conference.short_title, resource.id),
method: :delete, class: 'btn btn-danger', disabled: !(can? :destroy, resource),
data: { confirm: "Do you really want to delete #{resource.name}?" }
.row
.col-md-12
= link_to 'Add Resource', new_admin_conference_resource_path, class: 'btn btn-success pull-right', disabled: !(can? :create, Resource.new(conference_id: @conference_id))

View file

@ -0,0 +1 @@
= render 'form'

View file

@ -0,0 +1,26 @@
.row
.col-md-12
%h3
= @resource.name
.btn-group.pull-right
= link_to 'Edit', edit_admin_conference_resource_path(@conference.short_title, @resource), class: 'btn btn-mini btn-primary'
%br
.row
.col-md-12
%table.table
%tr
%td.col-md-2
%b Description
%td
= @resource.description
%tr
%td
%b Total Quantity
%td
= @resource.quantity
%tr
%td
%b Quantity left
%td
= quantity_left_of(@resource)

View file

@ -139,3 +139,8 @@
= link_to(admin_conference_roles_path(@conference.short_title)) do
%span.fa.fa-group
Roles
- if can? :index, @conference.resources.new
%li
= link_to admin_conference_resources_path(@conference.short_title) do
%span.fa.fa-pencil-square
Resources