Skip to content

Instantly share code, notes, and snippets.

@tbbooher
Created February 4, 2012 03:54
Show Gist options
  • Save tbbooher/1735117 to your computer and use it in GitHub Desktop.
Save tbbooher/1735117 to your computer and use it in GitHub Desktop.
class OfFitnessCampLocationValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless record.time_slot.fitness_camp.location_id.to_s == record.user.location_id.to_s
record.errors[attribute] << (options[:message] || "A user and time slot must share the same location")
end
if record.time_slot.fitness_camp.time_slots.map{|ts| ts.registrations}.flatten.map(&:user_id).include?(record.user.id)
record.errors[attribute] << (options[:message] || "A user can only register for one time_slot in a camp")
end
end
end
class Registration
include Mongoid::Document
include Mongoid::Timestamps
include ActiveModel::Validations
belongs_to :user
belongs_to :time_slot # , :null => false
belongs_to :order
has_many :friends
belongs_to :fitness_camp
validates :user, presence: true, of_fitness_camp_location: true
# does this do anything? is this deprecated?
# def self.produce_registration(cart_item)
# rsg = self.new
# rsg.time_slot_id = cart_item.fitnesscamp.id
# rsg.order_id = cart_item.order
# rsg
# end
end
require 'spec_helper'
describe Registration do
before(:each) do
cleanup_database
@ts = FactoryGirl.create(:time_slot)
@location_A = @ts.fitness_camp.location
@location_B = FactoryGirl.create(:location, name: "location B")
@user_at_B = FactoryGirl.create(:user, location_id: @location_B.id)
@user_at_A = FactoryGirl.create(:user, location_id: @location_A.id)
@second_user_at_A = FactoryGirl.create(:user, location_id: @location_A.id)
end
it "should reject a user that is not in the same location as the time_slot" do
r = Registration.new(user: @user_at_B, time_slot: @ts)
r.should_not be_valid
r.errors.messages[:user].first.should == "A user and time slot must share the same location"
end
it "should be valid if the user belongs to location and is unique" do
r = Registration.new(user: @user_at_A, time_slot: @ts)
r.should be_valid
end
it "should reject a user that tries to register twice" do
r = Registration.new(user: @user_at_A, time_slot: @ts)
r.save.should be true
second_r = Registration.new(user: @user_at_A, time_slot: @ts)
second_r.should_not be_valid
second_r.errors.messages[:user].first.should == "A user can only register for one time_slot in a camp"
end
it "should reject a user that has registerd for two time slots in the same camp" do
@ts_2_at_A = FactoryGirl.create(:time_slot, fitness_camp_id: @ts.fitness_camp.id)
Registration.create(user: @user_at_A, time_slot: @ts_2_at_A)
r = Registration.new(user: @user_at_A, time_slot: @ts)
r.should_not be_valid
r.errors.messages[:user].first.should == "A user can only register for one time_slot in a camp"
end
it "should not reject a user if two people sign up for a camp" do
r1 = Registration.create(user: @user_at_A, time_slot_id: @ts.id)
r2 = Registration.new(user: @second_user_at_A, time_slot_id: @ts.id)
r2.should be_valid
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment