From aa926526aafb0b418b3efd9c362a2a0e0f854a0b Mon Sep 17 00:00:00 2001 From: Oleg Date: Tue, 22 Oct 2013 16:02:05 -0400 Subject: [PATCH] working around routing issue --- .../admin/blog/posts_controller.rb | 2 +- app/controllers/blog/posts_controller.rb | 21 ++++++++++++++++++- app/views/admin/blog/blogs/index.html.haml | 3 +++ .../admin/blog/comments/_comment.html.haml | 3 ++- .../admin/blog/partials/_navigation.html.haml | 8 +++++-- app/views/blog/posts/index.html.haml | 2 ++ app/views/blog/posts/show.html.haml | 2 ++ lib/comfy_blog/routing.rb | 4 ++-- .../controllers/blog/posts_controller_test.rb | 10 ++++----- 9 files changed, 43 insertions(+), 12 deletions(-) diff --git a/app/controllers/admin/blog/posts_controller.rb b/app/controllers/admin/blog/posts_controller.rb index cfe8d3b..459358a 100644 --- a/app/controllers/admin/blog/posts_controller.rb +++ b/app/controllers/admin/blog/posts_controller.rb @@ -5,7 +5,7 @@ class Admin::Blog::PostsController < Admin::Blog::BaseController before_action :load_post, :only => [:edit, :update, :destroy] def index - @posts = @blog.posts.page(params[:page]) + @posts = @blog.posts.order(:published_at).page(params[:page]) end def new diff --git a/app/controllers/blog/posts_controller.rb b/app/controllers/blog/posts_controller.rb index c6bfdb0..14e3286 100644 --- a/app/controllers/blog/posts_controller.rb +++ b/app/controllers/blog/posts_controller.rb @@ -1,8 +1,27 @@ class Blog::PostsController < Blog::BaseController + + skip_before_action :load_blog, :only => [:serve] + + # due to fancy routing it's hard to say if we need show or index + # action. let's figure it out here. + def serve + # if there are more than one blog, blog_path is expected + if @cms_site.blogs.count >= 2 + params[:blog_path] = params.delete(:slug) if params[:blog_path].blank? + end + + load_blog + + if params[:slug].present? + show && render(:show) + else + index && render(:index) + end + end def index scope = if params[:year] - scope =@blog.posts.published.for_year(params[:year]) + scope = @blog.posts.published.for_year(params[:year]) params[:month] ? scope.for_month(params[:month]) : scope else @blog.posts.published diff --git a/app/views/admin/blog/blogs/index.html.haml b/app/views/admin/blog/blogs/index.html.haml index 96ab3aa..f988bc9 100644 --- a/app/views/admin/blog/blogs/index.html.haml +++ b/app/views/admin/blog/blogs/index.html.haml @@ -11,6 +11,9 @@ = link_to blog.label, edit_admin_blog_path(@site, blog) .item-meta = blog.identifier + %br + - blog_path = blog_posts_url(@site.path, blog.path) + = link_to blog_path, blog_path, :target => '_blank' %td .btn-group = link_to pluralize(blog.posts.count, 'Post'), admin_blog_posts_path(@site, blog), :class => 'btn btn-small btn-inverse' diff --git a/app/views/admin/blog/comments/_comment.html.haml b/app/views/admin/blog/comments/_comment.html.haml index 9751dbb..c215934 100644 --- a/app/views/admin/blog/comments/_comment.html.haml +++ b/app/views/admin/blog/comments/_comment.html.haml @@ -3,8 +3,9 @@ = link_to comment.post.title.truncate(25), admin_blog_comments_path(@site, @blog, :post_id => comment.post) %td.main = comment.author - = mail_to comment.email .item-meta + = mail_to comment.email + %br = comment.content %td = comment.created_at.to_s(:db) diff --git a/app/views/admin/blog/partials/_navigation.html.haml b/app/views/admin/blog/partials/_navigation.html.haml index d9dc891..99c8191 100644 --- a/app/views/admin/blog/partials/_navigation.html.haml +++ b/app/views/admin/blog/partials/_navigation.html.haml @@ -1,2 +1,6 @@ -- if @site.present? && !@site.new_record? - %li= active_link_to 'Blogs', admin_blogs_path(@site) \ No newline at end of file +- if @site.present? && @site.persisted? + %li= active_link_to 'Blogs', admin_blogs_path(@site) + - if @blog.present? && @blog.persisted? + %ul + %li= active_link_to 'Posts', admin_blog_posts_path(@site, @blog) + %li= active_link_to 'Comments', admin_blog_comments_path(@site, @blog) \ No newline at end of file diff --git a/app/views/blog/posts/index.html.haml b/app/views/blog/posts/index.html.haml index 96efe57..76598f3 100644 --- a/app/views/blog/posts/index.html.haml +++ b/app/views/blog/posts/index.html.haml @@ -1,3 +1,5 @@ +%h1 Blog Posts + - @posts.each do |post| %h1= link_to post.title, blog_post_path(@cms_site.path, @blog.path, post.slug) diff --git a/app/views/blog/posts/show.html.haml b/app/views/blog/posts/show.html.haml index 1694f17..acdbabb 100644 --- a/app/views/blog/posts/show.html.haml +++ b/app/views/blog/posts/show.html.haml @@ -1,3 +1,5 @@ +%h1 Blog Post + %h1= @post.title .author diff --git a/lib/comfy_blog/routing.rb b/lib/comfy_blog/routing.rb index a2c0197..8bfd343 100644 --- a/lib/comfy_blog/routing.rb +++ b/lib/comfy_blog/routing.rb @@ -30,8 +30,8 @@ def self.content(options = {}) o.get ':year/:month/:slug' => 'posts#show', :as => :posts_dated end post ':slug/comments' => 'comments#create', :as => :comments - get ':slug' => 'posts#show', :as => :post - get '/' => 'posts#index', :as => :posts + get ':slug' => 'posts#serve', :as => :post + get '/' => 'posts#serve', :as => :posts end end end diff --git a/test/controllers/blog/posts_controller_test.rb b/test/controllers/blog/posts_controller_test.rb index 302ecd3..28f658f 100644 --- a/test/controllers/blog/posts_controller_test.rb +++ b/test/controllers/blog/posts_controller_test.rb @@ -8,7 +8,7 @@ def setup end def test_get_index - get :index + get :serve assert_response :success assert_template :index assert assigns(:posts) @@ -16,7 +16,7 @@ def test_get_index end def test_get_index_as_rss - get :index, :format => :rss + get :serve, :format => :rss assert_response :success assert_template :index assert assigns(:posts) @@ -25,7 +25,7 @@ def test_get_index_as_rss def test_get_index_with_unpublished blog_posts(:default).update_column(:is_published, false) - get :index + get :serve assert_response :success assert_equal 0, assigns(:posts).size end @@ -51,7 +51,7 @@ def test_get_index_for_month_archive end def test_get_show - get :show, :slug => @post.slug + get :serve, :slug => @post.slug assert_response :success assert_template :show assert assigns(:post) @@ -60,7 +60,7 @@ def test_get_show def test_get_show_unpublished @post.update_attribute(:is_published, false) assert_exception_raised ComfortableMexicanSofa::MissingPage do - get :show, :slug => @post.slug + get :serve, :slug => @post.slug end end