Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Archives feature in Blogit #39

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions app/controllers/blogit/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class PostsController < ::Blogit::ApplicationController
# Raise a 404 error if the admin actions aren't to be included
# We can't use blogit_conf here because it sometimes raises NoMethodError in main app's routes
unless Blogit.configuration.include_admin_actions
before_filter :raise_404, except: [:index, :show, :tagged]
before_filter :raise_404, except: [:index, :show, :tagged, :archive]
end

blogit_authenticate(except: [:index, :show, :tagged])
blogit_authenticate(except: [:index, :show, :tagged, :archive])

blogit_cacher(:index, :show, :tagged)
blogit_sweeper(:create, :update, :destroy)
Expand Down Expand Up @@ -47,6 +47,12 @@ def tagged
render :index
end

def archive
@date = Date.new(params[:year].to_i, params[:month].to_i)
@posts = Post.for_index(params[Kaminari.config.param_name]).where("created_at >= ?", @date.beginning_of_month).where("created_at <= ?", @date.end_of_month)
render :index
end

def new
@post = current_blogger.blog_posts.new(params[:post])
end
Expand Down
23 changes: 23 additions & 0 deletions app/models/blogit/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,29 @@ def comments=(value)
super(value)
end

# =================
# = Class Methods =
# =================

def self.tags
result = Set.new
self.all.each do |p|
result.merge(p.tags)
end

return result
end

def self.archives
result = Set.new
self.all.each do |p|
@date = Date.new(p.created_at.year, p.created_at.month)
result.add(@date)
end

return result
end

# ==========
# = Scopes =
# ==========
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Keep these above the posts resources block
match "posts/page/:page" => "posts#index"
match "posts/tagged/:tag" => 'posts#tagged', as: :tagged_blog_posts
match "posts/archive/:year/:month" => 'posts#archive', as: :archive_blog_posts

resources :posts do
resources :comments, only: [:create, :destroy]
Expand Down