diff --git a/History.txt b/History.txt index d27cb50..374a20f 100644 --- a/History.txt +++ b/History.txt @@ -1,3 +1,7 @@ +* resource methods (new_resource, find_resource, and find_resources) are now in a mixin so it's easier + to override them. Just include a new mixin after the resources_controller_for call, or add them to your + action module. + * Allow new_resource to use the block initialisation syntax in rails 2.3 (reported by Chris Hapgood) * Fix bug where id is the same name as the resource name [mocoso] diff --git a/lib/ardes/resources_controller.rb b/lib/ardes/resources_controller.rb index 37312ba..4a2fbcb 100644 --- a/lib/ardes/resources_controller.rb +++ b/lib/ardes/resources_controller.rb @@ -451,6 +451,7 @@ def resources_controller_for(name, options = {}, &block) extend ResourcesController::ClassMethods helper ResourcesController::Helper include ResourcesController::InstanceMethods, ResourcesController::NamedRouteHelper + include ResourcesController::ResourceMethods unless included_modules.include?(ResourcesController::ResourceMethods) end before_filter(:load_enclosing_resources, when_options) unless load_enclosing_resources_filter_exists? @@ -458,6 +459,10 @@ def resources_controller_for(name, options = {}, &block) write_inheritable_attribute(:specifications, []) specifications << '*' unless options.delete(:load_enclosing) == false + unless (options.delete(:resource_methods) == false) + include ResourcesController::ResourceMethods + end + unless (actions = options.delete(:actions)) == false actions ||= options[:singleton] ? Ardes::ResourcesController.singleton_actions : Ardes::ResourcesController.actions include_actions actions, when_options @@ -550,33 +555,8 @@ def ensure_sane_wildcard end module InstanceMethods - def self.included(base) - base.class_eval do - protected - # we define the find|new_resource(s) methods only if they're not already defined - # this allows abstract controllers to define the resource service methods - unless instance_methods.include?('find_resources') - # finds the collection of resources - def find_resources - resource_service.find :all - end - end - - unless instance_methods.include?('find_resource') - # finds the resource, using the passed id - def find_resource(id = params[:id]) - resource_service.find id - end - end - - unless instance_methods.include?('new_resource') - # makes a new resource, optionally using the passed hash - def new_resource(attributes = (params[resource_name] || {}), &block) - resource_service.new attributes, &block - end - end - end - base.send :hide_action, *instance_methods + def self.included(controller) + controller.send :hide_action, *instance_methods end def resource_service=(service) diff --git a/lib/ardes/resources_controller/resource_methods.rb b/lib/ardes/resources_controller/resource_methods.rb new file mode 100644 index 0000000..f897213 --- /dev/null +++ b/lib/ardes/resources_controller/resource_methods.rb @@ -0,0 +1,25 @@ +module Ardes + module ResourcesController + # methods which communicate with the resource_service to find/create resources + module ResourceMethods + protected + # finds the collection of resources + def find_resources + resource_service.find :all + end + + # finds the resource, using the passed id, defaults to the current params[:id] + def find_resource(id = nil) + id ||= respond_to?(:params) && params[:id] + resource_service.find id + end + + # makes a new resource, if attributes are not supplied, determine them from the + # params hash and the current resource_name + def new_resource(attributes = nil, &block) + attributes ||= (respond_to?(:params) && params[resource_name]) || {} + resource_service.new attributes, &block + end + end + end +end \ No newline at end of file diff --git a/spec/app.rb b/spec/app.rb index 50ab1e3..b7a6cb7 100644 --- a/spec/app.rb +++ b/spec/app.rb @@ -238,11 +238,13 @@ class OwnersController < ApplicationController end class PostsAbstractController < ApplicationController + include Ardes::ResourcesController::ResourceMethods attr_accessor :filter_trace # for testing filter load order before_filter {|controller| controller.filter_trace ||= []; controller.filter_trace << :abstract} - + +protected # redefine find_resources def find_resources resource_service.find :all, :order => 'id DESC'