Class: ControllerResources::Resource

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • name (Symbol)
    • The name of this resource, typically given

    as a singular symbol.

  • &block (Block)
    • A block of code used to set up this

    object.

Yields:

  • (_self)

Yield Parameters:



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_paramsObject (readonly)

Permitted parameters for editing members.



23
24
25
# File 'lib/controller_resources/resource.rb', line 23

def edit_params
  @edit_params
end

#nameObject (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_paramsObject (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_nameObject

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_classObject

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_nameObject

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

Parameters:

  • A (Array)

    collection of params to whitelist.



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

Parameters:

  • A (Array)

    collection of params to whitelist.



82
83
84
# File 'lib/controller_resources/resource.rb', line 82

def search(*params)
  @search_params = params
end