Class: Blog::PostsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/blog/posts_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/blog/posts_controller.rb', line 22

def index
  scope = if params[:year]
    scope = @blog.posts.published.for_year(params[:year])
    params[:month] ? scope.for_month(params[:month]) : scope
  else
    @blog.posts.published
  end

  limit = ComfyBlog.config.posts_per_page
  respond_to do |format|
    format.html do
      @posts = scope.page(params[:page]).per(limit)
    end
    format.rss do
      @posts = scope.limit(limit)
    end
  end
end

#serveObject

due to fancy routing it’s hard to say if we need show or index action. let’s figure it out here.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/blog/posts_controller.rb', line 7

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

#showObject



41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/blog/posts_controller.rb', line 41

def show
  @post = if params[:slug] && params[:year] && params[:month]
    @blog.posts.published.where(:year => params[:year], :month => params[:month], :slug => params[:slug]).first!
  else
    @blog.posts.published.where(:slug => params[:slug]).first!
  end
  @comment = @post.comments.new

rescue ActiveRecord::RecordNotFound
  render :cms_page => '/404', :status => 404
end