Class: Wedge::Plugins::AbilityList

Inherits:
Form show all
Defined in:
lib/wedge/plugins/ability_list.rb

Defined Under Namespace

Modules: Helpers

Constant Summary collapse

Error =
Class.new(StandardError)

Instance Attribute Summary

Attributes inherited from Component

#wedge_method_called

Instance Method Summary collapse

Methods inherited from Form

#_accessor_options, #_accessors, #_aliases, #_attributes, #_atts_keys, #_keys, #_set_atts, #_with_atts, #_without_atts, attr_accessor, attr_reader, #attributes, #attributes?, #display_errors, form_accessor, inherited, #initialize, #method_missing, model_alias, #model_attributes, #model_attributes?, #nested?, #options, original_attr_accessor, original_attr_reader, #render_values, #slice, #validate_msg

Methods included from Form::Delegates

#_delegates

Methods included from Render

#_error_name, #display_errors, #render_values

Methods included from Form::Validations

#_errors, #_model_errors, #error, #errors, #model_errors, #valid, #valid?, #validate

Methods included from Methods

#client?, included, #server?

Methods inherited from Component

config, dom, html, html!, #method_missing, method_missing, plugin, set_dom, store, tmpl, #to_js, #wedge, #wedge_class_store, wedge_config, #wedge_config, #wedge_dom, wedge_dom, #wedge_from_client?, #wedge_from_server?, #wedge_function, wedge_html, #wedge_html, #wedge_javascript, #wedge_method_url, wedge_name, wedge_new, wedge_on, wedge_on_server, #wedge_plugin, #wedge_scope, #wedge_store, #wedge_tmpl, wedge_tmpl, #wedge_trigger

Constructor Details

This class inherits a constructor from Wedge::Plugins::Form

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Wedge::Plugins::Form

Instance Method Details

#authorize!(verb, object = nil) ⇒ Object

Ensures that the owner can perform ‘verb` on `object/class` – raises an error otherwise.



52
53
54
# File 'lib/wedge/plugins/ability_list.rb', line 52

def authorize!(verb, object=nil)
  can?(verb, object) or raise Error.new("Access denied (#{verb})")
end

#can(verb, klass = nil, columns = [], &block) ⇒ Object

Declares that the owner can perform ‘verb` on `class`.



19
20
21
22
# File 'lib/wedge/plugins/ability_list.rb', line 19

def can(verb, klass=nil, columns=[], &block)
  columns = [columns] unless columns.is_a? Array
  rules << [true, verb, get_class(klass), columns, block]
end

#can?(verb, object = nil, columns = []) ⇒ Boolean

Checks if the owner can perform ‘verb` on the given `object` (or class).

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
# File 'lib/wedge/plugins/ability_list.rb', line 33

def can?(verb, object=nil, columns=[])
  columns = [columns] unless columns.is_a? Array
  rules = rules_for(verb, get_class(object))
  rules.inject(false) do |bool, (sign, _, _, cols, proc)|
    sign ?
      ((bool || !proc ||  proc.call(object)) && ((columns & cols) == columns)) :  # can
      (bool &&  proc && !proc.call(object) && (columns.empty? || (columns & cols) != columns))    # cannot
  end
end

#cannot(verb, klass = nil, columns = [], &block) ⇒ Object

Inverse of ‘can`.



25
26
27
28
# File 'lib/wedge/plugins/ability_list.rb', line 25

def cannot(verb, klass=nil, columns=[], &block)
  columns = [columns] unless columns.is_a? Array
  rules << [false, verb, get_class(klass), columns, block]
end

#cannot?(verb, object = nil, columns = []) ⇒ Boolean

Inverse of ‘can?`.

Returns:

  • (Boolean)


44
45
46
# File 'lib/wedge/plugins/ability_list.rb', line 44

def cannot?(verb, object=nil, columns=[])
  !can?(verb, object, columns)
end

#rulesObject

Returns a list of rules. These are populated by ‘can` and `cannot`. (Rules are tuples)



12
13
14
# File 'lib/wedge/plugins/ability_list.rb', line 12

def rules
  @rules ||= []
end

#rules_for(verb, klass) ⇒ Object

Returns a subset of ‘rules` that match the given `verb` and `class`.



64
65
66
67
68
69
# File 'lib/wedge/plugins/ability_list.rb', line 64

def rules_for(verb, klass)
  rules.select do |(sign, _verb, _klass, cols, block)|
    (_verb  == :manage || _verb  == verb) &&
    (_klass == :all    || _klass == klass)
  end
end

#unauthorize!(verb, object = nil) ⇒ Object

Inverse of ‘authorize!`.



57
58
59
# File 'lib/wedge/plugins/ability_list.rb', line 57

def unauthorize!(verb, object=nil)
  cannot?(verb, object) or raise Error.new("Access denied (#{verb})")
end