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

Commit

Permalink
Introduced link posts
Browse files Browse the repository at this point in the history
  • Loading branch information
Aziz Light committed Sep 21, 2011
1 parent f1b3877 commit 5faf6db
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
32 changes: 21 additions & 11 deletions app/models/post.rb
Expand Up @@ -2,20 +2,22 @@
#
# Table name: posts
#
# id :integer not null, primary key
# title :string(255)
# body :text
# created_at :datetime
# updated_at :datetime
# user_id :integer
# draft :integer
# post_type :string(255)
# image :string(255)
# quote :string(255)
# id :integer not null, primary key
# title :string(255)
# body :text
# created_at :datetime
# updated_at :datetime
# user_id :integer
# draft :integer
# post_type :string(255)
# image :string(255)
# quote :string(255)
# quote_source :string(255)
# link :text
#

class Post < ActiveRecord::Base
attr_accessible :title, :body, :user_id, :draft, :post_type, :quote, :image, :quote_source
attr_accessible :title, :body, :user_id, :draft, :post_type, :quote, :image, :quote_source, :link

belongs_to :user

Expand All @@ -36,6 +38,10 @@ class Post < ActiveRecord::Base
:length => { :within => 1..255 }
end

with_options :if => :link_post_type? do |post|
post.validates :link, :presence => true
end

validates :user_id, :presence => true,
:numericality => {
:only_integer => true,
Expand Down Expand Up @@ -101,4 +107,8 @@ def image_post_type?
def quote_post_type?
post_type == "quote"
end

def link_post_type?
post_type == "link"
end
end
5 changes: 5 additions & 0 deletions db/migrate/20110921114256_add_link_to_posts.rb
@@ -0,0 +1,5 @@
class AddLinkToPosts < ActiveRecord::Migration
def change
add_column :posts, :link, :text
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110921111653) do
ActiveRecord::Schema.define(:version => 20110921114256) do

create_table "posts", :force => true do |t|
t.string "title"
Expand All @@ -24,6 +24,7 @@
t.string "image"
t.string "quote"
t.string "quote_source"
t.text "link"
end

create_table "users", :force => true do |t|
Expand Down
16 changes: 15 additions & 1 deletion spec/models/post_spec.rb
Expand Up @@ -25,7 +25,8 @@
:user_id => 1,
:draft => 0,
:post_type => "text",
:quote => "Aziz, Light!"
:quote => "Aziz, Light!",
:link => "http://www.example.com/"
}
end

Expand Down Expand Up @@ -88,6 +89,7 @@
post.should_not be_valid
end

# FIXME: Split that test
it "should require a valid post type" do
valid_post_types = %w[text quote link audio video]
valid_post_types.each do |valid_post_type|
Expand Down Expand Up @@ -115,5 +117,17 @@
post.should_not be_valid
end
end

describe "link" do
it "should require a link for posts of type link" do
post = Post.new(@attr.merge(:link => "", :post_type => "link"))
post.should_not be_valid
end

it "should not require a link for posts of type other than link" do
post = Post.new(@attr.merge(:link => "", :post_type => "text"))
post.should be_valid
end
end
end
end

0 comments on commit 5faf6db

Please sign in to comment.