Sha256: 9af75aeec6438570cb9494e51962536a339d5686393afa898b4f0746a2437fa7
Contents?: true
Size: 1.88 KB
Versions: 1
Compression:
Stored size: 1.88 KB
Contents
module Multiview class Manager attr_reader :versions_map # Params: # config: {controller_path: version def initialize(versions_map) @versions_map = versions_map end 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 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 if result = dispatch(controller.request.env, controller_path, action_name, version) 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 def find_version(path) versions_map[path.to_sym] end private def load_version_view(controller, version) return if version.to_sym == :v1 Rails.logger.warn("[Multiview] Prepend view path app/views/#{version}") controller.prepend_view_path(Rails.root.join("app/views/#{version}")) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
multiview-0.1.0 | lib/multiview/manager.rb |