| 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:
| as | [RW] | |
| find | [R] | |
| key | [R] | |
| klass | [R] | |
| name | [R] | |
| name_prefix | [R] | |
| segment | [R] | |
| source | [R] |
factory for Specification and SingletonSpecification
you can call Specification.new ‘name’, :singleton => true
# 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:
Options for unconvential use (otherwise these are all inferred from the name)
Passing a block is the same as passing :find => Proc
# 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
finds the resource using the custom :find Proc or symbol
# 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
# 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
# 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