From e1832732b4295434209fff173dadb5b90006fd0a Mon Sep 17 00:00:00 2001 From: raluka Date: Thu, 2 Jul 2015 12:27:28 +0200 Subject: [PATCH 1/4] Added check_box for presence --- app/views/admin/registrations/index.html.haml | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/app/views/admin/registrations/index.html.haml b/app/views/admin/registrations/index.html.haml index eb89719a..30b90c8a 100644 --- a/app/views/admin/registrations/index.html.haml +++ b/app/views/admin/registrations/index.html.haml @@ -44,24 +44,14 @@ %td = link_to 'Questions','#', class: 'btn btn-success question-btn', 'data-id' => index, 'data-name' => registration.name %td + = check_box_tag class:'switch-checkbox', + data: { size: "large", + on_color: 'success', + off_color: 'warning', + on_text: 'Present', + off_text: 'Absent' } .btn-group .btn-group - - if registration.attended - - btnclass = 'btn-success' - - else - - btnclass = 'btn-warning' - %button{type: "button", class: "btn #{btnclass} dropdown-toggle", "data-toggle" => "dropdown", "aria-expanded" => "false"} - - if registration.attended - Present - - else - Absent - %span.caret - %ul.dropdown-menu{role: "menu"} - %li - = link_to "Present", present_admin_conference_registration_path(@conference.short_title, id: registration.id), - method: :patch - = link_to "Absent" , absent_admin_conference_registration_path(@conference.short_title, id: registration.id), - method: :patch = link_to 'Edit', edit_admin_conference_registration_path(@conference.short_title, id: registration), method: :get, class: 'btn btn-primary' = link_to 'Delete', admin_conference_registration_path(@conference.short_title, registration), From bd0cfb99bc4ca7d26db315e98707e0d6d3e1ccee Mon Sep 17 00:00:00 2001 From: raluka Date: Thu, 2 Jul 2015 14:23:31 +0200 Subject: [PATCH 2/4] properly created switch button --- app/views/admin/registrations/index.html.haml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/admin/registrations/index.html.haml b/app/views/admin/registrations/index.html.haml index 30b90c8a..a60dd3fc 100644 --- a/app/views/admin/registrations/index.html.haml +++ b/app/views/admin/registrations/index.html.haml @@ -44,8 +44,8 @@ %td = link_to 'Questions','#', class: 'btn btn-success question-btn', 'data-id' => index, 'data-name' => registration.name %td - = check_box_tag class:'switch-checkbox', - data: { size: "large", + = check_box_tag @conference.short_title, registration.id, true, class: 'switch-checkbox', method: :patch, + data: { size: "large", on_color: 'success', off_color: 'warning', on_text: 'Present', From 522f0b31d95efb5d38ee126729a542f6795896ca Mon Sep 17 00:00:00 2001 From: raluka Date: Mon, 6 Jul 2015 18:36:46 +0200 Subject: [PATCH 3/4] created action for attendance-button --- .../admin/registrations_controller.rb | 18 ++++++++++++++++++ app/views/admin/registrations/index.html.haml | 6 ++++-- config/routes.rb | 3 +-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin/registrations_controller.rb b/app/controllers/admin/registrations_controller.rb index 27dacfa4..fd4a02be 100644 --- a/app/controllers/admin/registrations_controller.rb +++ b/app/controllers/admin/registrations_controller.rb @@ -36,6 +36,7 @@ module Admin end end +=begin def present @registration.attended = true if @registration.save @@ -59,6 +60,22 @@ module Admin redirect_to admin_conference_registrations_path(@conference.short_title) end end +=end + + def toggle_attendance + if params[:attended] == "true" + flash[:notice] = "#{@user.email} is attended." + @registration.attended = true + elsif params[:attended] == "false" + flash[:notice] = "#{@user.email} is not attended." + @registration.attended = false + end + unless @registration.save + flash[:notice] = "Update Attended for #{@user.email} failed!" \ + "#{@registration.errors.full_messages.join('. ')}" + end + render json: {attended: @registration.attended} + end protected @@ -77,5 +94,6 @@ module Admin :id, :name, :tshirt, :mobile, :volunteer_experience, :languages, :nickname, :affiliation ]) end + end end diff --git a/app/views/admin/registrations/index.html.haml b/app/views/admin/registrations/index.html.haml index a60dd3fc..fdad2aa4 100644 --- a/app/views/admin/registrations/index.html.haml +++ b/app/views/admin/registrations/index.html.haml @@ -43,13 +43,15 @@ -if @conference.questions.any? %td = link_to 'Questions','#', class: 'btn btn-success question-btn', 'data-id' => index, 'data-name' => registration.name - %td - = check_box_tag @conference.short_title, registration.id, true, class: 'switch-checkbox', method: :patch, + %td{'data-order' => "#{registration.attended}"} + = check_box_tag "#{@conference.short_title}_#{registration.id}", registration.id, registration.attended, class: 'switch-checkbox', + method: :patch, url: toggle_attendance_admin_conference_registration_path(@conference.short_title, id: registration.id)+"?attended=", data: { size: "large", on_color: 'success', off_color: 'warning', on_text: 'Present', off_text: 'Absent' } + %td .btn-group .btn-group = link_to 'Edit', edit_admin_conference_registration_path(@conference.short_title, id: registration), diff --git a/config/routes.rb b/config/routes.rb index 2a5cbcd0..17c74bcc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -35,8 +35,7 @@ Osem::Application.routes.draw do resources :registrations, except: [:create, :new] do member do - patch :present - patch :absent + patch :toggle_attendance end end From ff6f53aec2eeb6bbae529893338e244073501a20 Mon Sep 17 00:00:00 2001 From: raluka Date: Tue, 7 Jul 2015 19:10:20 +0200 Subject: [PATCH 4/4] refactoring code for attendance button --- app/assets/javascripts/osem-switch.js | 5 ++ .../admin/registrations_controller.rb | 66 +++++++------------ app/views/admin/registrations/index.html.haml | 5 +- 3 files changed, 30 insertions(+), 46 deletions(-) diff --git a/app/assets/javascripts/osem-switch.js b/app/assets/javascripts/osem-switch.js index 35393625..7a406f2b 100644 --- a/app/assets/javascripts/osem-switch.js +++ b/app/assets/javascripts/osem-switch.js @@ -2,6 +2,8 @@ $(function () { $("[class='switch-checkbox']").bootstrapSwitch(); $('input[class="switch-checkbox"]').on('switchChange.bootstrapSwitch', function(event, state) { + console.log(event); + console.log(state); var url = $(this).attr('url') + state; var method = $(this).attr('method'); @@ -11,4 +13,7 @@ $(function () { dataType: 'script' }); }); + + + }); diff --git a/app/controllers/admin/registrations_controller.rb b/app/controllers/admin/registrations_controller.rb index fd4a02be..281758d3 100644 --- a/app/controllers/admin/registrations_controller.rb +++ b/app/controllers/admin/registrations_controller.rb @@ -11,7 +11,8 @@ module Admin @attended = @conference.registrations.where('attended = ?', true).count end - def edit; end + def edit; + end def update @registration.update_attributes(registration_params) @@ -36,45 +37,22 @@ module Admin end end -=begin - def present - @registration.attended = true - if @registration.save - flash[:notice] = "#{@user.email} has attended" - redirect_to admin_conference_registrations_path(@conference.short_title) - else - flash[:notice] = "Update Attended for #{@user.email} failed!" \ - "#{@registration.errors.full_messages.join('. ')}" - redirect_to admin_conference_registrations_path(@conference.short_title) - end - end - - def absent - @registration.attended = false - if @registration.save - flash[:notice] = "#{@user.email} has not attended" - redirect_to admin_conference_registrations_path(@conference.short_title) - else - flash[:notice] = "Update Attended for #{@user.email} failed!" \ - "#{@registration.errors.full_messages.join('. ')}" - redirect_to admin_conference_registrations_path(@conference.short_title) - end - end -=end - def toggle_attendance - if params[:attended] == "true" - flash[:notice] = "#{@user.email} is attended." - @registration.attended = true - elsif params[:attended] == "false" - flash[:notice] = "#{@user.email} is not attended." - @registration.attended = false + case params[:attended] + when "true" + @registration.attended = true + when "false" + @registration.attended = false + else + # redirect_to action: :index, + # error: 'Wrong url path.') end - unless @registration.save - flash[:notice] = "Update Attended for #{@user.email} failed!" \ - "#{@registration.errors.full_messages.join('. ')}" + + if @registration.save + head :ok + else + head :unprocessable_entity end - render json: {attended: @registration.attended} end protected @@ -86,13 +64,13 @@ module Admin def registration_params params.require(:registration). permit( - :conference_id, :arrival, :departure, - :volunteer, - vchoice_ids: [], qanswer_ids: [], - qanswers_attributes: [], - user_attributes: [ - :id, :name, :tshirt, :mobile, :volunteer_experience, :languages, - :nickname, :affiliation ]) + :conference_id, :arrival, :departure, + :volunteer, + vchoice_ids: [], qanswer_ids: [], + qanswers_attributes: [], + user_attributes: [ + :id, :name, :tshirt, :mobile, :volunteer_experience, :languages, + :nickname, :affiliation]) end end diff --git a/app/views/admin/registrations/index.html.haml b/app/views/admin/registrations/index.html.haml index fdad2aa4..54cc9d5a 100644 --- a/app/views/admin/registrations/index.html.haml +++ b/app/views/admin/registrations/index.html.haml @@ -44,8 +44,9 @@ %td = link_to 'Questions','#', class: 'btn btn-success question-btn', 'data-id' => index, 'data-name' => registration.name %td{'data-order' => "#{registration.attended}"} - = check_box_tag "#{@conference.short_title}_#{registration.id}", registration.id, registration.attended, class: 'switch-checkbox', - method: :patch, url: toggle_attendance_admin_conference_registration_path(@conference.short_title, id: registration.id)+"?attended=", + = check_box_tag "#{@conference.short_title}_#{registration.id}", registration.id, registration.attended, + class: 'switch-checkbox', method: :patch, + url: toggle_attendance_admin_conference_registration_path(@conference.short_title, id: registration.id)+"?attended=", data: { size: "large", on_color: 'success', off_color: 'warning',