Skip to content

Commit

Permalink
extracted 'expose_as' controller helper into SingleControllerInherita…
Browse files Browse the repository at this point in the history
…nce module
  • Loading branch information
nakajima committed Feb 2, 2009
1 parent c55cc35 commit 4646ada
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
15 changes: 0 additions & 15 deletions app/controllers/admin/posts_controller.rb
@@ -1,19 +1,4 @@
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 Down
23 changes: 5 additions & 18 deletions app/controllers/posts_controller.rb
Expand Up @@ -2,23 +2,7 @@ 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
include SingleControllerInheritance

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

Expand All @@ -27,7 +11,10 @@ def self.expose_as(*types)
caches_page :index
caches_page :show

expose_as :articles, :links, :pictures, :quotes, :snippets, :tweets, :gists
expose_as :articles, :links, :pictures, :quotes, :snippets, :tweets, :gists do |name|
subtypes << name
define_method(:post_type) { name }
end

# GET /posts
# GET /posts.xml
Expand Down
1 change: 1 addition & 0 deletions config/environment.rb
Expand Up @@ -11,6 +11,7 @@
require File.join(File.dirname(__FILE__), 'boot')

require 'authenticated_model'
require 'single_controller_inheritance'

Rails::Initializer.run do |config|
config.load_paths += %W[
Expand Down
24 changes: 24 additions & 0 deletions lib/single_controller_inheritance.rb
@@ -0,0 +1,24 @@
module SingleControllerInheritance
def self.included(base)
base.extend ClassMethods
end

module ClassMethods
def expose_as(*types, &block)
options = types.extract_options!
namespace = options[:namespace]

types.each do |child|
class_name = namespace.to_s + child.to_s.titleize + 'Controller'
controller = Class.new(self) do
block[child] if block_given?
end

logger.info "=> Generating new %s subclass: %s" % [child, class_name]

scope = namespace ? namespace.constantize : Object
scope.const_set(class_name, controller)
end
end
end
end
4 changes: 3 additions & 1 deletion test/functional/admin_posts_controller_test.rb
Expand Up @@ -39,7 +39,9 @@ def test_should_get_index
end

# Post types

# NOTE Since these are all now handled by dynamically created controllers,
# we'll need a different way of testing them.
#
# def test_should_retrieve_links_only
# @request.stubs(:path).returns('/links')
# Link.expects(:paginate_index).returns(posts_stub)
Expand Down
3 changes: 3 additions & 0 deletions test/functional/posts_controller_test.rb
Expand Up @@ -44,6 +44,9 @@ def test_should_redirect_to_admin_show_if_logged_in_even_for_relative_urls
end

# # Post types
#
# NOTE Since these are all now handled by dynamically created controllers,
# we'll need a different way of testing them.
#
# def test_should_retrieve_links_only
# @request.stubs(:path).returns('/links')
Expand Down

0 comments on commit 4646ada

Please sign in to comment.