Sha256: a1cdaeac1e51384b91f91cc62beb206525037ba3a324056a878c07d341a9710f

Contents?: true

Size: 1.82 KB

Versions: 8

Compression:

Stored size: 1.82 KB

Contents

module Volt
  class ControllerHandler
    attr_reader :controller, :action

    def initialize(controller, action)
      @controller = controller
      @action = action.to_sym if action
    end

    def call_action(stage_prefix = nil, stage_suffix = nil)
      return unless @action

      has_stage = stage_prefix || stage_suffix

      if has_stage
        method_name = @action
        method_name = "#{stage_prefix}_#{method_name}" if stage_prefix
        method_name = "#{method_name}_#{stage_suffix}" if stage_suffix

        method_name = method_name.to_sym
      else
        method_name = @action
      end

      # If no stage, then we are calling the main action method,
      # so we should call the before/after actions
      unless has_stage
        if @controller.run_actions(:before, @action)
          # stop_chain was called
          return true
        end
      end

      @controller.send(method_name) if @controller.respond_to?(method_name)

      @controller.run_actions(:after, @action) unless has_stage

      # before_action chain was not stopped
      false
    end

    # Fetch the controller class
    def self.get_controller_and_action(controller_path)
      fail "Invalid controller path: #{controller_path.inspect}" unless controller_path && controller_path.size > 0

      action = controller_path[-1]

      # Get the constant parts
      parts  = controller_path[0..-2].map { |v| v.tr('-', '_').camelize }

      # Do const lookups starting at object and working our way down.
      # So Volt::ProgressBar would lookup Volt, then ProgressBar on Volt.
      obj = Object
      parts.each do |part|
        if obj.const_defined?(part)
          obj = obj.const_get(part)
        else
          # return a blank ModelController
          return [ModelController, nil]
        end
      end

      [obj, action]
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
volt-0.9.3 lib/volt/page/bindings/view_binding/controller_handler.rb
volt-0.9.3.pre6 lib/volt/page/bindings/view_binding/controller_handler.rb
volt-0.9.3.pre5 lib/volt/page/bindings/view_binding/controller_handler.rb
volt-0.9.3.pre4 lib/volt/page/bindings/view_binding/controller_handler.rb
volt-0.9.3.pre3 lib/volt/page/bindings/view_binding/controller_handler.rb
volt-0.9.3.pre2 lib/volt/page/bindings/view_binding/controller_handler.rb
volt-0.9.3.pre1 lib/volt/page/bindings/view_binding/controller_handler.rb
volt-0.9.2 lib/volt/page/bindings/view_binding/controller_handler.rb