Class: RSpec::Authorization::Adapters::RestfulHelperMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/authorization/adapters/restful_helper_method.rb

Overview

Create a new restful helper method using the available dictionaries. Method that is not available raise a NoMethodError, consider the following example:

RestfulHelperMethod.new(:to_read)

This class can be inferred automatically as an array, for example on multiple assignment, consider the following example:

behavior, actions = RestfulHelperMethod(:to_read)
behavior # => :read
actions  # => [:index, :show]

RESTful helper methods

Currently available helper method are:

  • to_read

  • to_create

  • to_update

  • to_delete

  • to_manage

The above method is not related to declarative_authorization privileges, and serve simply as convinience method, below is a table of restful actions returned from the restful helper method:

Method        RESTful actions
----------------------------------------------------------------------
to_read       [:index, :show]
to_create     [:new, :create]
to_update     [:edit, :update]
to_delete     [:destroy]
to_manage     [:index, :show, :new, :create, :edit, :update, :destroy]

Focused RESTful helper methods

Currently available focused helper methods are:

  • only_to_read

  • only_to_create

  • only_to_update

  • only_to_delete

And their negated counterparts are:

  • except_to_read

  • except_to_create

  • except_to_update

  • except_to_delete

The above helper methods have a different action table compared to the regular helper methods, this is due to it's nature to focus only on certain behavior, and it negates other actions:

Method         Focused actions   Negated actions
-------------------------------------------------------------------------
only_to_read   [:index, :show]   [:new, :create, :edit, :update, :delete]
only_to_create [:new, :create]   [:index, :show, :edit, :update, :delete]
only_to_update [:edit, :update]  [:index, :show, :new, :create, :delete]
only_to_delete [:destroy]        [:index, :show, :new, :create, :edit, :update]

The negated focused helper methods have exactly the opposite matching table, following is actions table for negated focused helper methods:

Method           Focused actions                                Negated actions
-------------------------------------------------------------------------------
except_to_read   [:new, :create, :edit, :update, :delete]       [:index, :show]
except_to_create [:index, :show, :edit, :update, :delete]       [:new, :create]
except_to_update [:index, :show, :new, :create, :delete]        [:edit, :update]
except_to_delete [:index, :show, :new, :create, :edit, :update] [:destroy]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ RestfulHelperMethod

Creates a restful helper method using the available dictionaries. Invalid or a non-available helper method that passed in raise an error, consider the following example:

RestfulHelperMethod.new(:to_explode) # this will explode
=> NoMethodError: undefined method `to_explode' for RestfulHelperMethod

Raises:

  • (NoMethodError)

95
96
97
98
99
100
101
102
103
104
# File 'lib/rspec/authorization/adapters/restful_helper_method.rb', line 95

def initialize(name)
  @name, @prefix, @behavior = find_method(name).to_a.map(&:to_sym)

  raise NoMethodError, error_message unless DICTIONARIES.key?(behavior)

  @actions         = DICTIONARIES[behavior]
  @negated_actions = prefix.eql?(:to) ? [] : (DICTIONARIES[:manage] - actions)

  swap_negated_actions if prefix.eql?(:except_to)
end

Instance Attribute Details

#actionsArray<Symbol> (readonly)


83
84
85
# File 'lib/rspec/authorization/adapters/restful_helper_method.rb', line 83

def actions
  @actions
end

#behaviorSymbol (readonly)


81
82
83
# File 'lib/rspec/authorization/adapters/restful_helper_method.rb', line 81

def behavior
  @behavior
end

#nameSymbol (readonly)


79
80
81
# File 'lib/rspec/authorization/adapters/restful_helper_method.rb', line 79

def name
  @name
end

#negated_actionsArray<Symbol> (readonly)


85
86
87
# File 'lib/rspec/authorization/adapters/restful_helper_method.rb', line 85

def negated_actions
  @negated_actions
end

#prefixSymbol (readonly)


77
78
79
# File 'lib/rspec/authorization/adapters/restful_helper_method.rb', line 77

def prefix
  @prefix
end

Instance Method Details

#humanizeObject


106
107
108
# File 'lib/rspec/authorization/adapters/restful_helper_method.rb', line 106

def humanize
  name.to_s.gsub("_", " ")
end