Skip to content
Browse files

Limit the validation of outing restaurant's uniqueness to a single day

  • Loading branch information...
1 parent 51d62c1 commit f7fe94001b3efbbe0f93e80d9bace8bfc8a4d96e Matthew Conway committed Dec 1, 2010
View
2 app/controllers/outings_controller.rb
@@ -12,7 +12,7 @@ def create
flash[:notice] = "Your outing has been created."
respond_with outing, :location => root_path
else
- flash[:alert] = "That outing has already been created!"
+ flash[:alert] = outing.errors.full_messages
redirect_to root_path
end
View
2 app/controllers/restaurants_controller.rb
@@ -3,7 +3,7 @@ class RestaurantsController < ApplicationController
expose(:restaurants) { Restaurant.all }
expose(:restaurant)
- expose(:outings) { Outing.where(["created_at >= ?", Date.today]) }
+ expose(:outings) { Outing.today }
def new
end
View
12 app/models/outing.rb
@@ -1,10 +1,20 @@
class Outing < ActiveRecord::Base
- validates_uniqueness_of :restaurant_id
belongs_to :restaurant
belongs_to :user
has_many :outing_users
has_many :users, :through => :outing_users
+ validate_on_create :unique_today
+ validates_presence_of :restaurant_id
+
default_scope :include => :restaurant, :order => 'restaurants.name'
+ scope :today, lambda { where(["DATE(created_at) = ?", Date.today]) }
+ scope :for_restaurant, lambda { |restaurant| where(:restaurant_id => restaurant.id) }
+
+ def unique_today
+ if Outing.today.for_restaurant(restaurant).present?
+ errors.add(:base, "Looks like there's already a group there today")
+ end
+ end
end
View
2 features/user_creates_outing.feature
@@ -48,4 +48,4 @@ Feature: user creates outing
When I follow "Delicomb" within "#restaurants"
And I fill in "What time?" with "12:00 PM"
And I press "Create Outing"
- Then I should see "That outing has already been created!"
+ Then I should see "Looks like there's already a group there today"
View
31 spec/models/outing_spec.rb
@@ -1,19 +1,32 @@
require 'spec_helper'
describe Outing do
+ let(:restaurant) { Fabricate(:restaurant) }
+
it { should belong_to(:restaurant) }
it { should belong_to(:user) }
context "validations" do
- context "when an outing already exists for a restaurant" do
- subject do
- outing_1 = Outing.new :restaurant_id => 2
- outing_1.save
- outing_2 = Outing.new :restaurant_id => 2
- outing_2
- end
-
- it { should_not be_valid }
+
+ context "when a previous restaurant outing started in the past" do
+ let!(:outing_1) { Fabricate(:outing, :created_at => Time.now.yesterday, :restaurant => restaurant) }
+ let(:outing_2) { Fabricate(:outing, :restaurant => restaurant) }
+
+ specify { outing_2.should have(0).errors }
end
+
+ context "when a previous restaurant outing started today" do
+ let!(:outing_1) { Fabricate(:outing, :restaurant => restaurant) }
+ let(:outing_2) { Fabricate(:outing, :restaurant => restaurant) }
+
+ specify { outing_2.should have(1).error }
+ end
+
+ end
+
+ describe ".today" do
+ let!(:outing_1) { Fabricate(:outing, :created_at => Time.now.yesterday, :restaurant => restaurant) }
+ let(:outing_2) { Fabricate(:outing, :restaurant => restaurant) }
+ specify { Outing.today.should == [outing_2] }
end
end

0 comments on commit f7fe940

Please sign in to comment.
Something went wrong with that request. Please try again.