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) {
var url = $(this).attr('url') + state;
var method = $(this).attr('method');
var method = $(this).attr('method') || 'patch';
$.ajax({
url: url,
@ -16,13 +16,20 @@ function checkboxSwitch(selector){
}
$(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']");
$("[class='switch-checkbox-schedule']").bootstrapSwitch();
$('input[class="switch-checkbox-schedule"]').on('switchChange.bootstrapSwitch', function(event, state) {
var url = $(this).attr('url');
var method = $(this).attr('method');
var method = $(this).attr('method') || 'patch';
if(state){
url += $(this).attr('value');

View file

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

View file

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

View file

@ -32,11 +32,7 @@
%td= event_registration.email
%td= event_registration.created_at
%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]=",
data: { size: 'small',
off_color: 'danger',
on_text: 'Yes',
off_text: 'No' }
= 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]="
%td
- if event_registration.registration.attended
%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
%td{ 'data-order' => registration.attended.to_s }
= 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: 'small',
on_color: 'success',
off_color: 'warning',
on_text: 'Present',
off_text: 'Absent' }
class: 'switch-checkbox',
url: toggle_attendance_admin_conference_registration_path(@conference.short_title, id: registration.id)+"?attended="
.btn-group
= link_to 'Edit', edit_admin_conference_registration_path(@conference.short_title, id: registration),
method: :get, class: 'btn btn-primary'

View file

@ -14,7 +14,7 @@
- if ( can? :toggle_user, @role )
%td.text-right
= 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.name
%td= user.email

View file

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

View file

@ -67,13 +67,8 @@
= 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 }
= check_box_tag "#{@conference.short_title}_#{track.short_name}", track.id, track.cfp_active,
class: 'switch-checkbox', method: :patch,
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' }
class: 'switch-checkbox',
url: toggle_cfp_inclusion_admin_conference_program_track_path(@conference.short_title, id: track.short_name)+"?included="
%td.text-center
- if track.self_organized?

View file

@ -78,13 +78,8 @@
%b Included in the Cfp?
%td
= check_box_tag "#{@conference.short_title}_#{@track.short_name}", @track.id, @track.cfp_active,
class: 'switch-checkbox', method: :patch,
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' }
class: 'switch-checkbox',
url: toggle_cfp_inclusion_admin_conference_program_track_path(@conference.short_title, id: @track.short_name)+"?included="
%tr
%td
%b State

View file

@ -6,18 +6,14 @@
Confirmed?
- if can? :toggle_confirmation, @user
= check_box_tag @user.id, @user.id, @user.confirmed?,
method: :patch,
url: "/admin/users/#{@user.id}/toggle_confirmation?user[to_confirm]=",
class: 'switch-checkbox',
readonly: false,
data: { size: 'small', on_color: 'success', off_color: 'warning', on_text: 'Yes', off_text: 'No' }
readonly: false
- else
= check_box_tag @user.id, @user.id, @user.confirmed?,
method: :patch,
url: "/admin/users/#{@user.id}/toggle_confirmation?user[to_confirm]=",
class: 'switch-checkbox',
readonly: true,
data: { size: 'small', on_color: 'success', off_color: 'warning', on_text: 'Yes', off_text: 'No' }
readonly: true
= 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 :username, :as => :string if @user.new_record?

View file

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

View file

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