Class: Thwart::ActionsStore

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/thwart/actions_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeActionsStore

Returns a new instance of ActionsStore.



8
9
10
# File 'lib/thwart/actions_store.rb', line 8

def initialize
  @actions = {}
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



5
6
7
# File 'lib/thwart/actions_store.rb', line 5

def actions
  @actions
end

#last_actionObject

Returns the value of attribute last_action.



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

def last_action
  @last_action
end

Instance Method Details

#add_crud!Object

Add the CRUD methods to the Thwart actions (create, read, update, destroy)



81
82
83
84
85
86
87
88
# File 'lib/thwart/actions_store.rb', line 81

def add_crud!
  if @crud.nil? || @crud == false      
    Thwart::CrudActions.each do |(k,v)|
      self.create_action(k, v)
    end
    @crud = true  
  end
end

#can_from_able(able) ⇒ Object

Finds the corresponding can from an able.

@param [Symbol] able_method The name of the [action-able]_by? method.


31
32
33
34
# File 'lib/thwart/actions_store.rb', line 31

def can_from_able(able)
  pair = self.actions.find {|k, v| v == able}
  pair.first if pair
end

#create_action(can, able = nil) ⇒ Object

Adds an action to actions and the correct methods to can and able modules.

@param [Symbol] can_method The name of the can_[action]? method.
@param [Symbol] resource_method The name of the [action-able]_by? method.


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/thwart/actions_store.rb', line 40

def create_action(can, able = nil)
  if able.nil?
    if can.respond_to?(:each) 
      can.each {|c,a| self.create_action(c,a)} 
    else
      raise ArgumentError, "able can't be nil"
    end
  else
    run_callbacks :add do
      @actions[can] = able
      @last_action = can
    end
  end
end

#find_able(name) ⇒ Object

Finds the able name of the action from a [action-able]_by? style method.

@param [Symbol] resource_method The name of the [action-able]_by? method.

Adds an action to actions and the correct methods to can and able modules.



59
60
61
62
63
64
65
66
# File 'lib/thwart/actions_store.rb', line 59

def find_able(name)
  md = name.to_s.match(/(.+)_by\?/)
  if md != nil && self.has_able?(md[1].intern)
    md[1].intern
  else
    false
  end
end

#find_can(name) ⇒ Object

Finds the can name of the action from a can_? style method.

@param [Symbol] can_method The name of the can_[action]? method.


71
72
73
74
75
76
77
78
# File 'lib/thwart/actions_store.rb', line 71

def find_can(name)
  md = name.to_s.match(/can_(.+)\?/)
  if md != nil && self.has_can?(md[1].intern)
    md[1].intern
  else
    false
  end
end

#has_able?(able) ⇒ Boolean

Returns true if Thwart is providing methods for the [action-able]_by? style action identifier, false otherwise.

@param [Symbol] able_method The name of the [action-able]_by? method to check for.

Returns:

  • (Boolean)


16
17
18
# File 'lib/thwart/actions_store.rb', line 16

def has_able?(able)
  self.actions.has_value?(able.to_sym)
end

#has_can?(can) ⇒ Boolean

Returns true if Thwart is providing methods for the can_? style action identifer, false otherwise.

@param [Symbol] able_method The name of the can_[action]? method to check for.

Returns:

  • (Boolean)


24
25
26
# File 'lib/thwart/actions_store.rb', line 24

def has_can?(can)
  self.actions.has_key?(can.to_sym)
end