From 4c60baf7c11f2e9706c795b35ad46696aa97e62f Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Wed, 5 Apr 2017 01:36:22 +0530 Subject: [PATCH] Add increment and decrement functionality to left resource --- app/assets/javascripts/application.js | 2 ++ app/assets/stylesheets/osem.css.scss | 5 +++ app/controllers/admin/resources_controller.rb | 35 +++++++++++++++---- .../admin/resources/_update_links.html.haml | 17 +++++++++ app/views/admin/resources/index.html.haml | 16 +++++++-- app/views/admin/resources/update.js.erb | 5 +++ app/views/layouts/_admin.html.haml | 4 +-- spec/features/resource_spec.rb | 2 +- 8 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 app/views/admin/resources/_update_links.html.haml create mode 100644 app/views/admin/resources/update.js.erb diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index b347821b..a3b468a0 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -57,3 +57,5 @@ $(document).ready(function() { delegateSelector: 'a.smoothscroll' }); }); + +UnobtrusiveFlash.flashOptions['timeout'] = 3000; diff --git a/app/assets/stylesheets/osem.css.scss b/app/assets/stylesheets/osem.css.scss index 5a331436..6761524d 100644 --- a/app/assets/stylesheets/osem.css.scss +++ b/app/assets/stylesheets/osem.css.scss @@ -94,3 +94,8 @@ p.comment-body { .box{ height: 230px; } + +.quantity_left{ + height: 3em; + width: 9em; +} diff --git a/app/controllers/admin/resources_controller.rb b/app/controllers/admin/resources_controller.rb index 89b068c3..5aa8d54b 100644 --- a/app/controllers/admin/resources_controller.rb +++ b/app/controllers/admin/resources_controller.rb @@ -2,6 +2,7 @@ module Admin class ResourcesController < Admin::BaseController load_and_authorize_resource :conference, find_by: :short_title load_and_authorize_resource :resource, only: [:show, :edit, :update, :destroy] + after_action :prepare_unobtrusive_flash, only: [:update] def index; end @@ -23,12 +24,23 @@ module Admin 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.now[:error] = "Resource update failed: #{@resource.errors.full_messages.join('. ')}." - render :edit + 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), + notice: 'Resource successfully updated.' + end + flash.now[:notice] = if params[:increment_used_resource_flag].to_i.zero? + "One #{@resource.name} freed." + else + "One more #{@resource.name} used." + 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 @@ -48,5 +60,16 @@ module Admin def resource_params params.require(:resource).permit(:name, :description, :quantity, :used, :conference_id) 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 diff --git a/app/views/admin/resources/_update_links.html.haml b/app/views/admin/resources/_update_links.html.haml new file mode 100644 index 00000000..92501ef7 --- /dev/null +++ b/app/views/admin/resources/_update_links.html.haml @@ -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 + + diff --git a/app/views/admin/resources/index.html.haml b/app/views/admin/resources/index.html.haml index 7f1b6137..285f2480 100644 --- a/app/views/admin/resources/index.html.haml +++ b/app/views/admin/resources/index.html.haml @@ -10,7 +10,7 @@ %table.table.table-hover.datatable %thead %th Name - %th Left( Left/Total ) + %th Remaining / Total %th Used %th Actions %tbody @@ -20,9 +20,18 @@ = link_to(admin_conference_resource_path(@conference.short_title, resource.id)) do = resource.name %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 - = resource.used + %span{ id: "resource_used_#{resource.id}" } + = resource.used %td .btn-group = link_to 'Edit', edit_admin_conference_resource_path(@conference.short_title, resource.id), @@ -34,3 +43,4 @@ .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)) + diff --git a/app/views/admin/resources/update.js.erb b/app/views/admin/resources/update.js.erb new file mode 100644 index 00000000..8de2a911 --- /dev/null +++ b/app/views/admin/resources/update.js.erb @@ -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(); + diff --git a/app/views/layouts/_admin.html.haml b/app/views/layouts/_admin.html.haml index a9add1ac..36202abf 100644 --- a/app/views/layouts/_admin.html.haml +++ b/app/views/layouts/_admin.html.haml @@ -8,7 +8,7 @@ -# Index admin sidebar = render 'layouts/admin_sidebar_index' .col-md-10 - #messages + .unobtrusive-flash-container#messages =render 'layouts/messages' #content - = yield \ No newline at end of file + = yield diff --git a/spec/features/resource_spec.rb b/spec/features/resource_spec.rb index 1d5c0437..c82a8aa5 100644 --- a/spec/features/resource_spec.rb +++ b/spec/features/resource_spec.rb @@ -33,7 +33,7 @@ feature Resource do click_button 'Update Resource' 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') end