Skip to content

Commit

Permalink
DRY'd up post_repo code so error cases could be handled the same for …
Browse files Browse the repository at this point in the history
…admin and normal posts controllers.
  • Loading branch information
qrush committed Sep 16, 2008
1 parent 367cf35 commit e034b4a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 36 deletions.
16 changes: 0 additions & 16 deletions app/controllers/admin/posts_controller.rb
@@ -1,6 +1,4 @@
class Admin::PostsController < ApplicationController
POST_TYPE_PATTERN = /\/(articles|tweets|quotes|pictures|links|snippets|posts)(\.rss)?\/?/i

rescue_from ActiveRecord::RecordNotFound, :with => :not_found

before_filter :login_required
Expand Down Expand Up @@ -99,20 +97,6 @@ def destroy
end

private
def post_repo
@post_type = params[:posts_type] || request.path.split('/admin').last.gsub(POST_TYPE_PATTERN, '\1')
return @post_type.classify.constantize
rescue => e
logger.info(e)
@post_type = 'posts'
@post_type.classify.constantize
end

def not_found
flash[:error] = "Sorry but that post could not be found."
redirect_to '/' and return
end

def expire_post!
expire_path("#{@post.link}.html")
end
Expand Down
24 changes: 24 additions & 0 deletions app/controllers/application.rb
Expand Up @@ -12,9 +12,33 @@ class ApplicationController < ActionController::Base

skip_before_filter :verify_authenticity_token # Page caching screws up forgery protection stuff

POST_TYPE_PATTERN = /\/(articles|tweets|quotes|pictures|links|snippets|posts)(\.rss)?\/?/i

def expire_path(file)
file = RAILS_ROOT + '/public' + file
FileUtils.rm_rf(file) if File.exists?(file)
logger.info("Expired cache: #{file}")
end

protected
def post_repo

# two replaces, but it's better than duped code.
@post_type = params[:posts_type] || request.path.gsub(/^\/admin/, '/').gsub(POST_TYPE_PATTERN, '\1')

# for some reason '/' this gets classified as '::', which is an Object. Adding a check for that.
throw NameError if @post_type.eql?('/')

return @post_type.classify.constantize
rescue => e
logger.info(e)
@post_type = 'posts'
return @post_type.classify.constantize
end

def not_found
cookies[:error] = "Sorry but that post could not be found."
redirect_to '/' and return
end

end
20 changes: 0 additions & 20 deletions app/controllers/posts_controller.rb
@@ -1,6 +1,4 @@
class PostsController < ApplicationController
POST_TYPE_PATTERN = /\/(articles|tweets|quotes|pictures|links|snippets|posts)(\.rss)?\/?/i

rescue_from ActiveRecord::RecordNotFound, :with => :not_found

before_filter :redirect_to_admin, :if => :logged_in?
Expand Down Expand Up @@ -32,24 +30,6 @@ def show
end

private
def post_repo
@post_type = params[:posts_type] || request.path.gsub(POST_TYPE_PATTERN, '\1')

# for some reason '/' this gets classified as '::', which is an Object. Adding a check for that.
throw NameError if @post_type.eql?('/')

return @post_type.classify.constantize
rescue => e
logger.info(e)
@post_type = 'posts'
return @post_type.classify.constantize
end

def not_found
cookies[:error] = "Sorry but that post could not be found."
redirect_to '/' and return
end

def redirect_to_admin
redirect_to "/admin" + request.path
end
Expand Down

0 comments on commit e034b4a

Please sign in to comment.