Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
adding basic validations of name presence
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Oct 19, 2008
1 parent 00f7a2f commit 70234ea
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 22 deletions.
2 changes: 2 additions & 0 deletions app/models/code_snippet.rb
Expand Up @@ -3,6 +3,8 @@ class CodeSnippet < ActiveRecord::Base

belongs_to :project

validates_presence_of :name, :content, :language

def to_s
name
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/color_swatch.rb
@@ -1,3 +1,5 @@
class ColorSwatch < ActiveRecord::Base
belongs_to :palette

validates_presence_of :hex
end
2 changes: 2 additions & 0 deletions app/models/link.rb
Expand Up @@ -3,6 +3,8 @@ class Link < ActiveRecord::Base

belongs_to :project

validates_presence_of :name, :url

def to_s
name
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/note.rb
Expand Up @@ -3,6 +3,8 @@ class Note < ActiveRecord::Base

belongs_to :project

validates_presence_of :name, :content

def to_s
name
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/palette.rb
Expand Up @@ -6,6 +6,8 @@ class Palette < ActiveRecord::Base

after_save :save_color_swatches

validates_presence_of :name

def to_s
name
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/photo.rb
Expand Up @@ -3,6 +3,8 @@ class Photo < ActiveRecord::Base

belongs_to :project

validates_presence_of :name

has_attached_file :image, :styles => { :small => "150x150>", :tiny => "64x64>" },
:url => "/assets/photos/:id/:style/:basename.:extension",
:path => "#{RAILS_ROOT}/public/assets/photos/:id/:style/:basename.:extension"
Expand Down
2 changes: 2 additions & 0 deletions app/models/project.rb
Expand Up @@ -10,6 +10,8 @@ class Project < ActiveRecord::Base

generate_unique :token

validates_presence_of :name

def to_s
name
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/screenshot.rb
Expand Up @@ -3,6 +3,8 @@ class Screenshot < ActiveRecord::Base

belongs_to :project

validates_presence_of :name

def to_s
name
end
Expand Down
46 changes: 46 additions & 0 deletions spec/factories.rb
Expand Up @@ -6,7 +6,53 @@
end

Factory.define :project do |f|
f.user { |c| c.association(:user) }
f.name 'Foo Bar'
f.description 'Lorem'
f.last_activity_at Time.now
end

Factory.define :code_snippet do |f|
f.project { |c| c.association(:project) }
f.name 'Hello, World'
f.content 'puts "Hello, World"'
f.language 'Ruby'
end

Factory.define :palette do |f|
f.project { |c| c.association(:project) }
f.name 'Color Theme'
end

Factory.define :color_swatch do |f|
f.palette { |c| c.association(:palette) }
f.hex '#FFF'
end

Factory.define :link do |f|
f.project { |c| c.association(:project) }
f.name 'Railscasts'
f.url 'http://railscasts.com'
f.description 'Free Ruby on Rails Screencasts'
end

Factory.define :note do |f|
f.project { |c| c.association(:project) }
f.name 'Foo'
f.content 'This is a note'
end

Factory.define :photo do |f|
f.project { |c| c.association(:project) }
f.name 'Me!'
f.image_file_name 'me.jpg'
f.image_content_type 'image/jpeg'
f.image_file_size '986123'
f.image_updated_at Time.now
end

Factory.define :screenshot do |f|
f.project { |c| c.association(:project) }
f.name 'Railscasts Home'
f.source_url 'http://railscasts.com'
end
16 changes: 14 additions & 2 deletions spec/models/code_snippet_spec.rb
@@ -1,7 +1,19 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe CodeSnippet do
it "should be valid" do
CodeSnippet.new.should be_valid
it "should be valid from factory" do
Factory.build(:code_snippet).should be_valid
end

it "should require name" do
Factory.build(:code_snippet, :name => '').should have(1).error_on(:name)
end

it "should require content" do
Factory.build(:code_snippet, :content => '').should have(1).error_on(:content)
end

it "should require language" do
Factory.build(:code_snippet, :language => '').should have(1).error_on(:language)
end
end
8 changes: 6 additions & 2 deletions spec/models/color_swatch_spec.rb
@@ -1,7 +1,11 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe ColorSwatch do
it "should be valid" do
ColorSwatch.new.should be_valid
it "should be valid from factory" do
Factory.build(:color_swatch).should be_valid
end

it "should require hex" do
Factory.build(:color_swatch, :hex => '').should have(1).error_on(:hex)
end
end
12 changes: 10 additions & 2 deletions spec/models/link_spec.rb
@@ -1,7 +1,15 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Link do
it "should be valid" do
Link.new.should be_valid
it "should be valid from factory" do
Factory.build(:link).should be_valid
end

it "should require name" do
Factory.build(:link, :name => '').should have(1).error_on(:name)
end

it "should require url" do
Factory.build(:link, :url => '').should have(1).error_on(:url)
end
end
12 changes: 10 additions & 2 deletions spec/models/note_spec.rb
@@ -1,7 +1,15 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Note do
it "should be valid" do
Note.new.should be_valid
it "should be valid from factory" do
Factory.build(:note).should be_valid
end

it "should require name" do
Factory.build(:note, :name => '').should have(1).error_on(:name)
end

it "should require content" do
Factory.build(:note, :content => '').should have(1).error_on(:content)
end
end
20 changes: 12 additions & 8 deletions spec/models/palette_spec.rb
@@ -1,23 +1,27 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Palette do
it "should be valid" do
Palette.new.should be_valid
it "should be valid from factory" do
Factory.build(:palette).should be_valid
end

it "should require name" do
Factory.build(:palette, :name => '').should have(1).error_on(:name)
end

it "should have color hex getter/seter method" do
palette = Palette.new(:color_hex_1 => 'foo', :color_hex_5 => 'bar')
palette = Factory.build(:palette, :color_hex_1 => 'foo', :color_hex_5 => 'bar')
palette.color_hex_1.should == 'foo'
palette.color_hex_5.should == 'bar'
end

it "should save color swatch on create" do
palette = Palette.create!(:color_hex_2 => 'foo', :color_hex_4 => 'bar')
palette = Factory(:palette, :color_hex_2 => 'foo', :color_hex_4 => 'bar')
palette.color_swatches(true).should have(2).records
end

it "should update or create existing color swatches" do
palette = Palette.create!(:color_hex_2 => 'foo', :color_hex_4 => 'bar')
palette = Factory(:palette, :color_hex_2 => 'foo', :color_hex_4 => 'bar')
palette.color_hex_1 = 'red'
palette.color_hex_2 = 'green'
palette.color_hex_3 = 'blue'
Expand All @@ -26,21 +30,21 @@
end

it "should destroy color hexes which aren't mentioned" do
palette = Palette.create!(:color_hex_1 => 'foo', :color_hex_3 => 'bar')
palette = Factory(:palette, :color_hex_1 => 'foo', :color_hex_3 => 'bar')
palette.color_hex_1 = 'red'
palette.save
palette.color_swatches(true).map(&:hex).should == %w[red]
end

it "should remember color hexes across reload (get from color swatch)" do
palette = Palette.create!(:color_hex_1 => 'foo', :color_hex_5 => 'bar')
palette = Factory(:palette, :color_hex_1 => 'foo', :color_hex_5 => 'bar')
palette.reload
palette.color_hex_1.should == 'foo'
palette.color_hex_2.should == 'bar'
end

it "should not clear color hexes when saving without updating colors" do
palette = Palette.create!(:color_hex_1 => 'foo', :color_hex_5 => 'bar')
palette = Factory(:palette, :color_hex_1 => 'foo', :color_hex_5 => 'bar')
palette.save
palette.color_hex_1.should == 'foo'
palette.color_hex_2.should == 'bar'
Expand Down
8 changes: 6 additions & 2 deletions spec/models/photo_spec.rb
@@ -1,7 +1,11 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Photo do
it "should be valid" do
Photo.new.should be_valid
it "should be valid from factory" do
Factory.build(:photo).should be_valid
end

it "should require name" do
Factory.build(:photo, :name => '').should have(1).error_on(:name)
end
end
8 changes: 6 additions & 2 deletions spec/models/project_spec.rb
@@ -1,8 +1,12 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Project do
it "should be valid" do
Project.new.should be_valid
it "should be valid from factory" do
Factory.build(:project).should be_valid
end

it "should require name" do
Factory.build(:project, :name => '').should have(1).error_on(:name)
end

it "should generate a unique token" do
Expand Down
8 changes: 6 additions & 2 deletions spec/models/screenshot_spec.rb
@@ -1,8 +1,12 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Screenshot do
it "should be valid" do
Screenshot.new.should be_valid
it "should be valid from factory" do
Factory.build(:screenshot).should be_valid
end

it "should require name" do
Factory.build(:screenshot, :name => '').should have(1).error_on(:name)
end

it "should use thumbalizr for image url" do
Expand Down

0 comments on commit 70234ea

Please sign in to comment.