Skip to content

Commit

Permalink
resources_controller
Browse files Browse the repository at this point in the history
* Enclosing resources are now all loaded by one method :load_enclosing which is a prepend_before_filter.
  This means that you can access the enclosing resources in all before_filters, even when a subclass adds
  more nestings.
  
  Example:
  
    class PostsController < ApplicationController
      resources_controller_for :posts
    end
    
    class UserPostsController < PostsController
      nested_in :user
      
      before_filter {|c| raise 'boom' if @user.name == 'Santa'}
    end



git-svn-id: https://svn.ardes.com/rails_plugins/resources_controller@398 845bbffb-5c18-0410-91b3-f25c072b94c1
  • Loading branch information
ian committed Aug 17, 2007
1 parent 836cf60 commit f4f78c8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
* Enclosing resources are now all loaded by one method :load_enclosing which is a prepend_before_filter.
This means that you can access the enclosing resources in all before_filters, even when a subclass adds
more nestings.

Example:

class PostsController < ApplicationController
resources_controller_for :posts
end

class UserPostsController < PostsController
nested_in :user

before_filter {|c| raise 'boom' if @user.name == 'Santa'}
end

* Removed ResourceService proxy class. This was mainly used for BC with rails <= 1.2.2. If you're
using this, then freeze to r377 of resources_controller. The resource_service is now either an
ActiveRecord or association proxy.
Expand Down
7 changes: 5 additions & 2 deletions SPECDOC
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ resource_service in ForumPostsController
- should raise RecordNotFound with find(@other_post.id)
- should find only posts belonging to @forum with find(:all)

ForumPostsController order of before_filters
- should == [:load_enclosing, :abstract, :posts, :forum_posts]

Requesting /forums/2/posts (testing the before filters)
- should find the forum
- should assign the found forum for the view
Expand Down Expand Up @@ -469,6 +472,6 @@ Automatic (route_name, name_prefix) for
- HasAComplexNameController should be ('has_a_complex_name', '')
- EnclosedByFooHasAComplexNameController should be ('has_a_complex_name', 'enclosed_by_foo_')

Finished in 2.473041 seconds
Finished in 2.541796 seconds

324 examples, 0 failures
325 examples, 0 failures
14 changes: 11 additions & 3 deletions lib/ardes/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ def resources_controller_for(resources_name, options = {})
self.class_eval do
unless included_modules.include?(::Ardes::ResourcesController::InstanceMethods)
class_inheritable_reader :route_name, :singular_route_name
class_inheritable_accessor :resources_name, :resource_name, :resource_class, :resource_collection_name
class_inheritable_accessor :resources_name, :resource_name, :resource_class, :resource_collection_name, :enclosing_loaders

self.enclosing_loaders = []

class<<self
attr_writer :name_prefix
Expand All @@ -196,6 +198,8 @@ def route_name=(name)
include UrlHelper
include Actions if options[:actions_include].nil? || options[:actions_include] == true
helper Helper

prepend_before_filter :load_enclosing
end
end

Expand Down Expand Up @@ -289,8 +293,8 @@ def add_enclosing(name, options = {}, &block)
options.assert_valid_keys(:polymorphic, :anonymous, :load_enclosing, :class_name, :collection_name, :foreign_key, :name_prefix)
options[:anonymous] = options[:anonymous] || options[:polymorphic]

before_filter {|controller| controller.send :load_enclosing_resources} if options[:load_enclosing]
before_filter {|controller| controller.send :load_enclosing_resource, name, options, &block }
enclosing_loaders << :load_enclosing_resources if options[:load_enclosing]
enclosing_loaders << [:load_enclosing_resource, [name, options], block]
end

module InstanceMethods
Expand Down Expand Up @@ -394,6 +398,10 @@ def resources_request
end

private
def load_enclosing
enclosing_loaders.each {|method| method.is_a?(Symbol) ? send(method) : send(method[0], *method[1], &method[2]) }
end

# load any remaining enclosing resources that nest the current nested_in
def load_enclosing_resources
raise RuntimeError, "you can only specify nested_in :load_enclosing => true once" if @_load_enclosing_resources
Expand Down
16 changes: 16 additions & 0 deletions spec/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,32 @@ class ForumsController < ActionController::Base
end

class PostsAbstractController < ActionController::Base
attr_accessor :filter_trace

before_filter {|controller| controller.filter_trace ||= []; controller.filter_trace << :abstract}

# redefine find_resources
def find_resources
resource_service.find :all, :order => 'id DESC'
end
end

class PostsController < PostsAbstractController
before_filter {|controller| controller.filter_trace ||= []; controller.filter_trace << :posts}

# example of providing options to resources_controller_for
resources_controller_for :posts, :class_name => 'Post', :route_name => 'posts', :name_prefix => ''

def load_enclosing_with_trace(*args)
self.filter_trace ||= []; self.filter_trace << :load_enclosing
load_enclosing_without_trace(*args)
end
alias_method_chain :load_enclosing, :trace
end

class UserPostsController < PostsController
before_filter {|controller| controller.filter_trace ||= []; controller.filter_trace << :user_posts}

# example of providing options to nested in
nested_in :user, :class_name => 'User', :foreign_key => 'user_id', :name_prefix => 'user_'
end
Expand All @@ -120,6 +134,8 @@ class AddressesController < ActionController::Base
end

class ForumPostsController < PostsController
before_filter {|controller| controller.filter_trace ||= []; controller.filter_trace << :forum_posts}

# example of providing a custom finder for the nesting resource
nested_in :forum do
Forum.find(params[:forum_id])
Expand Down
9 changes: 9 additions & 0 deletions spec/controllers/forum_posts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ def setup_mocks
end
end

describe ForumPostsController, ' order of before_filters' do
before do
@forum = Forum.create
get :index, :forum_id => @forum.id
end

it { @controller.filter_trace.should == [:load_enclosing, :abstract, :posts, :forum_posts] }
end

describe "Requesting /forums/2/posts (testing the before filters)" do
include ForumPostsSpecHelper
controller_name :forum_posts
Expand Down

0 comments on commit f4f78c8

Please sign in to comment.