Skip to content

Commit

Permalink
Event sessions can now be marked as 'volunteers only'
Browse files Browse the repository at this point in the history
Volunteer only sessions do not show up on student RSVP forms

An event cannot be both 'volunteers only' and 'required for students'
  • Loading branch information
tjgrathwell committed Dec 7, 2014
1 parent df000ea commit 00a361c
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 8 deletions.
10 changes: 10 additions & 0 deletions app/assets/javascripts/events.js.coffee
Expand Up @@ -20,6 +20,14 @@ setUpDatePicker = ($el) ->
$el.on('change', dateChanged)
$el.datepicker()

setUpExclusiveCheckboxes = ($el) ->
$el.find('.exclusive-checkbox').on 'change', (e) ->
$target = $(e.target)
if ($target.prop('checked'))
$target.closest('.form-group').find('.exclusive-checkbox').each (ix, checkbox) ->
if (checkbox.id != e.target.id)
$(checkbox).prop('checked', false)

setupRemoveSessions = ->
if $('.remove-session').length
$(document).on 'click', '.remove-session > a', (e)->
Expand All @@ -37,6 +45,7 @@ jQuery ->
$('.select2-dropdown').select2(width: 'element')

setUpDatePicker($('.datepicker'))
setUpExclusiveCheckboxes($('body'))

rsvpTypesChanged = ->
$el = $('.workshop-only')
Expand All @@ -54,6 +63,7 @@ jQuery ->
$field = event.field
$dateField = $field.find('.datepicker')
setUpDatePicker($dateField)
setUpExclusiveCheckboxes($field)

cocChanged = ->
$el = $('#coc')
Expand Down
4 changes: 4 additions & 0 deletions app/assets/stylesheets/events/_form.css.scss
Expand Up @@ -37,5 +37,9 @@
margin: 0;
}
}

.session-checkboxes label {
margin-right: 12px;
}
}

7 changes: 6 additions & 1 deletion app/models/event_session.rb
@@ -1,5 +1,5 @@
class EventSession < ActiveRecord::Base
PERMITTED_ATTRIBUTES = [:starts_at, :ends_at, :name, :required_for_students]
PERMITTED_ATTRIBUTES = [:starts_at, :ends_at, :name, :required_for_students, :volunteers_only]

validates_presence_of :starts_at, :ends_at, :name
validates_uniqueness_of :name, scope: [:event_id]
Expand All @@ -13,6 +13,11 @@ class EventSession < ActiveRecord::Base
errors.add(:ends_at, 'must be after session start time')
end
end
validate do
if required_for_students && volunteers_only
errors.add(:base, "A session cannot be both Required for Students and Volunteers Only")
end
end

belongs_to :event, inverse_of: :event_sessions
has_many :rsvp_sessions, dependent: :destroy
Expand Down
9 changes: 9 additions & 0 deletions app/models/rsvp.rb
Expand Up @@ -62,6 +62,15 @@ def setup_for_role(role)
end
end

def selectable_sessions
sessions = event.event_sessions.order('starts_at ASC')
if role == Role::VOLUNTEER
sessions
elsif role == Role::STUDENT
sessions.where(volunteers_only: false)
end
end

def operating_system_title
operating_system.try(:title)
end
Expand Down
8 changes: 6 additions & 2 deletions app/views/events/_form.html.erb
Expand Up @@ -53,9 +53,13 @@
-
<%= event_sessions_form.time_select :ends_at, {:ampm => true, :minute_step => 15}, {class: "input-small end_time form-control"} %>
</div>
<div class='form-group'>
<div class='form-group session-checkboxes'>
<%= event_sessions_form.label :required_for_students do %>
<%= event_sessions_form.check_box :required_for_students %> Required for Students?
<%= event_sessions_form.check_box :required_for_students, class: 'exclusive-checkbox' %> Required for Students?
<% end %>
<%= event_sessions_form.label :volunteers_only do %>
<%= event_sessions_form.check_box :volunteers_only, class: 'exclusive-checkbox' %> Volunteers Only!
<% end %>
</div>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/rsvps/_form.html.erb
Expand Up @@ -83,7 +83,7 @@
<% if @rsvp.event.event_sessions.length > 1 %>
<h2>Sessions you're attending</h2>
<%= f.collection_check_boxes(:event_session_ids,
@event.event_sessions.order('starts_at ASC'),
@rsvp.selectable_sessions,
:id,
-> (session) { "#{session.name}: #{formatted_session_date(session)} #{formatted_session_timerange(session)}" },
{
Expand Down
@@ -0,0 +1,5 @@
class AddVolunteersOnlyToEventSessions < ActiveRecord::Migration
def change
add_column :event_sessions, :volunteers_only, :boolean, default: false
end
end
9 changes: 5 additions & 4 deletions db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20141115223733) do
ActiveRecord::Schema.define(version: 20141205091133) do

create_table "authentications", force: true do |t|
t.integer "user_id"
Expand Down Expand Up @@ -72,10 +72,11 @@
t.datetime "starts_at"
t.datetime "ends_at"
t.integer "event_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name", null: false
t.boolean "required_for_students", default: true
t.boolean "volunteers_only", default: false
end

add_index "event_sessions", ["event_id", "name"], name: "index_event_sessions_on_event_id_and_name", unique: true
Expand Down
7 changes: 7 additions & 0 deletions spec/models/event_session_spec.rb
Expand Up @@ -26,6 +26,13 @@
session.should have(0).errors_on(:starts_at)
end

it 'does not allow required_for_students and volunteers_only simultaneously' do
session = create(:event_session)
session.volunteers_only = true
session.required_for_students = true
session.should have(1).error_on(:base)
end

describe "#update_event_times" do
it "denormalizes starts_at and ends_at onto the event" do
event = create(:event)
Expand Down
31 changes: 31 additions & 0 deletions spec/models/rsvp_spec.rb
Expand Up @@ -127,6 +127,37 @@
end
end

describe "#selectable_sessions" do
let(:event) do
build(:event_with_no_sessions).tap do |event|
@session_no_options = build(:event_session, event: event, required_for_students: false, volunteers_only: false)
event.event_sessions << @session_no_options
event.save!
end
end

before do
@session_required_for_students = create(:event_session, event: event, required_for_students: true, volunteers_only: false)
@session_volunteers_only = create(:event_session, event: event, required_for_students: false, volunteers_only: true)
end

describe "for students" do
let(:rsvp) { create(:student_rsvp, event: event) }

it "returns only those sessions which are not marked as volunteer only" do
rsvp.selectable_sessions.pluck(:id).should =~ [@session_no_options.id, @session_required_for_students.id]
end
end

describe "for volunteers" do
let(:rsvp) { create(:volunteer_rsvp, event: event) }

it "returns all sessions" do
rsvp.selectable_sessions.pluck(:id).should =~ [@session_no_options.id, @session_required_for_students.id, @session_volunteers_only.id]
end
end
end

describe "#as_json" do
before do
@user = create(:user, first_name: 'Bill', last_name: 'Blank')
Expand Down

0 comments on commit 00a361c

Please sign in to comment.