Module: ActionControl

Defined in:
lib/action_control/errors.rb,
lib/action_control.rb,
lib/action_control/railtie.rb,
lib/action_control/version.rb,
lib/action_control/controller_methods.rb

Overview

Helper methods for your controller to identify RESTful actions.

Author:

  • Tobias Feistmantl

Defined Under Namespace

Classes: AuthenticationError, AuthenticationNotPerformedError, AuthorizationError, AuthorizationNotPerformedError, NotAuthenticatedError, NotAuthorizedError, Railtie

Constant Summary collapse

VERSION =
"0.2"

Instance Method Summary collapse

Instance Method Details

#authenticate!Object

Authenticates the user



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/action_control.rb', line 21

def authenticate!
  # Raise an error if the #authenticate? action isn't defined.
  #
  # This ensures that you actually do authentication in your controller.
  raise ActionControl::AuthenticationNotPerformedError unless defined?(authenticated?)
  
  # If the authenticated? method returns not true
  # it raises the ActionControl::NotAuthenticatedError.
  #
  # Use the .rescue_from method from ActionController::Base
  # to catch the exception and show the user a proper error message.
  raise ActionControl::NotAuthenticatedError unless authenticated? == true
end

#authorize!Object

Authorizes the user.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/action_control.rb', line 6

def authorize!
  # Raise an error if the #authorize? action isn't defined.
  #
  # This ensures that you actually do authorization in your controller. 
  raise ActionControl::AuthorizationNotPerformedError unless defined?(authorized?)
  
  # If the authorized? method does not return true
  # it raises the ActionControl::NotAuthorizedError
  #
  # Use the .rescue_from method from ActionController::Base
  # to catch the exception and show the user a proper error message.
  raise ActionControl::NotAuthorizedError unless authorized? == true
end

#change_action?Boolean



28
29
30
31
32
# File 'lib/action_control/controller_methods.rb', line 28

def change_action?
  action_name == 'edit' ||
  action_name == 'update' ||
  action_name == 'destroy'
end

#create_action?Boolean

Note:

Also true for the pseudo update action ‘new`.

Note:

Only true for create methods such as new and create.

Returns True if the called action is a create action.



59
60
61
62
# File 'lib/action_control/controller_methods.rb', line 59

def create_action?
  action_name == 'new' ||
  action_name == 'create'
end

#destroy_action?Boolean Also known as: delete_action?



78
79
80
# File 'lib/action_control/controller_methods.rb', line 78

def destroy_action?
  action_name == 'destroy'
end

#index_action?Boolean



37
38
39
# File 'lib/action_control/controller_methods.rb', line 37

def index_action?
  action_name == 'index'
end

#read_action?Boolean



9
10
11
12
# File 'lib/action_control/controller_methods.rb', line 9

def read_action?
  action_name == 'index' ||
  action_name == 'show'
end

#show_action?Boolean



44
45
46
# File 'lib/action_control/controller_methods.rb', line 44

def show_action?
  action_name == 'show'
end

#update_action?Boolean

Note:

Also true for the pseudo update action ‘edit`.

Returns True if the called action is a update action.



71
72
73
74
# File 'lib/action_control/controller_methods.rb', line 71

def update_action?
  action_name == 'edit' ||
  action_name == 'update'
end

#write_action?Boolean



17
18
19
20
21
22
23
# File 'lib/action_control/controller_methods.rb', line 17

def write_action?
  action_name == 'new' ||
  action_name == 'create' ||
  action_name == 'edit' ||
  action_name == 'update' ||
  action_name == 'destroy'
end