Class Ardes::ResourcesController::Specification
In: lib/ardes/resources_controller/specification.rb
Parent: Object

This class holds all the info that is required to find a resource, or determine a name prefix, based on a route segment or segment pair (e.g. /blog or /users/3).

You don‘t need to instantiate this class directly - it is created by ResourcesController::ClassMethods#nested_in, ResourcesController#map_resource (and ResourcesController::InstanceMethods#load_wildcard)

This is primarily a container class. A summary of its behaviour:

  1. setting defaults for its own variables on initialize, and
  2. finding an enclosing resource, given a controller object

Methods

Attributes

as  [RW] 
find  [R] 
key  [R] 
klass  [R] 
name  [R] 
name_prefix  [R] 
segment  [R] 
source  [R] 

Public Class methods

factory for Specification and SingletonSpecification

you can call Specification.new ‘name’, :singleton => true

[Source]

    # File lib/ardes/resources_controller/specification.rb, line 20
20:       def self.new(name, options = {}, &block)
21:         options.delete(:singleton) ? SingletonSpecification.new(name, options, &block) : super(name, options, &block)
22:       end

Example Usage

 Specifcation.new <name>, <options hash>, <&block>

name should always be singular.

Options:

  • :singleton: (default false) set this to true if the resource is a Singleton
  • :find: (default null) set this to a symbol or Proc to specify how to find the resource. Use this if the resource is found in an unconventional way

Options for unconvential use (otherwise these are all inferred from the name)

  • :source: a plural string or symbol (e.g. :users). This is used to find the class or association name
  • :class: a Class. This is the class of the resource (if it can‘t be inferred from name or :source)
  • :key: (e.g. :user_id) used to find the resource id in params
  • :name_prefix: (e.g. ‘user_’) (set this to false if you want to specify that there is none)
  • :segment: (e.g. ‘users’) the segment name in the route that is matched

Passing a block is the same as passing :find => Proc

[Source]

    # File lib/ardes/resources_controller/specification.rb, line 44
44:       def initialize(spec_name, options = {}, &block)
45:         options.assert_valid_keys(:class, :source, :key, :find, :name_prefix, :segment, :as)
46:         @name        = spec_name.to_s
47:         @find        = block || options.delete(:find)
48:         @segment     = (options[:segment] && options[:segment].to_s) || name.pluralize
49:         @source      = (options[:source] && options[:source].to_s) || name.pluralize
50:         @name_prefix = options[:name_prefix] || (options[:name_prefix] == false ? '' : "#{name}_")
51:         @klass       = options[:class] || ((source && source.classify) || name.camelize).constantize
52:         @key         = (options[:key] && options[:key].to_s) || name.foreign_key
53:         @as          = options[:as]
54:       end

Public Instance methods

finds the resource using the custom :find Proc or symbol

[Source]

    # File lib/ardes/resources_controller/specification.rb, line 67
67:       def find_custom(controller)
68:         raise "This specification has no custom :find attribute" unless find
69:         find.is_a?(Proc) ? controller.instance_eval(&find) : controller.send(find)
70:       end

given a controller object, returns the resource according to this specification

[Source]

    # File lib/ardes/resources_controller/specification.rb, line 62
62:       def find_from(controller)
63:         find ? find_custom(controller) : find_resource(controller)
64:       end

finds the resource on a controller using enclosing resources or resource class

[Source]

    # File lib/ardes/resources_controller/specification.rb, line 73
73:       def find_resource(controller)
74:         (controller.enclosing_resource ? controller.enclosing_resource.send(source) : klass).find controller.params[key]
75:       end

returns false

[Source]

    # File lib/ardes/resources_controller/specification.rb, line 57
57:       def singleton?
58:         false
59:       end

[Validate]