Skip to content

Instantly share code, notes, and snippets.

@tors
Forked from woods/chapter.rb
Created May 27, 2010 15:32
Show Gist options
  • Save tors/415942 to your computer and use it in GitHub Desktop.
Save tors/415942 to your computer and use it in GitHub Desktop.
class Chapter < ActiveRecord::Base
validates :subdomain,
:presence => true,
:uniqueness => true,
:length => { :maximum => 63 }
validates_format_of :subdomain, :with => /^[a-z0-9-]+$/i, :message => 'can only contain letters, numbers, and dashes'
validates_format_of :subdomain, :without => /^-/, :message => 'can not start with a dash'
validates_format_of :subdomain, :without => /-$/, :message => 'can not end with a dash'
validates_format_of :subdomain, :without => /--/, :message => 'can not contain two consecutive dashes'
end
# == Schema Information
#
# Table name: chapters
#
# id :integer not null, primary key
# name :string(255) not null
# subdomain :string(255) not null
# created_at :datetime
# updated_at :datetime
#
require 'spec_helper'
describe Chapter do
subject { Chapter.make }
it { should respond_to(:name) }
it { should respond_to(:subdomain) }
describe "#subdomain" do
context 'that is not present' do
before { subject.subdomain = nil }
it { should_not be_valid }
context 'its errors' do
before { subject.valid? }
it { subject.errors[:subdomain].should include("can't be blank") }
end
end
context 'that is not unique' do
before { subject.subdomain = Chapter.make.subdomain }
it { should_not be_valid }
context 'its errors' do
before { subject.valid? }
it { subject.errors[:subdomain].should include("has already been taken") }
end
end
context 'with an invalid value' do
context 'such as one that begins with a dash' do
before { subject.subdomain = '-lions' }
it { should_not be_valid }
context 'its errors' do
before { subject.valid? }
it { subject.errors[:subdomain].should include("can not start with a dash") }
end
end
context 'such as one that ends with a dash' do
before { subject.subdomain = 'lions-' }
it { should_not be_valid }
context 'its errors' do
before { subject.valid? }
it { subject.errors[:subdomain].should include("can not end with a dash") }
end
end
context 'such as one that includes two consecutive dashes' do
before { subject.subdomain = 'good--times' }
it { should_not be_valid }
context 'its errors' do
before { subject.valid? }
it { subject.errors[:subdomain].should include("can not contain two consecutive dashes") }
end
end
context 'such as one that includes a dot' do
before { subject.subdomain = 'good.times' }
it { should_not be_valid }
context 'its errors' do
before { subject.valid? }
it { subject.errors[:subdomain].should include("can only contain letters, numbers, and dashes") }
end
end
context 'such as one that is more than 63 characters long' do
before { subject.subdomain = 'a' * 64 }
it { should_not be_valid }
context 'its errors' do
before { subject.valid? }
it { subject.errors[:subdomain].should include("is too long (maximum is 63 characters)") }
end
end
end
context 'with a valid value' do
context 'such as "lions-2"' do
before { subject.subdomain = 'lions-2' }
it { should be_valid }
end
context 'such as "2climbers"' do
before { subject.subdomain = '2climbers' }
it { should be_valid }
end
context 'such as "a"' do
before { subject.subdomain = 'a' }
it { should be_valid }
end
end
end
end
# == Schema Information
#
# Table name: chapters
#
# id :integer not null, primary key
# name :string(255) not null
# subdomain :string(255) not null
# created_at :datetime
# updated_at :datetime
#
woods@beidleheimer ~/src/git/bni (chapters) $ rake spec
(in /Users/woods/src/git/bni)
NOTICE: CREATE TABLE will create implicit sequence "chapters_id_seq" for serial column "chapters.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "chapters_pkey" for table "chapters"
Chapter
should respond to #name
should respond to #subdomain
#subdomain
that is not present
should not be valid
its errors
should include "can't be blank"
that is not unique
should not be valid
its errors
should include "has already been taken"
with an invalid value
such as one that begins with a dash
should not be valid
its errors
should include "can not start with a dash"
such as one that ends with a dash
should not be valid
its errors
should include "can not end with a dash"
such as one that includes two consecutive dashes
should not be valid
its errors
should include "can not contain two consecutive dashes"
such as one that includes a dot
should not be valid
its errors
should include "can only contain letters, numbers, and dashes"
such as one that is more than 63 characters long
should not be valid
its errors
should include "is too long (maximum is 63 characters)"
with a valid value
such as "lions-2"
should be valid
such as "2climbers"
should be valid
such as "a"
should be valid
Finished in 0.30498 seconds
19 examples, 0 failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment