diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 0998550..e0d9688 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -84,16 +84,28 @@ class ArticlesController < ApplicationController end 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 protected def check_current_user_is_admin - + redirect_to root_path unless (@current_user && @current_user.admin) end def article - + @article = Article.find params[:id] end end diff --git a/app/controllers/articles_controller.rb~ b/app/controllers/articles_controller.rb~ index a818945..e627ba1 100644 --- a/app/controllers/articles_controller.rb~ +++ b/app/controllers/articles_controller.rb~ @@ -60,21 +60,49 @@ class ArticlesController < ApplicationController end 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 def destroy - + if @article.destroy + redirect_to articles_path + else + flash[:error] = '删除失败' + redirect_to article_path(@article) + end end 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 protected def check_current_user_is_admin - + redirect_to root_path unless (@current_user && @current_user.admin) end def article diff --git a/app/views/articles/_article.html.haml b/app/views/articles/_article.html.haml new file mode 100644 index 0000000..0f2e992 --- /dev/null +++ b/app/views/articles/_article.html.haml @@ -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] diff --git a/app/views/articles/_article.html.haml~ b/app/views/articles/_article.html.haml~ new file mode 100644 index 0000000..e69de29 diff --git a/app/views/articles/_article_sidebar.html.haml b/app/views/articles/_article_sidebar.html.haml new file mode 100644 index 0000000..ad6ffcf --- /dev/null +++ b/app/views/articles/_article_sidebar.html.haml @@ -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}篇" + diff --git a/app/views/articles/_article_sidebar.html.haml~ b/app/views/articles/_article_sidebar.html.haml~ new file mode 100644 index 0000000..e69de29 diff --git a/app/views/articles/_articles.html.haml b/app/views/articles/_articles.html.haml new file mode 100644 index 0000000..304cde5 --- /dev/null +++ b/app/views/articles/_articles.html.haml @@ -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' diff --git a/app/views/articles/_articles.html.haml~ b/app/views/articles/_articles.html.haml~ new file mode 100644 index 0000000..e69de29 diff --git a/app/views/articles/_comment.html.haml b/app/views/articles/_comment.html.haml new file mode 100644 index 0000000..762464f --- /dev/null +++ b/app/views/articles/_comment.html.haml @@ -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 diff --git a/app/views/articles/_comment.html.haml~ b/app/views/articles/_comment.html.haml~ new file mode 100644 index 0000000..e69de29 diff --git a/app/views/articles/_comment_form.html.haml b/app/views/articles/_comment_form.html.haml new file mode 100644 index 0000000..ba889da --- /dev/null +++ b/app/views/articles/_comment_form.html.haml @@ -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'); + }); + }); diff --git a/app/views/articles/_comment_form.html.haml~ b/app/views/articles/_comment_form.html.haml~ new file mode 100644 index 0000000..e69de29 diff --git a/app/views/articles/_comments.html.haml b/app/views/articles/_comments.html.haml new file mode 100644 index 0000000..701b7bf --- /dev/null +++ b/app/views/articles/_comments.html.haml @@ -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 diff --git a/app/views/articles/_comments.html.haml~ b/app/views/articles/_comments.html.haml~ new file mode 100644 index 0000000..e69de29 diff --git a/app/views/articles/_toolbar_upload_img.html.haml b/app/views/articles/_toolbar_upload_img.html.haml new file mode 100644 index 0000000..97e2aa4 --- /dev/null +++ b/app/views/articles/_toolbar_upload_img.html.haml @@ -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"} + × + %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'); + }); + }); diff --git a/app/views/articles/_toolbar_upload_img.html.haml~ b/app/views/articles/_toolbar_upload_img.html.haml~ new file mode 100644 index 0000000..e69de29 diff --git a/app/views/articles/star.js.haml b/app/views/articles/star.js.haml new file mode 100644 index 0000000..0a3fa15 --- /dev/null +++ b/app/views/articles/star.js.haml @@ -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'); diff --git a/app/views/articles/star.js.haml~ b/app/views/articles/star.js.haml~ new file mode 100644 index 0000000..e69de29 diff --git a/test/controllers/articles_controller_test.rb b/test/controllers/articles_controller_test.rb index bb72ffa..a66356a 100644 --- a/test/controllers/articles_controller_test.rb +++ b/test/controllers/articles_controller_test.rb @@ -3,8 +3,8 @@ require 'articles_controller' class ArticlesControllerTest < ActionController::TestCase setup do - @controller = AriticlesController.new - @ariticle = Ariticle.new + @controller = ArticlesController.new + @ariticle = Article.new @ariticle.title = 'title1' @ariticle.tags = 'tags1' @ariticle.source = 'source1' @@ -19,14 +19,14 @@ class ArticlesControllerTest < ActionController::TestCase end test "should show article" do - get :show, id: @ariticle.id + get :show, id: @article.id assert_response :success end - test "should create ariticle" do - post :create,:ariticle =>{:title=>'title1', :tags=>'tags1', :source=>'source', :content=>'content1'} - assert_not_nil session[:ariticle_id] - assert_redirected_to article_path(@ariticle) + test "should create article" do + post :create,:article =>{:title=>'title1', :tags=>'tags1', :source=>'source', :content=>'content1'} + assert_not_nil session[:article_id] + assert_redirected_to article_path(@article) end test "should destroy article" do @@ -38,7 +38,7 @@ class ArticlesControllerTest < ActionController::TestCase end test "destroy_wrong" do - post :destroy, :ariticle=>{:username=>'title1'} + post :destroy, :article=>{:username=>'title1'} assert_response :success assert_equal '删除失败', flash[:error] end diff --git a/test/controllers/articles_controller_test.rb~ b/test/controllers/articles_controller_test.rb~ index 889cea5..bb72ffa 100644 --- a/test/controllers/articles_controller_test.rb~ +++ b/test/controllers/articles_controller_test.rb~ @@ -1,15 +1,15 @@ require 'test_helper' require 'articles_controller' -class ArtilesControllerTest < ActionController::TestCase +class ArticlesControllerTest < ActionController::TestCase setup do - @controller = AritilesController.new - @aritile = Aritile.new - @aritile.title = 'title1' - @aritile.tags = 'tags1' - @aritile.source = 'source1' - @aritile.content = 'content1' - @aritile.save + @controller = AriticlesController.new + @ariticle = Ariticle.new + @ariticle.title = 'title1' + @ariticle.tags = 'tags1' + @ariticle.source = 'source1' + @ariticle.content = 'content1' + @ariticle.save end test "should get index" do @@ -19,13 +19,28 @@ class ArtilesControllerTest < ActionController::TestCase end test "should show article" do - get :show, id: @aritile.id + get :show, id: @ariticle.id assert_response :success end - test "should create aritile" do - post :create,:aritile =>{:title=>'title1', :tags=>'tags1', :source=>'source', :content=>'content1'} - assert_not_nil session[:aritile_id] - assert_redirected_to article_path(@article) + test "should create ariticle" do + post :create,:ariticle =>{:title=>'title1', :tags=>'tags1', :source=>'source', :content=>'content1'} + assert_not_nil session[:ariticle_id] + assert_redirected_to article_path(@ariticle) 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