Last active
August 29, 2015 14:05
-
-
Save elizabrock/40e36732e887d5fbfd7c to your computer and use it in GitHub Desktop.
Psuedo-code for choosing a random student
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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