Add increment and decrement functionality to left resource

This commit is contained in:
Chaitanya 2017-04-05 01:36:22 +05:30
parent 033dbe4c40
commit 4c60baf7c1
8 changed files with 74 additions and 12 deletions

View file

@ -57,3 +57,5 @@ $(document).ready(function() {
delegateSelector: 'a.smoothscroll' delegateSelector: 'a.smoothscroll'
}); });
}); });
UnobtrusiveFlash.flashOptions['timeout'] = 3000;

View file

@ -94,3 +94,8 @@ p.comment-body {
.box{ .box{
height: 230px; height: 230px;
} }
.quantity_left{
height: 3em;
width: 9em;
}

View file

@ -2,6 +2,7 @@ module Admin
class ResourcesController < Admin::BaseController class ResourcesController < Admin::BaseController
load_and_authorize_resource :conference, find_by: :short_title load_and_authorize_resource :conference, find_by: :short_title
load_and_authorize_resource :resource, only: [:show, :edit, :update, :destroy] load_and_authorize_resource :resource, only: [:show, :edit, :update, :destroy]
after_action :prepare_unobtrusive_flash, only: [:update]
def index; end def index; end
@ -23,12 +24,23 @@ module Admin
end end
def update def update
if @resource.update_attributes(resource_params) successful_update = check_successful_update params[:increment_used_resource_flag]
respond_to do |format|
if successful_update
format.html do
redirect_to admin_conference_resources_path(conference_id: @conference.short_title), redirect_to admin_conference_resources_path(conference_id: @conference.short_title),
notice: 'Resource successfully updated.' notice: 'Resource successfully updated.'
end
flash.now[:notice] = if params[:increment_used_resource_flag].to_i.zero?
"One #{@resource.name} freed."
else else
flash.now[:error] = "Resource update failed: #{@resource.errors.full_messages.join('. ')}." "One more #{@resource.name} used."
render :edit end
else
flash.now[:error] = "Resource #{@resource.name}'s update failed: #{@resource.errors.full_messages.join('. ')}."
format.html{ render :edit }
end
format.js
end end
end end
@ -48,5 +60,16 @@ module Admin
def resource_params def resource_params
params.require(:resource).permit(:name, :description, :quantity, :used, :conference_id) params.require(:resource).permit(:name, :description, :quantity, :used, :conference_id)
end end
def check_successful_update(increment_used_resource_flag)
if increment_used_resource_flag.present?
# Increment/Decrement of Resource via Index Page
@resource.used = params[:increment_used_resource_flag].to_i == 1 ? (@resource.used + 1) : (@resource.used - 1)
@resource.save
else
# Update via Edit Page
@resource.update_attributes(resource_params)
end
end
end end
end end

View file

@ -0,0 +1,17 @@
= link_to admin_conference_resource_path(conference.short_title,
resource, increment_used_resource_flag: 0), method: :put,
title: "Free 1 #{resource.name}",
class: "btn btn-primary #{'disabled' if resource.used <= 0}",
remote: true do
%span.fa.fa-minus.fa-2x.update_action
%button.btn.btn-default.quantity_left.disabled.action-btn
%span{ id: "resource_left_#{resource.id}" }
= quantity_left_of(resource)
= link_to admin_conference_resource_path(conference.short_title,
resource, increment_used_resource_flag: 1), method: :put,
title: "Use 1 #{resource.name}",
class: "btn btn-primary #{'disabled' if resource.used >= resource.quantity}",
remote: true do
%span.fa.fa-plus.fa-2x.update_action

View file

@ -10,7 +10,7 @@
%table.table.table-hover.datatable %table.table.table-hover.datatable
%thead %thead
%th Name %th Name
%th Left( Left/Total ) %th Remaining / Total
%th Used %th Used
%th Actions %th Actions
%tbody %tbody
@ -20,8 +20,17 @@
= link_to(admin_conference_resource_path(@conference.short_title, resource.id)) do = link_to(admin_conference_resource_path(@conference.short_title, resource.id)) do
= resource.name = resource.name
%td %td
= quantity_left_of(resource) .btn-group.hidden-sm.hidden-xs
%span{ id: "group_large_update_actions_#{resource.id}" }
= render partial: 'update_links',
locals: { conference: @conference, resource: resource }
.btn-group-vertical.visible-sm.visible-xs
%span{ id: "group_small_update_actions_#{resource.id}" }
= render partial: 'update_links',
locals: { conference: @conference, resource: resource}
%td %td
%span{ id: "resource_used_#{resource.id}" }
= resource.used = resource.used
%td %td
.btn-group .btn-group
@ -34,3 +43,4 @@
.col-md-12 .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)) = 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,5 @@
$('<%= "#group_large_update_actions_#{@resource.id.to_s}" %>').html('<%= j render partial: "update_links", locals: { conference: @conference, resource: @resource} %>');
$('<%= "#group_small_update_actions_#{@resource.id.to_s}" %>').html('<%= j render partial: "update_links", locals: { conference: @conference, resource: @resource} %>');
$('<%= "#resource_left_#{@resource.id.to_s}" %>').text('<%= "#{@resource.quantity - @resource.used }/#{@resource.quantity}"%>').fadeIn();
$('<%= "#resource_used_#{@resource.id.to_s}" %>').text(<%=@resource.used%>).fadeIn();

View file

@ -8,7 +8,7 @@
-# Index admin sidebar -# Index admin sidebar
= render 'layouts/admin_sidebar_index' = render 'layouts/admin_sidebar_index'
.col-md-10 .col-md-10
#messages .unobtrusive-flash-container#messages
=render 'layouts/messages' =render 'layouts/messages'
#content #content
= yield = yield

View file

@ -33,7 +33,7 @@ feature Resource do
click_button 'Update Resource' click_button 'Update Resource'
resource.reload resource.reload
expect(flash).to eq('Resource successfully updated.') expect(page).to have_css('#messages'), text('Resource successfully updated.')
expect(resource.name).to eq('changed_name') expect(resource.name).to eq('changed_name')
end end