Skip to content

Instantly share code, notes, and snippets.

@elizabrock
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elizabrock/40e36732e887d5fbfd7c to your computer and use it in GitHub Desktop.
Save elizabrock/40e36732e887d5fbfd7c to your computer and use it in GitHub Desktop.
Psuedo-code for choosing a random student
class Student < ActiveRecord::Base
default_scope ->{ order("id ASC") }
scope :not_called_on_today, ->{ my_scope_definition_goes_here }
def self.pick_victim
# This assumes the not_called_on_today is a named scope, defined elsewhere in the class
possible_students = Student.not_called_on_today.all
possible_students.sample # Gives us a random student from the list
end
end
RSpec.describe "Choosing a victim" do
it 'can return the first valid choice' do
student_1 = Fabricate(:student)
Fabricate(:student)
Fabricate(:student)
Fabricate(:student, called_on: 1.hour.ago)
Array.any_instance.stub(:sample) do |array|
array[0]
end
Student.pick_victim.should == student_1
end
it 'can return the last valid choice' do
Fabricate(:student)
Fabricate(:student)
student_3 = Fabricate(:student)
Fabricate(:student, called_on: 1.hour.ago)
Array.any_instance.stub(:sample) do |array|
array.last
end
Student.pick_victim.should == student_3
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment