Class: ControllerResources::Resource
- Inherits:
-
Object
- Object
- ControllerResources::Resource
- Defined in:
- lib/controller_resources/resource.rb
Overview
The “data model” for the controlled resource. In the controller, defining the resource block instantiates this class, which is used to hold all of the data the developer provides inside the block and used in the Extension to actually do the “heavy lifting” in the controller. This class is just a way of storing all of the configured state for a given controller’s resource.
Defined Under Namespace
Classes: NotConfiguredError
Instance Attribute Summary collapse
-
#edit_params ⇒ Object
readonly
Permitted parameters for editing members.
-
#name ⇒ Object
readonly
The name of this resource, used to look up the collection and model names.
-
#search_params ⇒ Object
readonly
Permitted parameters for searching collections.
Instance Method Summary collapse
-
#collection_name ⇒ Object
Pluralized version of the given resource name.
-
#initialize(name) {|_self| ... } ⇒ Resource
constructor
Instantiate a new Resource with the given name and a block to define permitted parameters.
-
#model_class ⇒ Object
The model name as a class constant.
-
#model_name ⇒ Object
Singular version of the given resource name.
-
#modify(*params) ⇒ Object
Set the edit params for this controller.
-
#search(*params) ⇒ Object
Set the search params for this controller.
Constructor Details
#initialize(name) {|_self| ... } ⇒ Resource
Instantiate a new Resource with the given name and a block to define permitted parameters. The code given in the resource block is passed directly here so that the Resource class can be set up with its proper state.
By default, resources are not given any search or edit params. If none are provided (via the search and modify configuration methods), all parameters will be permitted on collection and member resources respectively.
44 45 46 47 48 49 50 |
# File 'lib/controller_resources/resource.rb', line 44 def initialize(name, &block) @name = name.to_s @search_params = [] @edit_params = [] @block = block yield self if block_given? end |
Instance Attribute Details
#edit_params ⇒ Object (readonly)
Permitted parameters for editing members.
23 24 25 |
# File 'lib/controller_resources/resource.rb', line 23 def edit_params @edit_params end |
#name ⇒ Object (readonly)
The name of this resource, used to look up the collection and model names.
13 14 15 |
# File 'lib/controller_resources/resource.rb', line 13 def name @name end |
#search_params ⇒ Object (readonly)
Permitted parameters for searching collections.
18 19 20 |
# File 'lib/controller_resources/resource.rb', line 18 def search_params @search_params end |
Instance Method Details
#collection_name ⇒ Object
Pluralized version of the given resource name.
62 63 64 |
# File 'lib/controller_resources/resource.rb', line 62 def collection_name name.pluralize.to_sym end |
#model_class ⇒ Object
The model name as a class constant.
69 70 71 |
# File 'lib/controller_resources/resource.rb', line 69 def model_class @model_class ||= model_name.to_s.classify.constantize end |
#model_name ⇒ Object
Singular version of the given resource name.
55 56 57 |
# File 'lib/controller_resources/resource.rb', line 55 def model_name name.singularize.to_sym end |
#modify(*params) ⇒ Object
Set the edit params for this controller.
Example:
resource :post do
modify :title, :category, :tag, :body, :is_published
end
95 96 97 |
# File 'lib/controller_resources/resource.rb', line 95 def modify(*params) @edit_params = params end |
#search(*params) ⇒ Object
Set the search params for this controller.
Example:
resource :post do
search :title, :category, :tag
end
82 83 84 |
# File 'lib/controller_resources/resource.rb', line 82 def search(*params) @search_params = params end |