diff --git a/Gemfile b/Gemfile index f57f076..caed57c 100644 --- a/Gemfile +++ b/Gemfile @@ -39,4 +39,5 @@ end group :test, :development do gem "rspec-rails", ">= 2.8.1" + gem 'pry-rails' end diff --git a/app/controllers/admin/posts_controller.rb b/app/controllers/admin/posts_controller.rb index 8898ba7..cba746e 100644 --- a/app/controllers/admin/posts_controller.rb +++ b/app/controllers/admin/posts_controller.rb @@ -32,7 +32,7 @@ class Admin::PostsController < ApplicationController md = Redcarpet::Markdown.new(rd, :autolink=>true) render :text => md.render(text) end - + def post_params params.require(:post).permit(:title, :content, :type) end diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index 0799134..960d8ad 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb @@ -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 @@ -22,6 +12,8 @@ class BlogsController < ApplicationController end def show + @post = Post.find(params[:id]) + @comments = Comment.all.where(post_id: @post.id) end private diff --git a/app/models/comment.rb b/app/models/comment.rb index 4915691..eb912b6 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 diff --git a/app/models/label.rb b/app/models/label.rb new file mode 100644 index 0000000..9fab876 --- /dev/null +++ b/app/models/label.rb @@ -0,0 +1,10 @@ +class Label + include Mongoid::Document + include Mongoid::Timestamps + + field :type, :type => String + + has_and_belongs_to_many :post + + validates :type, presence: true +end diff --git a/app/models/post.rb b/app/models/post.rb index b7d4bec..c1d3381 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -12,6 +12,7 @@ class Post field :visited_count, :type=>Integer, :default=>0 has_many :comments + has_and_belongs_to_many :labels validates :title, :presence=>true, :uniqueness=> true validates :content, :presence=>true, :length => { :minimum=> 30 } @@ -28,4 +29,11 @@ class Post self.save self.visited_count end + + def sub_content + rd = Redcarpet::Render::HTML.new(:hard_wrap=>true) + md = Redcarpet::Markdown.new(rd, :autolink=>true) + sub_cont = md.render(self.content) + HTML_Truncator.truncate(sub_cont,100) + end end diff --git a/app/views/blogs/index.html.slim b/app/views/blogs/index.html.slim index 5ae11b1..bab0258 100644 --- a/app/views/blogs/index.html.slim +++ b/app/views/blogs/index.html.slim @@ -1,37 +1,26 @@ .row .small-12.large-8.columns - h2.blog-title 科学是什么 + h2.blog-title #{@newest.title} p.ptag | 分类: - span 技术 + span #{@newest.type} p.ptag | 标签: - span 生活, 感悟 + span #{@newest.labels.collect{ |label| label.name}.join(', ')} .content - p 没错, 科学只是哲学的一个部分, 所以此书给我最大的收获是, 科学只是一种实用主义. 只是这种实用主义更纯粹些: - p - | 1. 任何公理都值得怀疑 - | 2. 能够验证与重复 + == @newest.sub_content - p 瞧, 第 2 点的感觉就是说, 太阳一直东方升起, 这就是科学, 因为我们一直在重复这个结论. 但是, 我们知道, 1000万年后, 我们可能就再也看不到太阳了, 所以这个结论还需要修正. 这就是科学的本质. - - p 再举个历史的例子: - - p - | 1. 地球是平的 - | 2. 地球是圆的 - | 3. 地球是椭圆的 - p.read-more 阅读全文 >> + = link_to "阅读全文 >>", blog_path(@newest) p.published-at 发表于 2014-2-12 h4.recent-title RECENT ul.recent-content - li 你是我的朋友 - li 该更新装备了 + - @recent.each do |re| + li = link_to "#{re.title}",blog_path(re) .large-3.columns.large-offset-1 h4 WELCOME hr - p 我是李亚飞, WinDy 是我的网名, 又名风一样的男子. + p 我是李亚飞, WinDy 是我的网名. h4 ABOUTME hr diff --git a/spec/controllers/blogs_controller_spec.rb b/spec/controllers/blogs_controller_spec.rb index fe7bd08..5574f85 100644 --- a/spec/controllers/blogs_controller_spec.rb +++ b/spec/controllers/blogs_controller_spec.rb @@ -3,22 +3,62 @@ 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 + + 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! + b = Comment.new(name: '2',content: 'iloveyou',email: 'liuzhen@.com') + b.post = post + b.save! + label = Label.new(type: '生活') + post.labels << label + post.save! + 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 diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index ee00a23..d632891 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -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 diff --git a/spec/models/label_spec.rb b/spec/models/label_spec.rb new file mode 100644 index 0000000..45b9dde --- /dev/null +++ b/spec/models/label_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Label do + +end