Skip to content

Commit

Permalink
Se agregó el archivo de posts por fecha.
Browse files Browse the repository at this point in the history
  • Loading branch information
fcingolani committed Dec 10, 2011
1 parent 96d5c3e commit ccb6829
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/controllers/posts_controller.rb
Expand Up @@ -11,4 +11,8 @@ def show
@comments = comments_collection.page(page).per(VenganzasDelPasado::Application.config.comments_per_page)
end

def archive
@posts = @posts.published.created_on(params[:year],params[:month],params[:day]).lifo.page(params[:page]).per(VenganzasDelPasado::Application.config.posts_per_page)
end

end
18 changes: 18 additions & 0 deletions app/models/post.rb
Expand Up @@ -10,4 +10,22 @@ class Post < ActiveRecord::Base
scope :published, where(:status => 'published')
scope :lifo, order('created_at DESC')


def self.created_on(year, month = nil, day = nil)
date = Date.new year.to_i

if month.present?
date = date.change( :month => month.to_i )
if day.present?
date = date.change( :day => day.to_i )
range_date = date.beginning_of_day..date.end_of_day
else
range_date = date.beginning_of_month..date.end_of_month
end
else
range_date = date.beginning_of_year..date.end_of_year
end

where(:created_at => range_date)
end
end
18 changes: 18 additions & 0 deletions app/views/posts/archive.html.haml
@@ -0,0 +1,18 @@
- if params[:day].present?
- title_part = "Archivo del #{params[:day]}/#{params[:month]}/#{params[:year]}"
- elsif params[:month].present?
- title_part = "Archivo del #{params[:month]}/#{params[:year]}"
- else
- title_part = "Archivo del año #{params[:year]}"

- if params[:page].nil?
- page_title(title_part)
- else
- page_title("#{title_part} <small>Página #{params[:page]}</small>")

%h2.fancy-title!= html_page_title

.posts
= render @posts

= paginate @posts, :window => 2
3 changes: 3 additions & 0 deletions config/routes.rb
Expand Up @@ -6,6 +6,9 @@
get '/users/auth/:provider' => 'users/omniauth_callbacks#passthru', :as => :user_omniauth
end

match 'posts/archive/:year(/:month(/:day))' => 'posts#archive', :constraints => {
:year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/
}
resources :posts, :only => [:index, :show] do
resources :comments, :only => [:show, :create] do
member do
Expand Down

0 comments on commit ccb6829

Please sign in to comment.