Class: Cathode::Action

Inherits:
Object
  • Object
show all
Includes:
ActionDsl
Defined in:
lib/cathode/action.rb

Overview

An ‘Action` can be added to a Resource or a Version and contains the default behavior of that action (if it is a default action), or the override behavior if it is a custom action or an overridden default action.

Defined Under Namespace

Modules: RequiresAssociation, RequiresCustomActionForSingular, RequiresHasManyAssociation, RequiresHasOneAssociation, RequiresStrongParams

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActionDsl

#actions, #custom_actions, #default_actions

Constructor Details

#initialize(action, resource, params = {}, &block) ⇒ Action

Initializes the action

Parameters:

  • action (Symbol)

    The action’s name

  • resource (Resource)

    The resource the action belongs to

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

    An optional params hash

  • block

    The action’s properties, defined with the Cathode::ActionDsl



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cathode/action.rb', line 50

def initialize(action, resource, params = {}, &block)
  @name, @resource = action, resource
  @allowed_subactions = []

  if block_given?
    if params[:override]
      override &block
    else
      if [:index, :show, :create, :update, :destroy].include? action
        instance_eval &block
      else 
        @action_block = block
      end
    end
  end

  @http_method = params[:method] if params.present?

  after_initialize if respond_to? :after_initialize
end

Instance Attribute Details

#action_access_filterObject (readonly)

Returns the value of attribute action_access_filter.



9
10
11
# File 'lib/cathode/action.rb', line 9

def action_access_filter
  @action_access_filter
end

#action_blockObject (readonly)

Returns the value of attribute action_block.



9
10
11
# File 'lib/cathode/action.rb', line 9

def action_block
  @action_block
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



9
10
11
# File 'lib/cathode/action.rb', line 9

def http_method
  @http_method
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/cathode/action.rb', line 9

def name
  @name
end

#override_blockObject (readonly)

Returns the value of attribute override_block.



9
10
11
# File 'lib/cathode/action.rb', line 9

def override_block
  @override_block
end

#resourceObject (readonly)

Returns the value of attribute resource.



9
10
11
# File 'lib/cathode/action.rb', line 9

def resource
  @resource
end

#strong_paramsObject

Returns the value of attribute strong_params.



8
9
10
# File 'lib/cathode/action.rb', line 8

def strong_params
  @strong_params
end

Class Method Details

.create(action, resource, params = nil, &block) ⇒ IndexAction, ...

Creates an action by initializing the appropriate subclass

Parameters:

  • action (Symbol)

    The action’s name

  • resource (Resource)

    The resource the action belongs to

  • params (Hash) (defaults to: nil)

    An optional params hash

  • block

    The action’s properties, defined with the Cathode::ActionDsl

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cathode/action.rb', line 26

def create(action, resource, params = nil, &block)
  klass = case action
          when :index
            IndexAction
          when :show
            ShowAction
          when :create
            CreateAction
          when :update
            UpdateAction
          when :destroy
            DestroyAction
          else
            CustomAction
          end
  klass.new(action, resource, params, &block)
end

Instance Method Details

#allowed?(subaction) ⇒ Boolean

Whether a subaction is permitted

Parameters:

  • subaction (Symbol)

    The subaction’s name

Returns:

  • (Boolean)


74
75
76
# File 'lib/cathode/action.rb', line 74

def allowed?(subaction)
  @allowed_subactions.include? subaction
end