class RSpec::Authorization::Adapters::RestfulHelperMethod

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:

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:

And their negated counterparts are:

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]

Constants

DICTIONARIES

Attributes

actions[R]

@return [Array<Symbol>] The list of actions for a given behavior

behavior[R]

@return [Symbol] The behavior of the restful helper method

name[R]

@return [Symbol] Restful helper method name

negated_actions[R]

@return [Array<Symbol>] The list of negated actions for a given behavior

prefix[R]

@return [Symbol] Restful helper method prefix

Public Class Methods

new(name) click to toggle source

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

@param name [Symbol] restful method name to be retrieved

# 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

Public Instance Methods

humanize() click to toggle source
# File lib/rspec/authorization/adapters/restful_helper_method.rb, line 106
def humanize
  name.to_s.gsub("_", " ")
end

Private Instance Methods

error_message() click to toggle source
# File lib/rspec/authorization/adapters/restful_helper_method.rb, line 112
def error_message
  "undefined method `#{name}' for #{self.class}"
end
find_method(name) click to toggle source
# File lib/rspec/authorization/adapters/restful_helper_method.rb, line 116
def find_method(name)
  /^(to|only_to|except_to)_(.*)$/.match(name)
end
swap_negated_actions() click to toggle source
# File lib/rspec/authorization/adapters/restful_helper_method.rb, line 120
def swap_negated_actions
  @actions, @negated_actions = negated_actions, actions
end
to_a() click to toggle source
# File lib/rspec/authorization/adapters/restful_helper_method.rb, line 124
def to_a
  [actions, negated_actions]
end