Skip to content

Commit

Permalink
working around routing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
GBH committed Oct 22, 2013
1 parent 2735adb commit aa92652
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/blog/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion app/controllers/blog/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions app/views/admin/blog/blogs/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/blog/comments/_comment.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions app/views/admin/blog/partials/_navigation.html.haml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
- if @site.present? && [email protected]_record?
%li= active_link_to 'Blogs', admin_blogs_path(@site)
- 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)
2 changes: 2 additions & 0 deletions app/views/blog/posts/index.html.haml
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 2 additions & 0 deletions app/views/blog/posts/show.html.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
%h1 Blog Post

%h1= @post.title

.author
Expand Down
4 changes: 2 additions & 2 deletions lib/comfy_blog/routing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/controllers/blog/posts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ def setup
end

def test_get_index
get :index
get :serve
assert_response :success
assert_template :index
assert assigns(:posts)
assert_equal 1, assigns(:posts).size
end

def test_get_index_as_rss
get :index, :format => :rss
get :serve, :format => :rss
assert_response :success
assert_template :index
assert assigns(:posts)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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

Expand Down

0 comments on commit aa92652

Please sign in to comment.