Skip to content

Commit

Permalink
resource methods now in mixin to make overriding them easier
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwhite committed Dec 20, 2009
1 parent 6c080a7 commit 472440a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
4 changes: 4 additions & 0 deletions 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]
Expand Down
34 changes: 7 additions & 27 deletions lib/ardes/resources_controller.rb
Expand Up @@ -451,13 +451,18 @@ 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?

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
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions 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
4 changes: 3 additions & 1 deletion spec/app.rb
Expand Up @@ -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'
Expand Down

0 comments on commit 472440a

Please sign in to comment.