Module: Volt::Actions::ClassMethods

Defined in:
lib/volt/controllers/actions.rb

Instance Method Summary collapse

Instance Method Details

#setup_action_helpers_in_class(*groups) ⇒ Object

Takes a list of action groups (as symbols). An action group is typically used for before/after, but can be used anytime you have multiple sets of actions. The method will create an x_action method for each group passed in.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/volt/controllers/actions.rb', line 21

def setup_action_helpers_in_class(*groups)
  groups.each do |group|
    # Setup a class attribute to track the
    callbacks_var_name = :"#{group}_action_callbacks"
    class_attribute(callbacks_var_name)

    # Create the method on the class
    define_singleton_method(:"#{group}_action") do |*args, &block|
      # Add the block in place of the symbol
      args.unshift(block) if block

      fail 'No callback symbol or block provided' unless args[0]

      callbacks = send(callbacks_var_name)

      unless callbacks
        callbacks = []
        send(:"#{callbacks_var_name}=", callbacks)
      end

      if args.last.is_a?(Hash)
        options = args.pop
      else
        options = nil
      end

      args.each do |callback|
        callbacks << [callback, options]
      end
    end
  end
end