My app/controllers/blog_controller.rb:
class BlogController < ApplicationController
require 'date'
caches_page :index, :show
def index
if (params[:population_categories])
@blog_posts = BlogPost.live.where(:population_category_ids =>
params[:population_categories]).latest.page(params[:page])
# @blog_posts = BlogPost.find(:all, :conditions => ['created_at >
? AND created_at < ?', b_dt.strftime("%Y-%m-%d %H:%M"),
e_dt.strftime("%Y-%m-%d %H:%M")]).latest.page(params[:page])
elsif (params[:month])
b_dt = DateTime.strptime("01/" + params[:month].to_s + "/" +
params[:year] + " 00:00", "%d/%m/%Y %H:%M")
e_dt = DateTime.strptime("01/" + (params[:month].to_i+1).to_s +
"/" + params[:year] + " 00:00", "%d/%m/%Y %H:%M")
@blog_posts = BlogPost.live.where(:created_at =>
b_dt.strftime("%Y-%m-%d %H:%M DESC")..e_dt.strftime("%Y-%m-%d %H:%M
DESC") ).latest.page(params[:page] )
#@blog_posts = @posts.group_by { |t| t.publication_date
.beginning_of_month }
else
@blog_posts =
BlogPost.live.keyword(params[:keyword]).latest.page(params[:page])
end
@posts_by_month = BlogPost.all.group_by { |post|
post.created_at.strftime("%m %Y") }
end
def show
@blog_post = BlogPost.where(:url_fragment =>
params[:url_fragment]).first
raise ActionController::RoutingError.new('Not Found') if
@blog_post.nil?
@blog_posts =
BlogPost.live.keyword(params[:keyword]).latest.page(params[:page])
@posts_by_month = BlogPost.all.group_by { |post|
post.created_at.strftime("%m %Y") }
end
def feed
@posts_feed = BlogPost.where(:content_state => 'live')
respond_to do |format|
format.atom { render layout: false }
end
end
end
app/controllers/month_posts_controler:
class MonthPostsController < ApplicationController
def display
@posts_by_month = BlogPost.all.group_by { |post|
post.created_at.strftime("%B") }
#@posts_by_month = BlogPost.all.each { |post|
post.created_at.strftime("December") }
end
def show
@posts = BlogPost.all.find(params[:id])
@posts = Post.includes(:comments).order("created_at DESC").limit(5)
end
end
app/controllers/post_controller:
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
format.atom
end
end
app/views/blog/index.html.erb
<div class='section white' id='blog_posts'>
<div class='section_content'>
<div class='section-left'>
<% @blog_posts.limit(3).each do |post| %>
<% @section_keywords += post.keywords_array %>
<%= render :partial => '/blog/index/blog_post', :locals =>
{:atom => post} %>
<% end %>
<div class="right-col">
<h2>ARCHIVES</h2>
<div class='posts_by_month'>
<ul>
<% archive_string = "" %>
<% date_value = "" %>
<% @posts_by_month.each do |monthname, posts| %>
<% date_value = (Date::MONTHNAMES[monthname[0..-6].to_i]) + "
" + (monthname[3..8]) %>
<% archive_string = "<li><a class='filter' href='/blog?month="
+ monthname[0..-6] + "&year=" + monthname[3..8] + "'>" + date_value +
"</a></li>" + archive_string %>
<% end %>
<%= raw archive_string %>
</ul>
</div>
</div>
<div class="right-col">
<div class="blogpost-rss">
<a class="rss-link" href="/feed.atom"></a>
<%= auto_discovery_link_tag(:rss, feed_url(:format => :atom)) %>
</div>
</div>
app/views/blog/show.html.erb:
<div class='section white blog_post blog_detail' id='blog_post'>
<div class='section_content'>
<div class="right-col">
<h2>ARCHIVES</h2>
<div class='posts_by_month'>
<ul>
<% archive_string = "" %>
<% date_value = "" %>
<% @posts_by_month.each do |monthname, posts| %>
<% date_value = (Date::MONTHNAMES[monthname[0..-6].to_i]) + " " +
(monthname[3..8]) %>
<% if(date_value == pub_date) %>
<% archive_string = "<li class='active'><a class='filter'
href='/blog?month=" + monthname[0..-6] + "&year=" + monthname[3..8] +
"'>" + date_value + "</a></li>" + archive_string %>
<% else %>
<% archive_string = "<li><a class='filter' href='/blog?month=" +
monthname[0..-6] + "&year=" + monthname[3..8] + "'>" + date_value +
"</a></li>" + archive_string %>
<% end %>
<% end %>
<%= raw archive_string %>
</ul>
</div>
</div>