Class: Thwart::ActionGroupBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/thwart/action_group_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actions_store = Thwart::Actions) ⇒ ActionGroupBuilder

Returns a new instance of ActionGroupBuilder.



52
53
54
55
56
57
58
59
# File 'lib/thwart/action_group_builder.rb', line 52

def initialize(actions_store = Thwart::Actions)
  builder = self
  @actions_store = actions_store
  @actions_store.class.set_callback :add, :after do |object|
    builder.add_actionable(actions_store.last_action)
  end
  builder
end

Instance Attribute Details

#actionablesObject

Holds all the different action groups in actionables => [array of resolved actions]



4
5
6
# File 'lib/thwart/action_group_builder.rb', line 4

def actionables
  @actionables
end

Instance Method Details

#add_actionable(name, actions = nil) ⇒ Object

Adds an actionable to the list of actionable things.

Parameters:

  • name (symbol)

    The name of the action

  • actions (symbol|array|nil) (defaults to: nil)

    The actions this actionable resolves to. If nil, this is set to the name argument.



13
14
15
16
17
18
19
20
# File 'lib/thwart/action_group_builder.rb', line 13

def add_actionable(name, actions = nil)
  if actions.nil?
    actions = Array.wrap(name)
  else
    actions = Array.wrap(actions)
  end 
  self.actionables[name] = actions
end

#add_crud_group!Object

Adds the :create, :read, :update, and :destroy actionables



44
45
46
47
48
49
50
# File 'lib/thwart/action_group_builder.rb', line 44

def add_crud_group!
  @actions_store.add_crud! if @actions_store.respond_to?(:add_crud!)
  if @crud.nil? || @crud == false    
    self.create_action_group(:crud, Thwart::CrudActions.keys)
    @crud = true
  end
end

#create_action_group(name, others) ⇒ Object

Creates an action group from an array of actionables.

Parameters:

  • name (symbol)

    The name of the new action group

  • others (symbol|array)

    One or an array symbols reffering to action or action groups



25
26
27
# File 'lib/thwart/action_group_builder.rb', line 25

def create_action_group(name, others)
  self.add_actionable(name, resolve_action_group(others))
end

#resolve_action_group(name) ⇒ Object

Resolves some actionables recursively down to the raw actions it corresponds to.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/thwart/action_group_builder.rb', line 31

def resolve_action_group(name)
  # - if name is an array => resolve it recursively
  # - if name is in the action groups, pull out its existing resolution
  # - otherwise, raise an error because we don't know what this is.
  # Simple action groups (an action itself) must be added to the actions before they
  # are referenced, which is accomplished using the :save callback on Actions
  
  return name.map{|n| resolve_action_group(n)}.flatten.uniq if name.respond_to?(:map)
  return self.actionables[name].flatten.uniq if self.actionables.include?(name)
  raise Thwart::ActionOrGroupNotFoundError, "Action or group #{name} could not be found!"
end