From b36619b3d11ea16c045ddf9b64e82b46ae7f64c0 Mon Sep 17 00:00:00 2001 From: windy Date: Sat, 29 Mar 2014 23:42:30 +0800 Subject: [PATCH] label added, index modified --- app/controllers/admin/posts_controller.rb | 2 +- app/models/label.rb | 10 ++++++++++ app/models/post.rb | 12 ++++++++++-- app/views/blogs/index.html.slim | 23 ++++++----------------- spec/controllers/blogs_controller_spec.rb | 20 ++++++++++++++++++++ spec/models/label_spec.rb | 5 +++++ 6 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 app/models/label.rb create mode 100644 spec/models/label_spec.rb 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/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 7f83daa..c1d3381 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -10,9 +10,10 @@ class Post field :content, :type => String field :type, :type=> String 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 } validates :type, :presence=>true, :inclusion => { :in => [ TECH, LIFE, CREATOR ] } @@ -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 5e8f3b7..bab0258 100644 --- a/app/views/blogs/index.html.slim +++ b/app/views/blogs/index.html.slim @@ -6,32 +6,21 @@ 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 cc3b3d3..5574f85 100644 --- a/spec/controllers/blogs_controller_spec.rb +++ b/spec/controllers/blogs_controller_spec.rb @@ -41,4 +41,24 @@ describe BlogsController do 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/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