Class: Multiview::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/multiview/manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(versions_map) ⇒ Manager

Params:

config: {controller_path: version


7
8
9
# File 'lib/multiview/manager.rb', line 7

def initialize(versions_map)
  @versions_map = versions_map.symbolize_keys
end

Instance Attribute Details

#versions_mapObject (readonly)

Returns the value of attribute versions_map.



3
4
5
# File 'lib/multiview/manager.rb', line 3

def versions_map
  @versions_map
end

Instance Method Details

#dispatch(env, controller_path, action_name, version = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/multiview/manager.rb', line 11

def dispatch(env, controller_path, action_name, version = nil)
  version ||= find_version(controller_path)
  return if version.nil?

  class_name = "#{version}/#{controller_path}_controller".camelize
  ctrl_cls = class_name.safe_constantize
  new_action_name = "#{class_name}##{action_name}"
  env['multiview'] = {version: version}.with_indifferent_access

  if ctrl_cls && ctrl_cls.public_method_defined?(action_name)
    Rails.logger.debug("[Multiview] Dispatch to #{new_action_name}")
    action_block = ctrl_cls.action(action_name)
    action_block.call(env)
  else
    Rails.logger.warn("[Multiview] Not found #{new_action_name}")
    nil
  end
end

#find_version(path) ⇒ Object



50
51
52
# File 'lib/multiview/manager.rb', line 50

def find_version(path)
  versions_map[path.to_sym]
end

#redispatch(controller, controller_path = nil, action_name = nil, version = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/multiview/manager.rb', line 30

def redispatch(controller, controller_path = nil, action_name = nil, version = nil)
  controller_path ||= controller.params[:controller]
  action_name ||= controller.params[:action]
  version ||= find_version(controller_path)
  return unless version

  result = try_dispatch(controller.request.env, controller_path, action_name, version)
  if result
    res = controller.response
    status, headers, body = result
    res.status = status
    res.header.clear
    res.header.merge!(headers)
    controller.response_body = body
  else
    # no action, just use views
    load_version_view(controller, version)
  end
end