DRY up bootstrapSwitches with default values

This commit is contained in:
James Mason 2018-06-18 17:44:06 -07:00
parent d703dad496
commit 1e9765c163
12 changed files with 33 additions and 66 deletions

View file

@ -5,7 +5,7 @@ function checkboxSwitch(selector){
$(selector).on('switchChange.bootstrapSwitch', function(event, state) { $(selector).on('switchChange.bootstrapSwitch', function(event, state) {
var url = $(this).attr('url') + state; var url = $(this).attr('url') + state;
var method = $(this).attr('method'); var method = $(this).attr('method') || 'patch';
$.ajax({ $.ajax({
url: url, url: url,
@ -16,13 +16,20 @@ function checkboxSwitch(selector){
} }
$(function () { $(function () {
$.fn.bootstrapSwitch.defaults.onColor = 'success';
$.fn.bootstrapSwitch.defaults.offColor = 'warning';
$.fn.bootstrapSwitch.defaults.onText = 'Yes';
$.fn.bootstrapSwitch.defaults.offText = 'No';
$.fn.bootstrapSwitch.defaults.size = 'small';
checkboxSwitch("[class='switch-checkbox']"); checkboxSwitch("[class='switch-checkbox']");
$("[class='switch-checkbox-schedule']").bootstrapSwitch(); $("[class='switch-checkbox-schedule']").bootstrapSwitch();
$('input[class="switch-checkbox-schedule"]').on('switchChange.bootstrapSwitch', function(event, state) { $('input[class="switch-checkbox-schedule"]').on('switchChange.bootstrapSwitch', function(event, state) {
var url = $(this).attr('url'); var url = $(this).attr('url');
var method = $(this).attr('method'); var method = $(this).attr('method') || 'patch';
if(state){ if(state){
url += $(this).attr('value'); url += $(this).attr('value');

View file

@ -168,14 +168,7 @@ module EventsHelper
event, event,
event: { attribute => nil } event: { attribute => nil }
), ),
method: :patch, class: 'switch-checkbox'
class: 'switch-checkbox',
data: {
size: 'small',
off_color: 'warning',
on_text: 'Yes',
off_text: 'No'
}
) )
end end

View file

@ -15,11 +15,9 @@
%td= user.created_at.strftime('%m/%d/%Y') %td= user.created_at.strftime('%m/%d/%Y')
%td %td
= check_box_tag user.id, user.id, user.confirmed?, = check_box_tag user.id, user.id, user.confirmed?,
method: :patch,
url: "/admin/users/#{user.id}/toggle_confirmation?user[to_confirm]=", url: "/admin/users/#{user.id}/toggle_confirmation?user[to_confirm]=",
class: 'switch-checkbox', class: 'switch-checkbox',
readonly: true, readonly: true
data: { size: 'small', on_color: 'success', off_color: 'warning', on_text: 'Yes', off_text: 'No' }
- else - else
%h5.text-warning.text-center %h5.text-warning.text-center
No sign ups! No sign ups!

View file

@ -32,11 +32,7 @@
%td= event_registration.email %td= event_registration.email
%td= event_registration.created_at %td= event_registration.created_at
%td %td
= check_box_tag @conference.short_title, @event.id, event_registration.attended, class: 'switch-checkbox', method: :patch, url: "/admin/conferences/#{@conference.short_title}/program/events/#{@event.id}/toggle_attendance?events_registration_id=#{event_registration.id}&&event_registration[attended]=", = check_box_tag @conference.short_title, @event.id, event_registration.attended, class: 'switch-checkbox', url: "/admin/conferences/#{@conference.short_title}/program/events/#{@event.id}/toggle_attendance?events_registration_id=#{event_registration.id}&&event_registration[attended]="
data: { size: 'small',
off_color: 'danger',
on_text: 'Yes',
off_text: 'No' }
%td %td
- if event_registration.registration.attended - if event_registration.registration.attended
%i.fa.fa-check.text-success %i.fa.fa-check.text-success

View file

@ -68,13 +68,8 @@
= 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.to_s } %td{ 'data-order' => registration.attended.to_s }
= check_box_tag "#{@conference.short_title}_#{registration.id}", registration.id, registration.attended, = check_box_tag "#{@conference.short_title}_#{registration.id}", registration.id, registration.attended,
class: 'switch-checkbox', method: :patch, class: 'switch-checkbox',
url: toggle_attendance_admin_conference_registration_path(@conference.short_title, id: registration.id)+"?attended=", url: toggle_attendance_admin_conference_registration_path(@conference.short_title, id: registration.id)+"?attended="
data: { size: 'small',
on_color: 'success',
off_color: 'warning',
on_text: 'Present',
off_text: 'Absent' }
.btn-group .btn-group
= link_to 'Edit', edit_admin_conference_registration_path(@conference.short_title, id: registration), = link_to 'Edit', edit_admin_conference_registration_path(@conference.short_title, id: registration),
method: :get, class: 'btn btn-primary' method: :get, class: 'btn btn-primary'

View file

@ -14,7 +14,7 @@
- if ( can? :toggle_user, @role ) - if ( can? :toggle_user, @role )
%td.text-right %td.text-right
= hidden_field_tag "role[user_ids][]", nil = hidden_field_tag "role[user_ids][]", nil
= check_box_tag @conference.short_title, @role.id, (@role.user_ids.include? user.id), method: :post, url: "#{@url}?user[email]=#{user.email}&user[state]=", class: 'switch-checkbox', data: { size: 'small', off_color: 'warning', on_text: 'Yes', off_text: 'No' } = check_box_tag @conference.short_title, @role.id, (@role.user_ids.include? user.id), url: "#{@url}?user[email]=#{user.email}&user[state]=", method: :post, class: 'switch-checkbox'
%td= user.id %td= user.id
%td= user.name %td= user.name
%td= user.email %td= user.email

View file

@ -19,11 +19,8 @@
value = @schedule.id == @selected_schedule.try(:id) value = @schedule.id == @selected_schedule.try(:id)
url = admin_conference_program_path(@conference.short_title) + '?[program][selected_schedule_id]=' url = admin_conference_program_path(@conference.short_title) + '?[program][selected_schedule_id]='
end end
= check_box_tag @conference.short_title, @schedule.id, value, method: :patch, url: url, = check_box_tag @conference.short_title, @schedule.id, value, url: url,
class: 'switch-checkbox-schedule', data: { size: 'small', class: 'switch-checkbox-schedule'
off_color: 'warning',
on_text: 'Yes',
off_text: 'No' }
.h4 .h4
Unscheduled events Unscheduled events
.unscheduled-events .unscheduled-events

View file

@ -67,13 +67,8 @@
= link_to track.submitter.name, admin_user_path(track.submitter) if track.self_organized? = link_to track.submitter.name, admin_user_path(track.submitter) if track.self_organized?
%td.text-center{ 'id' => "cfp_switch_#{track.id}", 'data-order' => track.cfp_active.to_s } %td.text-center{ 'id' => "cfp_switch_#{track.id}", 'data-order' => track.cfp_active.to_s }
= check_box_tag "#{@conference.short_title}_#{track.short_name}", track.id, track.cfp_active, = check_box_tag "#{@conference.short_title}_#{track.short_name}", track.id, track.cfp_active,
class: 'switch-checkbox', method: :patch, class: 'switch-checkbox',
url: toggle_cfp_inclusion_admin_conference_program_track_path(@conference.short_title, id: track.short_name)+"?included=", url: toggle_cfp_inclusion_admin_conference_program_track_path(@conference.short_title, id: track.short_name)+"?included="
data: { size: 'small',
on_color: 'success',
off_color: 'warning',
on_text: 'Yes',
off_text: 'No' }
%td.text-center %td.text-center
- if track.self_organized? - if track.self_organized?

View file

@ -78,13 +78,8 @@
%b Included in the Cfp? %b Included in the Cfp?
%td %td
= check_box_tag "#{@conference.short_title}_#{@track.short_name}", @track.id, @track.cfp_active, = check_box_tag "#{@conference.short_title}_#{@track.short_name}", @track.id, @track.cfp_active,
class: 'switch-checkbox', method: :patch, class: 'switch-checkbox',
url: toggle_cfp_inclusion_admin_conference_program_track_path(@conference.short_title, id: @track.short_name)+"?included=", url: toggle_cfp_inclusion_admin_conference_program_track_path(@conference.short_title, id: @track.short_name)+"?included="
data: { size: 'small',
on_color: 'success',
off_color: 'warning',
on_text: 'Yes',
off_text: 'No' }
%tr %tr
%td %td
%b State %b State

View file

@ -6,18 +6,14 @@
Confirmed? Confirmed?
- if can? :toggle_confirmation, @user - if can? :toggle_confirmation, @user
= check_box_tag @user.id, @user.id, @user.confirmed?, = check_box_tag @user.id, @user.id, @user.confirmed?,
method: :patch,
url: "/admin/users/#{@user.id}/toggle_confirmation?user[to_confirm]=", url: "/admin/users/#{@user.id}/toggle_confirmation?user[to_confirm]=",
class: 'switch-checkbox', class: 'switch-checkbox',
readonly: false, readonly: false
data: { size: 'small', on_color: 'success', off_color: 'warning', on_text: 'Yes', off_text: 'No' }
- else - else
= check_box_tag @user.id, @user.id, @user.confirmed?, = check_box_tag @user.id, @user.id, @user.confirmed?,
method: :patch,
url: "/admin/users/#{@user.id}/toggle_confirmation?user[to_confirm]=", url: "/admin/users/#{@user.id}/toggle_confirmation?user[to_confirm]=",
class: 'switch-checkbox', class: 'switch-checkbox',
readonly: true, readonly: true
data: { size: 'small', on_color: 'success', off_color: 'warning', on_text: 'Yes', off_text: 'No' }
= f.input :is_admin, hint: 'An admin can create a new conference, manage users and make other users admins.' = f.input :is_admin, hint: 'An admin can create a new conference, manage users and make other users admins.'
= f.input :name, as: :string = f.input :name, as: :string
= f.input :username, :as => :string if @user.new_record? = f.input :username, :as => :string if @user.new_record?

View file

@ -27,7 +27,7 @@
%td %td
= markdown(@user.biography) = markdown(@user.biography)
- elsif attr == 'email' - elsif attr == 'email'
%td %td
= @user.send(attr) = @user.send(attr)
%tr %tr
%td %td
@ -35,19 +35,15 @@
Confirmed? Confirmed?
%td %td
- if can? :toggle_confirmation, @user - if can? :toggle_confirmation, @user
= check_box_tag @user.id, @user.id, @user.confirmed?, = check_box_tag @user.id, @user.id, @user.confirmed?,
method: :patch, url: "/admin/users/#{@user.id}/toggle_confirmation?user[to_confirm]=",
url: "/admin/users/#{@user.id}/toggle_confirmation?user[to_confirm]=", class: 'switch-checkbox',
class: 'switch-checkbox', readonly: false
readonly: false,
data: { size: 'small', on_color: 'success', off_color: 'warning', on_text: 'Yes', off_text: 'No' }
- else - else
= check_box_tag @user.id, @user.id, @user.confirmed?, = check_box_tag @user.id, @user.id, @user.confirmed?,
method: :patch, url: "/admin/users/#{@user.id}/toggle_confirmation?user[to_confirm]=",
url: "/admin/users/#{@user.id}/toggle_confirmation?user[to_confirm]=", class: 'switch-checkbox',
class: 'switch-checkbox', readonly: true
readonly: true,
data: { size: 'small', on_color: 'success', off_color: 'warning', on_text: 'Yes', off_text: 'No' }
- else - else
%td= @user.send(attr) %td= @user.send(attr)
#submissions-content.tab-pane{class: "#{'active' if params[:tab] == 'submissions-content'}"} #submissions-content.tab-pane{class: "#{'active' if params[:tab] == 'submissions-content'}"}

View file

@ -71,9 +71,8 @@ describe EventsHelper, type: :helper do
) )
end end
it 'should patch to the admin event url' do it 'should use the admin event url' do
expect(result).to include( expect(result).to include(
'method="patch"',
"url=\"/admin/conferences/#{conference.short_title}/program" \ "url=\"/admin/conferences/#{conference.short_title}/program" \
"/events/#{event.id}?event%5Bis_highlight%5D=\"" "/events/#{event.id}?event%5Bis_highlight%5D=\""
) )