添加comment表

This commit is contained in:
windy 2014-03-29 21:59:15 +08:00
parent 27adb4e47e
commit 5800f6f31a
7 changed files with 54 additions and 30 deletions

View File

@ -39,4 +39,5 @@ end
group :test, :development do
gem "rspec-rails", ">= 2.8.1"
gem 'pry-rails'
end

View File

@ -1,18 +1,8 @@
# encoding : utf-8
class BlogsController < ApplicationController
def index
type = params[:type]
page = params[:page].to_i
page = 1 if params[:page].nil?
max = 3
if type or type == ""
@posts = Post.paginate( :page=>page, :limit=> max ).where(type: map[type]).order(:created_at => :desc )
else
@posts = Post.paginate( :page=>page, :limit=> max ).all.order(:created_at => :desc )
end
@page = page
@has_old = @posts.to_a.size == max
@has_new = ! ( page == 1 )
@newest = Post.desc(:created_at).to_a.first
@recent = Post.desc(:created_at).to_a[1..2]
end
def rss
@ -23,6 +13,7 @@ class BlogsController < ApplicationController
def show
@post = Post.find(params[:id])
@comments = Comment.all.where(post_id: @post.id)
end
private

View File

@ -1,6 +1,14 @@
class Comment
include Mongoid::Document
include Mongoid::Timestamps
field :name, :type => String
field :content, :type => String
field :email, :type=>String
belongs_to :post
validates :name, presence: true
validates :email, confirmation: true,:format => /@/
validates :content, presence: true
end

View File

@ -10,9 +10,9 @@ class Post
field :content, :type => String
field :type, :type=> String
field :visited_count, :type=>Integer, :default=>0
has_many :comments
validates :title, :presence=>true, :uniqueness=> true
validates :content, :presence=>true, :length => { :minimum=> 30 }
validates :type, :presence=>true, :inclusion => { :in => [ TECH, LIFE, CREATOR ] }

View File

@ -1,9 +1,9 @@
.row
.small-12.large-8.columns
h2.blog-title 科学是什么
h2.blog-title #{@newest.title}
p.ptag
| 分类:
span 技术
span #{@newest.type}
p.ptag
| 标签:
span 生活, 感悟

View File

@ -3,22 +3,42 @@ require 'spec_helper'
describe BlogsController do
it 'index should get by order desc' do
a = Post.new(title: '123', content: '123'*20, type: Post::TECH)
a = Post.new(title: 'a123', content: '123'*20, type: Post::TECH)
a.save!
sleep 1
b = Post.new(title: '1234', content: '123'*20,type: Post::TECH)
b = Post.new(title: 'b1234', content: '123'*20,type: Post::TECH)
b.save!
c = Post.new(title: 'c1234', content: '123'*20,type: Post::TECH)
c.save!
d = Post.new(title: 'd1234', content: '123'*20,type: Post::TECH)
d.save!
a.update(title: 'aaa')
get :index
assigns[:posts][1].title.should == a.title
assigns[:newest].title.should == d.title
assigns[:recent][0].title.should == c.title
assigns[:recent][1].title.should == b.title
end
it "index with label should get by order desc" do
a = Post.new(title: '123', content: '123'*20, type: Post::TECH)
it 'test show method' do
post = Post.new(title: 'a123', content: '123'*20, type: Post::TECH)
post.save!
a = Comment.new(name: '1',content: '2432423',email: '22@.com')
a.post = post
a.save!
sleep 1
b = Post.new(title: '1234', content: '123'*20,type: Post::TECH)
b = Comment.new(name: '2',content: 'iloveyou',email: 'liuzhen@.com')
b.post = post
b.save!
get :index, :type=> "tech"
assigns[:posts][1].title.should == a.title
get :show, id: post.id
assigns[:comments][0].name.should == '1'
assigns[:comments][0].content.should == '2432423'
assigns[:comments][0].email.should == '22@.com'
assigns[:comments][1].name.should == '2'
assigns[:comments][1].content.should == 'iloveyou'
assigns[:comments][1].email.should == 'liuzhen@.com'
end
end

View File

@ -1,10 +1,14 @@
require 'spec_helper'
describe Comment do
it "comment should not blank" do
a = Comment.new
a.save.should == false
a = Comment.new(content: '11')
it 'test comment' do
a = Comment.new(name: '1',content: '2432423',email: '22@.com')
a.save.should == true
b = Comment.new(content: '2432ddd423',email: '22@.com')
b.save.should == false
b = Comment.new(name: '2', email: '22@.com')
b.save.should == false
b = Comment.new(name: '2', content: '2432ddd423',email: '22.com')
b.save.should == false
end
end