Method: Canned::ControllerExt::ClassMethods#register_resource

Defined in:
lib/canned/controller_ext.rb

#register_resource(_name, _options = {}, &_block) ⇒ Object

Registers a canned resource

Parameters:

  • _name (String)

    Resource name

  • _options (String) (defaults to: {})

    Options:

    • using: Parameter used as key if not block is given.

    • only: If set, will only load the resource for the given actions.

    • except: If set, will not load the resource for any of the given actions.

    • from: TODO load_resource :raffle, from: :site

    • as: TODO: load_resource :raffle, from: :site, as: :draws

  • _block (Block)

    generator block, will be called to generate the resource if needed.



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/canned/controller_ext.rb', line 137

def register_resource(_name, _options={}, &_block)
  self._cn_resources ||= []
  self._cn_resources << {
    name: _name,
    only: unless _options[:only].nil? then Array(_options[:only]) else nil end,
    except: Array(_options[:except]),
    loader: _block || Proc.new do
      key = _options.fetch(:using, :id)
      if params.has_key? key then eval(_name.to_s.camelize).find params[key]
      else nil end
    end
  }
end