This commit is contained in:
HuangQi 2016-01-06 11:31:40 +08:00
parent 4d902ef9fe
commit 6ce5c98845
20 changed files with 216 additions and 28 deletions

View File

@ -84,16 +84,28 @@ class ArticlesController < ApplicationController
end end
def star def star
if current_user_can_star? @article
@result = {status: false, message: '', star_count: 0}
star = @article.article_stars.new user_id: @current_user.id
if star.save
@result[:star_count] = (@article.star_count || 0) + 1
@result[:status] = true
else
@result[:message] = '称赞失败'
end
respond_to do |format|
format.js
end
end
end end
protected protected
def check_current_user_is_admin def check_current_user_is_admin
redirect_to root_path unless (@current_user && @current_user.admin)
end end
def article def article
@article = Article.find params[:id]
end end
end end

View File

@ -60,21 +60,49 @@ class ArticlesController < ApplicationController
end end
def update def update
attributes = params.require(:article).permit(:title, :tags, :source, :content)
if params[:article].present? && params[:article][:category_id].present?
attributes[:category_id] = params[:article][:category_id]
elsif params[:article].present? && params[:article][:category_name].present?
category = Category.find_or_create params[:article][:category_name]
attributes[:category_id] = category.id
end
if @article.update_attributes attributes
redirect_to article_path(@article)
else
render 'edit'
end
end end
def destroy def destroy
if @article.destroy
redirect_to articles_path
else
flash[:error] = '删除失败'
redirect_to article_path(@article)
end
end end
def star def star
if current_user_can_star? @article
@result = {status: false, message: '', star_count: 0}
star = @article.article_stars.new user_id: @current_user.id
if star.save
@result[:star_count] = (@article.star_count || 0) + 1
@result[:status] = true
else
@result[:message] = '称赞失败'
end
respond_to do |format|
format.js
end
end
end end
protected protected
def check_current_user_is_admin def check_current_user_is_admin
redirect_to root_path unless (@current_user && @current_user.admin)
end end
def article def article

View File

@ -0,0 +1,25 @@
.article_content
%h3
- if article.source.empty?
[原创]
= link_to article.title, article_path(article), target: '_blank'
- get_tags(article).each do |tag|
%span.label.label-default
= tag
%p
%span.icon.icon-time(data-toggle="tooltip" data-placement="bottom" title="发表时间")
= article.updated_at.strftime('%Y-%m-%d')
%span.icon.icon-eye-open(data-toggle="tooltip" data-placement="bottom" title="浏览数")
= article.view_count || 0
%span.icon.icon-thumbs-up(data-toggle="tooltip" data-placement="bottom" title="称赞数")
= article.star_count || 0
%span.icon.icon-comments(data-toggle="tooltip" data-placement="bottom" title="评论数")
= article.comments_count || 0
- unless article.source.empty?
%p
转载:
= article.source
.row-fluid.article_des
%p
= article.content[0..300]

View File

View File

@ -0,0 +1,15 @@
.text-left.ui-sortable
%h4 文章分类
%ul.nav.bs-docs-sidenav
%li
= link_to articles_path, remote: true do
全部
%span.badge
= "#{get_articles_count}篇"
- get_categories.each do |category|
%li
= link_to articles_path(c: category.id), remote: true do
= category.name
%span.badge
= "#{category.articles_count || 0}篇"

View File

@ -0,0 +1,7 @@
- unless @articles.present?
.jumbotron
%h2 什么也没有~
- @articles.each do |article|
= render partial: 'articles/article', locals: {article: article}
= paginate @articles, remote: true if params[:controller] == 'articles'

View File

View File

@ -0,0 +1,18 @@
.panel.panel-dfault{id: "comment_#{comment.id}"}
.panel-body
.row
.col-md-1.text-center
%span.comment_avatar
= avatar comment.user
= comment.user.nickname
.comment_content.col-md-9{id: "comment_#{comment.id}_body"}
= raw markdown_parser(comment.content)
.row.comment_footer.text-right
%span.icon.icon-time(data-toggle="tooltip" data-placement="bottom" title="评论时间")
= "#{comment.updated_at.strftime('%Y-%m-%d %H:%M:%S')}"
- if current_user_can_edit_comment?(comment)
= link_to edit_article_comment_path(@article, comment), remote: true do
%span.icon.icon-edit
- if current_user_is_admin? || current_user_can_edit_comment?(comment)
= link_to article_comment_path(@article, comment), method: 'delete', remote: true, "data-confirm"=> '确定删除吗?' do
%span.icon.icon-trash

View File

View File

@ -0,0 +1,25 @@
= render partial: 'toolbar_upload_img', locals: {text_id: 'article_comment_content'}
.comment_form
- url = @comment.new_record? ? article_comments_path(@article) : article_comment_path(@article, @comment)
= form_for @comment, url: url, role: 'form', remote: true do |f|
.form-group
%ul.nav.nav-tabs#preview_tab{role: "tablist"}
%li.active
= link_to (@comment.new_record? ? '发表评论' : '编辑评论'), '#edit', role: 'tab', 'data-toggle'=>'tab'
%li
= link_to '预览', '#preview', role: 'tab', 'data-toggle'=>'tab'
.tab-content
.tab-pane.active#edit
= f.text_area :content, placeholder: '评论内容', class: 'form-control'
.tab-pane#preview
.form-group
= f.submit '提交', class: 'btn btn-success btn-lg btn-block'
:javascript
$(function(){
$('#preview_tab a[href="#preview"]').click(function(e){
e.preventDefault();
$(this).tab('show');
preview('article_comment_content', 'preview');
});
});

View File

@ -0,0 +1,7 @@
- unless @comments.present?
.jumbotron#no_comments
%h2 还没有评论~
- @comments.each do |comment|
= render partial: 'comment', locals: {comment: comment}
#paginate
= paginate @comments, remote: true

View File

View File

@ -0,0 +1,32 @@
= form_for :upload, url: upload_img_blogs_path, enctype: "multipart/form-data", class: 'form-horizontal', role: 'form', remote: true do |f|
.modal.fade#upload_modal
.modal-dialog
.modal-content
.modal-header
%button.close{"data-dismiss"=>"modal"}
%span{"aria-hidden"=>"true"}
&times;
%span.sr-only
Close
%h4 上传图片
.modal-body
.form-group.row.text-center
.error.text-center#error
%label.col-sm-2.control-label.text-right{for: "upload_img"} 图片:
.col-sm-8.text-left
= token_tag
= f.hidden_field :text_id, value: text_id
= f.file_field :img
.modal-footer
%button.btn.btn-default{"data-dismiss"=>"modal"} 关闭
= submit_tag '上传', class: 'btn btn-primary'
:javascript
$(function(){
new MarkdownToolbar($('##{text_id}'));
$('.mdt_button_image').unbind('click');
$('.mdt_button_image').click(function(){
$('#upload_modal').modal('show');
});
});

View File

@ -0,0 +1,4 @@
- if @result[:status]
:plain
$('#article_star').html(" #{@result[:star_count]}");
$('#article_star').parent('a').attr('href', 'javascript:void(0);').addClass('disabled');

View File

View File

@ -3,8 +3,8 @@ require 'articles_controller'
class ArticlesControllerTest < ActionController::TestCase class ArticlesControllerTest < ActionController::TestCase
setup do setup do
@controller = AriticlesController.new @controller = ArticlesController.new
@ariticle = Ariticle.new @ariticle = Article.new
@ariticle.title = 'title1' @ariticle.title = 'title1'
@ariticle.tags = 'tags1' @ariticle.tags = 'tags1'
@ariticle.source = 'source1' @ariticle.source = 'source1'
@ -19,14 +19,14 @@ class ArticlesControllerTest < ActionController::TestCase
end end
test "should show article" do test "should show article" do
get :show, id: @ariticle.id get :show, id: @article.id
assert_response :success assert_response :success
end end
test "should create ariticle" do test "should create article" do
post :create,:ariticle =>{:title=>'title1', :tags=>'tags1', :source=>'source', :content=>'content1'} post :create,:article =>{:title=>'title1', :tags=>'tags1', :source=>'source', :content=>'content1'}
assert_not_nil session[:ariticle_id] assert_not_nil session[:article_id]
assert_redirected_to article_path(@ariticle) assert_redirected_to article_path(@article)
end end
test "should destroy article" do test "should destroy article" do
@ -38,7 +38,7 @@ class ArticlesControllerTest < ActionController::TestCase
end end
test "destroy_wrong" do test "destroy_wrong" do
post :destroy, :ariticle=>{:username=>'title1'} post :destroy, :article=>{:username=>'title1'}
assert_response :success assert_response :success
assert_equal '删除失败', flash[:error] assert_equal '删除失败', flash[:error]
end end

View File

@ -1,15 +1,15 @@
require 'test_helper' require 'test_helper'
require 'articles_controller' require 'articles_controller'
class ArtilesControllerTest < ActionController::TestCase class ArticlesControllerTest < ActionController::TestCase
setup do setup do
@controller = AritilesController.new @controller = AriticlesController.new
@aritile = Aritile.new @ariticle = Ariticle.new
@aritile.title = 'title1' @ariticle.title = 'title1'
@aritile.tags = 'tags1' @ariticle.tags = 'tags1'
@aritile.source = 'source1' @ariticle.source = 'source1'
@aritile.content = 'content1' @ariticle.content = 'content1'
@aritile.save @ariticle.save
end end
test "should get index" do test "should get index" do
@ -19,13 +19,28 @@ class ArtilesControllerTest < ActionController::TestCase
end end
test "should show article" do test "should show article" do
get :show, id: @aritile.id get :show, id: @ariticle.id
assert_response :success assert_response :success
end end
test "should create aritile" do test "should create ariticle" do
post :create,:aritile =>{:title=>'title1', :tags=>'tags1', :source=>'source', :content=>'content1'} post :create,:ariticle =>{:title=>'title1', :tags=>'tags1', :source=>'source', :content=>'content1'}
assert_not_nil session[:aritile_id] assert_not_nil session[:ariticle_id]
assert_redirected_to article_path(@article) assert_redirected_to article_path(@ariticle)
end end
test "should destroy article" do
assert_difference('Article.count', -1) do
delete :destroy, id: @article.id
end
assert_redirected_to articles_path
end
test "destroy_wrong" do
post :destroy, :ariticle=>{:username=>'title1'}
assert_response :success
assert_equal '删除失败', flash[:error]
end
end end