Skip to content

Commit

Permalink
add show posts by month
Browse files Browse the repository at this point in the history
  • Loading branch information
IceskYsl committed Jul 1, 2012
1 parent 716310d commit 2144cb8
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/assets/stylesheets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ h1, h2, h3, h4 { font-family: Arial, Helvetica, sans-serif; }
.p-head h1, .p-head h1, .p-head h1 a, .p-head h1 a:visited { color:#272d36; font: normal 19px/24px "Helvetica"; letter-spacing: -1px; }
.p-head h1 a:hover { color:#0099cc; text-decoration: none; }

.p-head h2 { color:#272d36; font: normal 24px/24px "Helvetica"; letter-spacing: -1px; }
.p-head h2 { color:#272d36; font: normal 22px/22px "Helvetica"; letter-spacing: 0px; }
.p-head h2 a, .p-head h2 a:visited { color:#272d36; }
.p-head h2 a:hover { color:#0099cc; text-decoration: none; }

Expand Down
16 changes: 16 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ def init_aside
@aside_sites = Site.limit(15)
@aside_categories = Category.limit(15)
@aside_posts = Post.last_actived.limit(15)
init_aside_by_month
end


def init_aside_by_month
posts = Post.normal.all
@month = {}
posts.each do |post|
date = post.created_at.strftime('%Y%m')
if @month[date]
@month.store("#{date}",@month["#{date}"] + 1)
else
@month.store("#{date}",1)
end
end
@array_mon = @month.sort.reverse!
end

end
8 changes: 8 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ def tag
render :action => "index" #, :stream => true
end

def month
month = params[:month] || Time.now.strftime('%Y%m')
start = Date.strptime("#{month}",'%Y%m').beginning_of_month
finish= Date.strptime("#{month}",'%Y%m').end_of_month
logger.info"======#{start}====#{finish}"
@posts = Post.normal.by_month(start,finish).paginate :page => params[:page], :per_page => 10
render :action => "index"
end


end
1 change: 1 addition & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ def notice_message

flash_messages.join("\n").html_safe
end


end
10 changes: 8 additions & 2 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ class Post
validates_presence_of :title,:category_id

# scopes
scope :last_actived, desc("updated_at").desc("created_at")
scope :fields_for_list, without(:body,:body_html)

scope :normal, where(:state => STATE[:normal])
scope :by_tag, Proc.new { |t| where(:tags => t) }

scope :last_actived, desc("updated_at").desc("created_at")
scope :fields_for_list, without(:body,:body_html)

scope :by_month, lambda {|start, finish|
where(:created_at => {'$gte' => start,'$lt' => finish}) if start && finish
}


before_save :split_tags
def split_tags
Expand Down
2 changes: 1 addition & 1 deletion app/views/index/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div class="p-head">
<h2><%= link_to(truncate(post.title, :length => 30), post_path(post), :title => post.title) %> </h2>
<p class="p-cat">
In: <%= link_to(post.category.name, category_posts_path(post.category), :title => post.category.name )%> Tags:<%=post_tags_tag(post)%>
By: <%= raw SiteConfig.site_author %> In: <%= link_to(post.category.name, category_posts_path(post.category), :title => post.category.name )%> Tags:<%=post_tags_tag(post)%>
</p>
<small class="p-time">
<strong class="day"><%=post.created_at.day%></strong>
Expand Down
2 changes: 1 addition & 1 deletion app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="post" style="margin-bottom: 40px;">
<div class="p-head">
<h2><%=link_to post.title,post_path(post)%> </h2>
<p class="p-cat">In: <%=post.category.name%> Tag:<%=post_tags_tag(post)%> </p>
<p class="p-cat">By: <%= raw SiteConfig.site_author %> In: <%=post.category.name%> Tag:<%=post_tags_tag(post)%> </p>
<small class="p-time">
<strong class="day"><%=post.created_at.day%></strong>
<strong class="month"><%=post.created_at.strftime("%b")%></strong>
Expand Down
25 changes: 25 additions & 0 deletions app/views/shared/_aside.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@
</ul>
</div>

<div id="categories" class="widget widget_categories">
<h3>按日期归档</h3>

<ul>
<% @array_mon.each do |i,v| %>
<li> <%=link_to "#{i}(#{v})" , month_posts_path(i)%> </li>
<%end%>
</ul>
</div>




<div id="recent-posts" class="widget widget_recent_entries">
<h3>友情链接</h3>
Expand All @@ -62,4 +74,17 @@
</ul>
</div>

<div id="recent-posts" class="widget widget_recent_entries">
<h3>数据统计</h3>
<ul>
<li>文章: <%= Post.count %></li>
<li>页面: <%= Page.count %></li>
<li>分类: <%= Category.count %></li>
<li>友情链接: <%= Site.count %></li>
</ul>
</div>




</div> <!--SR-->
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
resources :posts
match "posts/category/:id" => "posts#category", :as => :category_posts
match "posts/tag/:tag" => "posts#tag", :as => :tag_posts
match "posts/month/:month" => "posts#month", :as => :month_posts

resources :pages, :path => "page" do
collection do
Expand Down

0 comments on commit 2144cb8

Please sign in to comment.