backport_new_renderer 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/backport_new_renderer.rb +144 -0
  3. metadata +57 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 59fe57a08551c70a3911309349cd9d7c63e60878
4
+ data.tar.gz: 6e6d8fd80b2c37f153e2128144ac733e07212268
5
+ SHA512:
6
+ metadata.gz: f9d51bf08da39297a3621f5de0dc4622e686f15696b800c6981ac465999ff82d8b050021a8d2eda26002439a9cb68529e071c7393c666bebd07b86a6e22d060a
7
+ data.tar.gz: d473bd44eeb85535b9070756ea963c390f3791c4fea2c1741b0f3e4a3d01cda593ce91d8a1f6518b57f8ab81efddcc436042e2bd23d8c53d2fbf361fac5a5e65
@@ -0,0 +1,144 @@
1
+ require 'rails'
2
+
3
+ module ActionController
4
+ # ActionController::Renderer allows to render arbitrary templates
5
+ # without requirement of being in controller actions.
6
+ #
7
+ # You get a concrete renderer class by invoking ActionController::Base#renderer.
8
+ # For example,
9
+ #
10
+ # ApplicationController.renderer
11
+ #
12
+ # It allows you to call method #render directly.
13
+ #
14
+ # ApplicationController.renderer.render template: '...'
15
+ #
16
+ # You can use a shortcut on controller to replace previous example with:
17
+ #
18
+ # ApplicationController.render template: '...'
19
+ #
20
+ # #render method allows you to use any options as when rendering in controller.
21
+ # For example,
22
+ #
23
+ # FooController.render :action, locals: { ... }, assigns: { ... }
24
+ #
25
+ # The template will be rendered in a Rack environment which is accessible through
26
+ # ActionController::Renderer#env. You can set it up in two ways:
27
+ #
28
+ # * by changing renderer defaults, like
29
+ #
30
+ # ApplicationController.renderer.defaults # => hash with default Rack environment
31
+ #
32
+ # * by initializing an instance of renderer by passing it a custom environment.
33
+ #
34
+ # ApplicationController.renderer.new(method: 'post', https: true)
35
+ #
36
+ class Renderer
37
+ class_attribute :controller, :defaults
38
+ # Rack environment to render templates in.
39
+ attr_reader :env
40
+
41
+ class << self
42
+ delegate :render, to: :new
43
+
44
+ # Create a new renderer class for a specific controller class.
45
+ def for(controller)
46
+ Class.new self do
47
+ self.controller = controller
48
+ self.defaults = {
49
+ http_host: 'example.org',
50
+ https: false,
51
+ method: 'get',
52
+ script_name: '',
53
+ 'rack.input' => ''
54
+ }
55
+ end
56
+ end
57
+ end
58
+
59
+ # Accepts a custom Rack environment to render templates in.
60
+ # It will be merged with ActionController::Renderer.defaults
61
+ def initialize(env = {})
62
+ @env = normalize_keys(defaults).merge normalize_keys(env)
63
+ @env['action_dispatch.routes'] = controller._routes
64
+ end
65
+
66
+ # Render templates with any options from ActionController::Base#render_to_string.
67
+ def render(*args)
68
+ raise 'missing controller' unless controller?
69
+
70
+ instance = controller.build_with_env(env)
71
+ instance.render_to_string(*args)
72
+ end
73
+
74
+ private
75
+ def normalize_keys(env)
76
+ http_header_format(env).tap do |new_env|
77
+ handle_method_key! new_env
78
+ handle_https_key! new_env
79
+ end
80
+ end
81
+
82
+ def http_header_format(env)
83
+ env.transform_keys do |key|
84
+ key.is_a?(Symbol) ? key.to_s.upcase : key
85
+ end
86
+ end
87
+
88
+ def handle_method_key!(env)
89
+ if method = env.delete('METHOD')
90
+ env['REQUEST_METHOD'] = method.upcase
91
+ end
92
+ end
93
+
94
+ def handle_https_key!(env)
95
+ if env.has_key? 'HTTPS'
96
+ env['HTTPS'] = env['HTTPS'] ? 'on' : 'off'
97
+ end
98
+ end
99
+ end
100
+
101
+ class << Base
102
+ delegate :render, to: :renderer
103
+
104
+ # Returns a renderer class (inherited from ActionController::Renderer)
105
+ # for the controller.
106
+ def renderer
107
+ @renderer ||= Renderer.for(self)
108
+ end
109
+
110
+ def build_with_env(env = {}) #:nodoc:
111
+ new.tap { |c| c.set_request! ActionDispatch::Request.new(env) }
112
+ end
113
+ end
114
+
115
+ class Base
116
+ def dispatch(name, request) #:nodoc:
117
+ set_request!(request)
118
+ process(name)
119
+ to_a
120
+ end
121
+
122
+ def set_request!(request) #:nodoc:
123
+ @_request = request
124
+ @_env = request.env
125
+ @_env['action_controller.instance'] = self
126
+ @_response = ActionDispatch::Response.new
127
+ @_response.request = request
128
+ end
129
+ end
130
+ end
131
+
132
+ module ActionView::Rendering
133
+ def _render_template(options) #:nodoc:
134
+ variant = options.delete(:variant)
135
+ assigns = options.delete(:assigns)
136
+ context = view_context
137
+
138
+ context.assign assigns if assigns
139
+ lookup_context.rendered_format = nil if options[:formats]
140
+ lookup_context.variants = variant if variant
141
+
142
+ view_renderer.render(context, options)
143
+ end
144
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backport_new_renderer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - brainopia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - backport_new_renderer.rb
34
+ homepage:
35
+ licenses: []
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - "."
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.4.5
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Backport render anywhere feature from rails 5
57
+ test_files: []