Module: Canned::ControllerExt::ClassMethods

Defined in:
lib/canned/controller_ext.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_restricted(_definition, _method = nil, &_block) ⇒ Object

Setups the controller user profile definitions and profile provider block (or proc)

The passed method or block must return a list of profiles to be validated by the definition.

TODO: default definition (canned config)

Parameters:

  • _definition (Definition)

    Profile definition

  • _method (Symbol) (defaults to: nil)

    Profile provider method name

  • _block (Block)

    Profile provider block



91
92
93
94
95
96
97
98
99
# File 'lib/canned/controller_ext.rb', line 91

def acts_as_restricted(_definition, _method=nil, &_block)
  self.before_filter do
    if is_restricted?
      profiles = Array(if _method.nil? then instance_eval(&_block) else send(_method) end)
      raise Canned::AuthError.new 'No profiles avaliable' if profiles.empty?
      raise Canned::AuthError unless perform_access_authorization(_definition, profiles)
    else perform_resource_loading end
  end
end

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

 Registers a canned actor

Parameters:

  • _name (String)

    Actor’s name and generator method name if no block is given.

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

    Options:

    • as: If given, this si used as actor’s name and _name is only used for generator retrieval.

  • _block (Block)

    generator block, used instead of generator method if given.



122
123
124
# File 'lib/canned/controller_ext.rb', line 122

def register_actor(_name, _options={}, &_block)
  self._cn_actors[_options.fetch(:as, _name)] = _block || _name
end

#register_default_resourcesObject



152
153
154
# File 'lib/canned/controller_ext.rb', line 152

def register_default_resources
  # TODO: Load resources using convention and controller names.
end

#register_resource(_name, _options = {}, &_block) ⇒ Object Also known as: load_resource

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

#unrestricted(*_excluded) ⇒ Object

Removes protection for the especified controller actions.

Parameters:

  • _excluded (splat)

    List of actions to be excluded.



110
111
112
113
# File 'lib/canned/controller_ext.rb', line 110

def unrestricted(*_excluded)
  self._cn_excluded ||= []
  self._cn_excluded.push(*(_excluded.collect &:to_sym))
end

#unrestricted_allObject

Removes protection for all controller actions.



102
103
104
# File 'lib/canned/controller_ext.rb', line 102

def unrestricted_all
  self._cn_excluded = :all
end