Skip to content

Commit

Permalink
recovered a ton of lost commits in one fell swoop. upgraded to Rails …
Browse files Browse the repository at this point in the history
…2.3 compatiblity.
  • Loading branch information
nakajima committed Feb 2, 2009
1 parent bdaa5ba commit f7862fd
Show file tree
Hide file tree
Showing 23 changed files with 256 additions and 197 deletions.
53 changes: 37 additions & 16 deletions app/controllers/admin/posts_controller.rb
@@ -1,4 +1,19 @@
class Admin::PostsController < ApplicationController
class Admin::PostsController < Application
def self.expose_as(*types)
types.each do |name|
controller_title = name.to_s.tableize.titleize + 'Controller'
controller_class = Class.new(PostsController) do
define_method(:post_type) { name }
end

logger.info "=> Generating new PostsController subclass"
logger.info " Class name: #{controller_title}"
logger.info " Class: #{controller_class.inspect}"

Admin.const_set(controller_title, controller_class)
end
end

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

before_filter :login_required
Expand All @@ -10,6 +25,7 @@ class Admin::PostsController < ApplicationController
# GET /posts.xml
def index
@posts = post_repo.paginate_index(:page => params[:page])

respond_to do |format|
format.html # index.html.erb
format.rss { render :action => 'index.rss.builder' }
Expand Down Expand Up @@ -53,7 +69,7 @@ def create
respond_to do |format|
if @post.save
flash[:notice] = 'Post was successfully created.'
format.html { redirect_to admin_post_path(@post) }
format.html { redirect_to [:admin, @post] }
format.xml { render :xml => @post, :status => :created, :location => @post }
else
flash[:error] = @post.errors.full_messages
Expand All @@ -72,7 +88,7 @@ def update
if @post.update_attributes(params[:post])
expire_fragment(@post.permalink)
flash[:notice] = 'Post was successfully updated.'
format.html { redirect_to admin_post_path(@post) }
format.html { redirect_to [:admin, @post] }
format.js { render :json => @post }
format.xml { head :ok }
else
Expand All @@ -97,17 +113,22 @@ def destroy
end

private
def expire_post!
expire_path("#{@post.link}.html")
end

def expire_index!
expire_path('/index.html')
expire_path('/posts.html')
expire_path('/posts.rss')
expire_path('/posts')
expire_path("/#{@post.type.tableize}.html")
expire_path("/#{@post.type.tableize}.rss")
expire_path("/#{@post.type.tableize}")
end

def post_type
:post
end

def expire_post!
expire_path("#{@post.link}.html")
end

def expire_index!
expire_path('/index.html')
expire_path('/posts.html')
expire_path('/posts.rss')
expire_path('/posts')
expire_path("/#{@post.type.tableize}.html")
expire_path("/#{@post.type.tableize}.rss")
expire_path("/#{@post.type.tableize}")
end
end
44 changes: 18 additions & 26 deletions app/controllers/application.rb
@@ -1,45 +1,37 @@
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
class Application < ActionController::Base
include AuthenticatedSystem

protect_from_forgery

helper :all # include all helpers, all the time

filter_parameter_logging :password, :password_confirmation

protect_from_forgery :secret => 'ad5fcf9cf9a6c79ef7b70f6ff02c6fca8e6692d9cd48306a21f27be3e36658f49234fe'

skip_before_filter :verify_authenticity_token # Page caching screws up forgery protection stuff

POST_TYPES = %w(articles links pictures quotes snippets tweets gists)
POST_TYPE_PATTERN = /\/(#{POST_TYPES.join('|')})(\.rss)?\/?/i

def expire_path(file)
file = RAILS_ROOT + '/public' + file
file = File.join(Rails.root.to_str, '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 root_path and return
def post_repo
begin
@post_type = post_type.to_s.tableize
@post_type.classify.constantize
rescue => e
logger.info(e)
@post_type = :post
retry
end
end

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

end
2 changes: 1 addition & 1 deletion app/controllers/comments_controller.rb
@@ -1,4 +1,4 @@
class CommentsController < ApplicationController
class CommentsController < Application
before_filter :login_required, :only => [:update, :destroy]
before_filter :get_commentable
# skip_before_filter :verify_authenticity_token, :only => :update
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/feeds_controller.rb
@@ -1,4 +1,4 @@
class FeedsController < ApplicationController
class FeedsController < Application

before_filter :login_required

Expand Down Expand Up @@ -113,7 +113,7 @@ def expire_index!
expire_path("/#{@feed.class.entry_type}.rss")
expire_path("/#{@feed.class.entry_type}")
else
POST_TYPES.each do |entry_type|
PostsController.subtypes.each do |entry_type|
expire_path("/#{entry_type}.html")
expire_path("/#{entry_type}.rss")
expire_path("/#{entry_type}")
Expand Down
52 changes: 45 additions & 7 deletions app/controllers/posts_controller.rb
@@ -1,18 +1,41 @@
class PostsController < ApplicationController
class PostsController < Application
@@subtypes = []
cattr_reader :subtypes

def self.expose_as(*types)
options = types.extract_options!
types.each do |name|
PostsController.subtypes << name.to_s
controller_title = options[:namespace].to_s + name.to_s.tableize.titleize + 'Controller'
controller_class = Class.new(PostsController) do
prepend_view_path File.join(Rails.root, *%w[app views posts])
prepend_view_path File.join(Rails.root, *%w[app views posts types])
prepend_view_path File.join(Rails.root, *%w[app views posts forms])
define_method(:post_type) { name }
end

logger.info "=> Generating new #{options[:namespace]}PostsController subclass: #{controller_title}"

Object.const_set(controller_title, controller_class)
end
end

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

before_filter :redirect_to_admin, :if => :logged_in?

caches_page :index
caches_page :show

expose_as :articles, :links, :pictures, :quotes, :snippets, :tweets, :gists

# GET /posts
# GET /posts.xml
def index
@posts = post_repo.paginate_index(:page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.rss { render :action => 'index.rss.builder' }
format.html { render :template => 'posts/index.html.erb' }
format.rss { render :template => 'posts/index.rss.builder' }
format.xml { render :xml => @posts }
end
end
Expand All @@ -24,13 +47,28 @@ def show
redirect_to root_path and return unless @post.type.match(/Article|Snippet/)
@comment = flash[:comment] || @post.comments.build
respond_to do |format|
format.html # show.html.erb
format.html { render :template => 'posts/show.html.erb' }
format.xml { render :xml => @post }
end
end

private
def redirect_to_admin
redirect_to admin_root_path + request.path
def default_template_name(action_name = self.action_name)
if action_name
action_name = action_name.to_s
if action_name.include?('/') && template_path_includes_controller?(action_name)
action_name = strip_out_controller(action_name)
end
end
"#{self.controller_path}/#{action_name}"
end

private

def post_type
:post
end

def redirect_to_admin
redirect_to admin_root_path + request.path
end
end
2 changes: 1 addition & 1 deletion app/controllers/sessions_controller.rb
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# This controller handles the login/logout function of the site.
class SessionsController < ApplicationController
class SessionsController < Application

# render new.rhtml
def new
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
@@ -1,4 +1,4 @@
class UsersController < ApplicationController
class UsersController < Application
# render new.rhtml
def new
redirect_to root_path if (User.count >= 1) && !logged_in?
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/application_helper.rb
Expand Up @@ -33,7 +33,7 @@ def comments_link_for(post)
def twitterize(string)
string.gsub!(/@(\w*)/, '@<a href="http://twitter.com/\1">\1</a>')
string = auto_link(string)
string = RubyPants.new(string).to_html
# string = RubyPants.new(string).to_html
spanify_links(string)
end

Expand Down Expand Up @@ -88,7 +88,7 @@ def feed_url_for(post)
end

def feed_tag(name, options={})
name_str = (name || @post_type).to_s.gsub('/','')
name_str = (name || @post_type).to_s.pluralize.gsub('/','')
options[:format] ||= :rss
options[:title] ||= "#{name_str.titleize} Only (#{options[:format].to_s.upcase})"
options[:url] ||= SITE_SETTINGS[:feedburner][(name || 'all')] || "http://#{host_helper}#{relative_url_helper}/#{name_str}.rss"
Expand Down
2 changes: 1 addition & 1 deletion app/models/posts/article.rb
Expand Up @@ -34,6 +34,6 @@ def name
end

def to_param
from_feed? ? id : permalink
from_feed? ? id.to_s : permalink
end
end
2 changes: 1 addition & 1 deletion app/models/posts/link.rb
Expand Up @@ -9,6 +9,6 @@ def link_text
end

def to_param
attributes['id']
attributes['id'].to_s
end
end
4 changes: 2 additions & 2 deletions app/views/posts/index.html.erb
Expand Up @@ -5,9 +5,9 @@
<% end -%>

<div id="posts">
<%= render :partial => "post", :collection => @posts %>
<%= render :partial => "posts/post.html.erb", :collection => @posts %>
</div>

<%= '<p>There were no posts found.</p>' if @posts.empty? %>
<%= will_paginate(@posts, :posts_type => @post_type) %>
<%= will_paginate(@posts, :posts_type => @post_type.to_s.tableize) %>
2 changes: 1 addition & 1 deletion app/views/posts/index.rss.builder
Expand Up @@ -3,7 +3,7 @@ xml.rss :version => "2.0" do
xml.channel do
xml.title "#{SITE_SETTINGS['site_title']}: #{@post_type.pluralize.titleize}"
xml.description SITE_SETTINGS['site_tagline']
xml.link formatted_posts_url(:rss)
xml.link posts_url(:format => :rss)
@posts.each do |post|
xml.item do
xml.title post.name
Expand Down
12 changes: 6 additions & 6 deletions config/environment.rb
Expand Up @@ -10,25 +10,25 @@
# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')

require 'authenticated_model'

Rails::Initializer.run do |config|
config.load_paths += %W[
#{RAILS_ROOT}/app/models/posts
#{RAILS_ROOT}/app/models/feeds
]

config.action_controller.cache_store = :file_store, "#{RAILS_ROOT}/public/cache"
config.action_controller.session_store = :cookie_store
config.action_controller.session = {
:session_key => '_aintablog_session',
:secret => '181d7e70eb790ceff32f283c373e39ca512ff461d86210571efb2ab49e29fe5763a6e4e27f618efbdbe93b4ac3a0a4339cf7033c18f9ddaf2ec845d44fc32bea'
:session_key => "_myapp_session",
:secret => (s = ""; 31.times { s << rand(10).to_s }; s)
}

# These gems are totally required
config.gem 'feed-normalizer'
config.gem 'hpricot'
config.gem 'rubypants'
config.gem 'RedCloth'
config.gem 'nokogiri'
end

require 'authenticated_model'

ActionController::Base.cache_store = :file_store, "#{RAILS_ROOT}/public/cache"
2 changes: 1 addition & 1 deletion config/environments/development.rb
Expand Up @@ -11,7 +11,7 @@
# Show full error reports and disable caching
config.action_controller.consider_all_requests_local = true
config.action_view.debug_rjs = true
config.action_controller.perform_caching = false
config.action_controller.perform_caching = true

# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
2 changes: 1 addition & 1 deletion config/environments/test.rb
@@ -1,7 +1,7 @@
config.cache_classes = true
config.whiny_nils = true
config.action_controller.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_controller.perform_caching = true
config.action_controller.allow_forgery_protection = false
config.action_mailer.delivery_method = :test

Expand Down
12 changes: 6 additions & 6 deletions config/routes.rb
Expand Up @@ -3,12 +3,12 @@
map.resources :posts
map.resources :users
map.resource :session
map.resources :articles, :controller => 'posts', :has_many => :comments
map.resources :quotes, :controller => 'posts'
map.resources :pictures, :controller => 'posts'
map.resources :tweets, :controller => 'posts'
map.resources :links, :controller => 'posts'
map.resources :snippets, :controller => 'posts', :has_many => :comments
map.resources :articles, :has_many => :comments
map.resources :quotes
map.resources :pictures
map.resources :tweets
map.resources :links
map.resources :snippets, :has_many => :comments
map.resources :comments, :member => { :report => :put }

map.namespace(:admin) do |admin|
Expand Down

0 comments on commit f7862fd

Please sign in to comment.