Class: AppKit::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/app_kit/action.rb

Overview

The action class is used to generate custom actions that can be performed on a given record. These actions are displayed when viewing a specific record. Actions are defiend in the main Resource DSL.

DSL Examples

Inline example

action :deactivate, :if => :active

Block example

action :deactivate, :if => :active do |record|
    record.update(active: false)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, resource, options = {}, &block) ⇒ Action

This class should be created from the Resource DSL and should not be created manually.



45
46
47
48
49
50
# File 'lib/app_kit/action.rb', line 45

def initialize(name, resource, options = {}, &block)
    @name = name
    @block = block if block_given?
    @resource = resource
    @if_method = options[:if]
end

Instance Attribute Details

#blockObject

The block to be executed when the aciton is performed.

action :deactive do |record|
   record.update(active: false)
end


31
32
33
# File 'lib/app_kit/action.rb', line 31

def block
  @block
end

#if_methodObject

A method to call on the model to determin if this action should be displayed.

action :deactivate, :if => :active


38
39
40
# File 'lib/app_kit/action.rb', line 38

def if_method
  @if_method
end

#nameObject

The name of the aciton. This will be used to display the link title of the action itself. It will also be used as a the name of the method on the model class to execute if a block is not given.

action :consolidate_items

This action would create a custom action visible when viewing a resource record. This action will also execute a method named consolidate_items on the model instance.



25
26
27
# File 'lib/app_kit/action.rb', line 25

def name
  @name
end

#resourceObject

The resoruce this action belongs to.



34
35
36
# File 'lib/app_kit/action.rb', line 34

def resource
  @resource
end

Instance Method Details

#action_typeSymbol

Determines if the action should call to a method or execute a block.



68
69
70
# File 'lib/app_kit/action.rb', line 68

def action_type
    (block.present? ? :block : :method_call)
end

#display_nameString

The display name to use when displaying a link to the action.



54
55
56
# File 'lib/app_kit/action.rb', line 54

def display_name 
    name.humanize
end

#enabled_for_record?(record) ⇒ Boolean

Determines if the action should be displayed



61
62
63
64
# File 'lib/app_kit/action.rb', line 61

def enabled_for_record?(record)
    return true unless if_method
    true if record.send(if_method) 
end

#is_block_action?Boolean

Convienence method that detmines if the action should call a block.



78
79
80
# File 'lib/app_kit/action.rb', line 78

def is_block_action?
    action_type == :block
end

#is_method_action?Boolean

Convienence method that detmines if the action should call a model method.



73
74
75
# File 'lib/app_kit/action.rb', line 73

def is_method_action?
    action_type == :method_call
end

#method_nameSymbol

The method name to call on the model.



84
85
86
# File 'lib/app_kit/action.rb', line 84

def method_name
    name
end