Module: Granite::Projector::ControllerActions::ClassMethods

Defined in:
lib/granite/projector/controller_actions.rb

Instance Method Summary collapse

Instance Method Details

#action(name, options = {}, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/granite/projector/controller_actions.rb', line 20

def action(name, options = {}, &block)
  if block
    self.controller_actions = controller_actions.merge(name.to_sym => options)
    controller_class.__send__(:define_method, name, &block)
    class_eval <<-METHOD, __FILE__, __LINE__ + 1
      def #{name}_url(options = {})                     # def foo_url(options = {}
        action_url(:#{name}, **options.symbolize_keys)  #   action_url(:foo, **options.symbolize_keys)
      end                                               # end
                                                        #
      def #{name}_path(options = {})                    # def foo_path(options = {})
        action_path(:#{name}, **options.symbolize_keys) #   action_path(:foo, **options.symbolize_keys)
      end                                               # end
    METHOD
  else
    controller_actions[name.to_sym]
  end
end

#action_for(http_method, action) ⇒ Object



38
39
40
41
42
43
# File 'lib/granite/projector/controller_actions.rb', line 38

def action_for(http_method, action)
  controller_actions.find do |controller_action, controller_action_options|
    controller_action_options.fetch(:as, controller_action).to_s == action &&
      Array(controller_action_options.fetch(:method)).include?(http_method)
  end&.first
end