Skip to content

Commit

Permalink
Make the SectionArranger a real object instead of a bag of class methods
Browse files Browse the repository at this point in the history
Fix a couple syntactic weirdnesses
  • Loading branch information
tjgrathwell committed Jan 7, 2016
1 parent f44018d commit 5434828
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def index
respond_to do |format|
format.html do
@events = Event.upcoming.published_or_organized_by(current_user).includes(:event_sessions, :location, :region)
@event_regions = @events.map { |e| e.region }.compact.uniq
@event_regions = @events.map(&:region).compact.uniq
@past_events = EventList.new('past').combined_events
end
format.json do
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/sections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def destroy
end

def arrange
SectionArranger.arrange(@event, params[:checked_in_to])
SectionArranger.new(@event).arrange(params[:checked_in_to])
redirect_to event_organize_sections_path(@event)
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def name_with_region
def editable_by?(user)
return true if events_count == 0
return true if user.admin?
notable_events.map { |e| e.organizers }.flatten.map(&:id).include?(user.id)
notable_events.map(&:organizers).flatten.map(&:id).include?(user.id)
end

def additional_details_editable_by?(user)
Expand Down
28 changes: 17 additions & 11 deletions app/services/section_arranger.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
class SectionArranger
IDEAL_CLASS_SIZE = 6.0

def self.arrange(event, checked_in = 'indiscriminate')
attr_reader :event

def initialize(event)
@event = event
end

def arrange(checked_in = 'indiscriminate')
event.sections.destroy_all
return if event.student_rsvps_count == 0

student_rsvps, volunteer_rsvps = rsvps_to_arrange(event, checked_in)
student_rsvps, volunteer_rsvps = rsvps_to_arrange(checked_in)

section_counts = self.section_counts(event)
sections = Hash.new { |hsh, key| hsh[key] = [] }

student_rsvps.each do |rsvp|
Expand Down Expand Up @@ -46,24 +51,25 @@ def self.arrange(event, checked_in = 'indiscriminate')

private

def self.rsvps_to_arrange(event, checked_in)
def rsvps_to_arrange(checked_in)
if checked_in == 'any'
condition = Proc.new { |relation| relation.where("checkins_count > 0") }
condition = proc { |relation| relation.where("checkins_count > 0") }
elsif checked_in == 'indiscriminate'
condition = Proc.new { |relation| relation }
condition = proc { |relation| relation }
else
session_id = checked_in.to_i
condition = Proc.new do |relation|
condition = proc do |relation|
relation.
joins(:rsvp_sessions).
where('rsvp_sessions.event_session_id' => session_id).
where("rsvp_sessions.checked_in = ?", true).readonly(false)
end
end
return [event.student_rsvps, event.volunteer_rsvps].map(&condition)

[event.student_rsvps, event.volunteer_rsvps].map(&condition)
end

def self.section_size_given_total_students(count)
def section_size_given_total_students(count)
if count <= IDEAL_CLASS_SIZE + 2
return count
end
Expand All @@ -72,8 +78,8 @@ def self.section_size_given_total_students(count)
(count / number_of_sections.to_f).ceil
end

def self.section_counts(event)
event.student_rsvps.select('event_id, class_level, count(class_level) count')
def section_counts
@section_counts ||= event.student_rsvps.select('event_id, class_level, count(class_level) count')
.group(:event_id, :class_level)
.each_with_object({}) do |rsvp_group, hsh|
hsh[rsvp_group.class_level] = section_size_given_total_students(rsvp_group.count.to_i)
Expand Down
2 changes: 1 addition & 1 deletion bin/rerun_failures
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
puts "Paste a list of rspec failures, followed by a blank line:"

lines = []
while line = gets
while (line = gets)
break if line == "\n"
lines << line
end
Expand Down
4 changes: 3 additions & 1 deletion spec/controllers/sections_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@

describe "#arrange" do
it 'tells the section arranger to arrange sections for this event' do
expect(SectionArranger).to receive(:arrange).with(@event, 'any')
fake_section_arranger = instance_double(SectionArranger)
expect(fake_section_arranger).to receive(:arrange).with('any')
expect(SectionArranger).to receive(:new).with(@event).and_return(fake_section_arranger)
post :arrange, event_id: @event.id, checked_in_to: 'any'
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/services/rsvp_sorter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:event) { create(:event) }

let(:rsvp_name) do
Proc.new { |r| r.user.full_name }
proc { |r| r.user.full_name }
end

context 'for a modern event' do
Expand Down
12 changes: 6 additions & 6 deletions spec/services/section_arranger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def volunteer_preferences(event)
end

it 'arranges students into classes based on their level' do
SectionArranger.arrange(@event)
SectionArranger.new(@event).arrange
expected_arrangement = {
1 => [{students: 1, volunteers: 2}],
2 => [{students: 5, volunteers: 2}, {students: 5, volunteers: 2}],
Expand All @@ -77,7 +77,7 @@ def volunteer_preferences(event)
end

it "assigns the students to sections without incident" do
SectionArranger.arrange(@event)
SectionArranger.new(@event).arrange
expected_arrangement = {
1 => [{students: 1, volunteers: 0}],
2 => [{students: 1, volunteers: 0}]
Expand All @@ -94,7 +94,7 @@ def volunteer_preferences(event)
end

it "doesn't do anything, successfully" do
SectionArranger.arrange(@event)
SectionArranger.new(@event).arrange
expect(@event.rsvps.map(&:section_id).uniq).to eq([nil])
end
end
Expand Down Expand Up @@ -127,7 +127,7 @@ def volunteer_preferences(event)

context "when asked to arrange for only the first session" do
before do
SectionArranger.arrange(@event, @session1.id)
SectionArranger.new(@event).arrange(@session1.id)
end

it 'arranges only those people' do
Expand All @@ -137,7 +137,7 @@ def volunteer_preferences(event)

context "when asked to arrange for only the second session" do
before do
SectionArranger.arrange(@event, @session2.id)
SectionArranger.new(@event).arrange(@session2.id)
end

it 'arranges only those people' do
Expand All @@ -147,7 +147,7 @@ def volunteer_preferences(event)

context "when asked to arrange for people who have checked in to any session" do
before do
SectionArranger.arrange(@event, 'any')
SectionArranger.new(@event).arrange('any')
end

it 'arranges only those people' do
Expand Down

0 comments on commit 5434828

Please sign in to comment.