refactoring code for attendance button

This commit is contained in:
raluka 2015-07-07 19:10:20 +02:00
parent 522f0b31d9
commit ff6f53aec2
3 changed files with 30 additions and 46 deletions

View file

@ -2,6 +2,8 @@ $(function () {
$("[class='switch-checkbox']").bootstrapSwitch(); $("[class='switch-checkbox']").bootstrapSwitch();
$('input[class="switch-checkbox"]').on('switchChange.bootstrapSwitch', function(event, state) { $('input[class="switch-checkbox"]').on('switchChange.bootstrapSwitch', function(event, state) {
console.log(event);
console.log(state);
var url = $(this).attr('url') + state; var url = $(this).attr('url') + state;
var method = $(this).attr('method'); var method = $(this).attr('method');
@ -11,4 +13,7 @@ $(function () {
dataType: 'script' dataType: 'script'
}); });
}); });
}); });

View file

@ -11,7 +11,8 @@ module Admin
@attended = @conference.registrations.where('attended = ?', true).count @attended = @conference.registrations.where('attended = ?', true).count
end end
def edit; end def edit;
end
def update def update
@registration.update_attributes(registration_params) @registration.update_attributes(registration_params)
@ -36,45 +37,22 @@ module Admin
end end
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 def toggle_attendance
if params[:attended] == "true" case params[:attended]
flash[:notice] = "#{@user.email} is attended." when "true"
@registration.attended = true @registration.attended = true
elsif params[:attended] == "false" when "false"
flash[:notice] = "#{@user.email} is not attended." @registration.attended = false
@registration.attended = false else
# redirect_to action: :index,
# error: 'Wrong url path.')
end end
unless @registration.save
flash[:notice] = "Update Attended for #{@user.email} failed!" \ if @registration.save
"#{@registration.errors.full_messages.join('. ')}" head :ok
else
head :unprocessable_entity
end end
render json: {attended: @registration.attended}
end end
protected protected
@ -86,13 +64,13 @@ module Admin
def registration_params def registration_params
params.require(:registration). params.require(:registration).
permit( permit(
:conference_id, :arrival, :departure, :conference_id, :arrival, :departure,
:volunteer, :volunteer,
vchoice_ids: [], qanswer_ids: [], vchoice_ids: [], qanswer_ids: [],
qanswers_attributes: [], qanswers_attributes: [],
user_attributes: [ user_attributes: [
:id, :name, :tshirt, :mobile, :volunteer_experience, :languages, :id, :name, :tshirt, :mobile, :volunteer_experience, :languages,
:nickname, :affiliation ]) :nickname, :affiliation])
end end
end end

View file

@ -44,8 +44,9 @@
%td %td
= link_to 'Questions','#', class: 'btn btn-success question-btn', 'data-id' => index, 'data-name' => registration.name = link_to 'Questions','#', class: 'btn btn-success question-btn', 'data-id' => index, 'data-name' => registration.name
%td{'data-order' => "#{registration.attended}"} %td{'data-order' => "#{registration.attended}"}
= check_box_tag "#{@conference.short_title}_#{registration.id}", registration.id, registration.attended, class: 'switch-checkbox', = check_box_tag "#{@conference.short_title}_#{registration.id}", registration.id, registration.attended,
method: :patch, url: toggle_attendance_admin_conference_registration_path(@conference.short_title, id: registration.id)+"?attended=", class: 'switch-checkbox', method: :patch,
url: toggle_attendance_admin_conference_registration_path(@conference.short_title, id: registration.id)+"?attended=",
data: { size: "large", data: { size: "large",
on_color: 'success', on_color: 'success',
off_color: 'warning', off_color: 'warning',