render_component_4 4.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +20 -0
- data/README +37 -0
- data/lib/render_component.rb +4 -0
- data/lib/render_component/components.rb +159 -0
- data/lib/render_component/version.rb +9 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2b25925b5536281831d8c613483166489b67ec1d
|
4
|
+
data.tar.gz: 0cfbfd0052ab37972b3245cd897f043938fa057c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f132ae42ab636a1c75b4e707aceea9f37a88a4a192ea9e1ddfeaf3076ba1a4a2b8f58bb66ae2ac6b160593b5d492f7877ae45d083b8d1cb2a0853fd2d999f2aa
|
7
|
+
data.tar.gz: d4b51c2d3dac0094ba6bf3f3d6b41c501a138d4b82afe52495f14c0c814b1c8403fdca972a9763243bd2ecbbb5e162719b07e9aeceae93376b7ebaa31879888c
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Sergio Cambra
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
Components allow you to call other actions for their rendered response while executing another action. You can either delegate
|
2
|
+
the entire response rendering or you can mix a partial response in with your other content.
|
3
|
+
|
4
|
+
class WeblogController < ActionController::Base
|
5
|
+
# Performs a method and then lets hello_world output its render
|
6
|
+
def delegate_action
|
7
|
+
do_other_stuff_before_hello_world
|
8
|
+
render_component :controller => "greeter", :action => "hello_world", :params => { :person => "david" }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class GreeterController < ActionController::Base
|
13
|
+
def hello_world
|
14
|
+
render :text => "#{params[:person]} says, Hello World!"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
The same can be done in a view to do a partial rendering:
|
19
|
+
|
20
|
+
Let's see a greeting:
|
21
|
+
<%= render_component :controller => "greeter", :action => "hello_world" %>
|
22
|
+
|
23
|
+
It is also possible to specify the controller as a class constant, bypassing the inflector
|
24
|
+
code to compute the controller class at runtime:
|
25
|
+
|
26
|
+
<%= render_component :controller => GreeterController, :action => "hello_world" %>
|
27
|
+
|
28
|
+
== When to use components
|
29
|
+
|
30
|
+
Components should be used with care. They're significantly slower than simply splitting reusable parts into partials and
|
31
|
+
conceptually more complicated. Don't use components as a way of separating concerns inside a single application. Instead,
|
32
|
+
reserve components to those rare cases where you truly have reusable view and controller elements that can be employed
|
33
|
+
across many applications at once.
|
34
|
+
|
35
|
+
So to repeat: Components are a special-purpose approach that can often be replaced with better use of partials and filters.
|
36
|
+
|
37
|
+
Copyright (c) 2007 David Heinemeier Hansson, released under the MIT license
|
@@ -0,0 +1,159 @@
|
|
1
|
+
module RenderComponent
|
2
|
+
module Components
|
3
|
+
def self.included(base) #:nodoc:
|
4
|
+
base.class_eval do
|
5
|
+
include InstanceMethods
|
6
|
+
extend ClassMethods
|
7
|
+
helper HelperMethods
|
8
|
+
|
9
|
+
# If this controller was instantiated to process a component request,
|
10
|
+
# +parent_controller+ points to the instantiator of this controller.
|
11
|
+
attr_accessor :parent_controller
|
12
|
+
|
13
|
+
alias_method_chain :session, :render_component
|
14
|
+
# disabling since this is breaking flash..
|
15
|
+
#alias_method_chain :flash, :render_component
|
16
|
+
alias_method :component_request?, :parent_controller
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
# Track parent controller to identify component requests
|
22
|
+
def process_with_components(request, action, parent_controller = nil) #:nodoc:
|
23
|
+
controller = new
|
24
|
+
controller.parent_controller = parent_controller
|
25
|
+
controller.dispatch(action, request)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module HelperMethods
|
30
|
+
def render_component(options)
|
31
|
+
controller.send(:render_component_into_view, options)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
module InstanceMethods
|
36
|
+
|
37
|
+
protected
|
38
|
+
# Renders the component specified as the response for the current method
|
39
|
+
def render_component(options) #:doc:
|
40
|
+
component_logging(options) do
|
41
|
+
response = component_response(options, true)[2]
|
42
|
+
if response.redirect_url
|
43
|
+
redirect_to response.redirect_url
|
44
|
+
else
|
45
|
+
render :text => response.body, :status => response.status
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Returns the component response as a string
|
51
|
+
def render_component_into_view(options) #:doc:
|
52
|
+
component_logging(options) do
|
53
|
+
response = component_response(options, false)[2]
|
54
|
+
if redirected = response.redirect_url
|
55
|
+
if redirected =~ %r{://}
|
56
|
+
location = URI.parse(redirected)
|
57
|
+
redirected = location.query ? "#{location.path}?#{location.query}" : location.path
|
58
|
+
end
|
59
|
+
render_component_into_view(Rails.application.routes.recognize_path(redirected, { :method => nil }))
|
60
|
+
else
|
61
|
+
response.body.html_safe
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
def flash_with_render_component(refresh = false) #:nodoc:
|
68
|
+
if @component_flash.nil? || refresh
|
69
|
+
@component_flash =
|
70
|
+
if defined?(@parent_controller)
|
71
|
+
debugger
|
72
|
+
@parent_controller.flash
|
73
|
+
elsif session['flash'].class == String
|
74
|
+
warn "WARNING: something, somehow, has replaced the flash by a inspect.to_s output. >>>#{session['flash']}<<<"
|
75
|
+
session['flash'] = ActionDispatch::Flash::FlashHash.new
|
76
|
+
else
|
77
|
+
session['flash'] ||= ActionDispatch::Flash::FlashHash.new
|
78
|
+
end
|
79
|
+
end
|
80
|
+
@component_flash
|
81
|
+
end
|
82
|
+
|
83
|
+
def session_with_render_component
|
84
|
+
#if defined?(@parent_controller)
|
85
|
+
if component_request?
|
86
|
+
@parent_controller.session
|
87
|
+
else
|
88
|
+
@_request.session
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
def component_response(options, reuse_response)
|
94
|
+
options[:controller] = options[:controller].to_s if options[:controller] && options[:controller].is_a?(Symbol)
|
95
|
+
klass = component_class(options)
|
96
|
+
component_request = request_for_component(klass.controller_path, options)
|
97
|
+
# needed ???
|
98
|
+
#if reuse_response
|
99
|
+
#component_request.env["action_controller.instance"].instance_variable_set :@_response, request.env["action_controller.instance"].instance_variable_get(:@_response)
|
100
|
+
#end
|
101
|
+
klass.process_with_components(component_request, options[:action], self)
|
102
|
+
end
|
103
|
+
|
104
|
+
# determine the controller class for the component request
|
105
|
+
def component_class(options)
|
106
|
+
if controller = options[:controller]
|
107
|
+
controller.is_a?(Class) ? controller : "#{controller.to_s.camelize}Controller".constantize
|
108
|
+
else
|
109
|
+
self.class
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# Create a new request object based on the current request.
|
114
|
+
# NOT IMPLEMENTED FOR RAILS 3 SO FAR: The new request inherits the session from the current request,
|
115
|
+
# bypassing any session options set for the component controller's class
|
116
|
+
def request_for_component(controller_path, options)
|
117
|
+
if options.is_a? Hash
|
118
|
+
old_style_params = options.delete(:params)
|
119
|
+
options.merge!(old_style_params) unless old_style_params.nil?
|
120
|
+
|
121
|
+
request_params = options.symbolize_keys
|
122
|
+
request_env = {}
|
123
|
+
|
124
|
+
request.env.select {|key, value| key == key.upcase || key == 'rack.input'}.each {|item| request_env[item[0]] = item[1]}
|
125
|
+
|
126
|
+
request_env['REQUEST_URI'] = url_for(options)
|
127
|
+
request_env["PATH_INFO"] = url_for(options.merge(:only_path => true))
|
128
|
+
request_env["action_dispatch.request.symbolized_path_parameters"] = request_params
|
129
|
+
request_env["action_dispatch.request.parameters"] = request_params.with_indifferent_access
|
130
|
+
request_env["action_dispatch.request.path_parameters"] = Hash[request_params.select{|key, value| [:controller, :action].include?(key)}].with_indifferent_access
|
131
|
+
request_env["warden"] = request.env["warden"] if (request.env.has_key?("warden"))
|
132
|
+
component_request = ActionDispatch::Request.new(request_env)
|
133
|
+
|
134
|
+
# its an internal request request forgery protection has to be disabled
|
135
|
+
# because otherwise forgery detection might raise an error
|
136
|
+
component_request.instance_eval do
|
137
|
+
def forgery_whitelisted?
|
138
|
+
true
|
139
|
+
end
|
140
|
+
end
|
141
|
+
component_request
|
142
|
+
else
|
143
|
+
request
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def component_logging(options)
|
148
|
+
if logger
|
149
|
+
logger.info "Start rendering component (#{options.inspect}): "
|
150
|
+
result = yield
|
151
|
+
logger.info "\n\nEnd of component rendering"
|
152
|
+
result
|
153
|
+
else
|
154
|
+
yield
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: render_component_4
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 4.1.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Heinemeier Hansson
|
8
|
+
- Sergio Cambra
|
9
|
+
- Mathieu Jobin
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2016-07-12 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: railties
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - "~>"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 4.1.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - "~>"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 4.1.0
|
29
|
+
description: Components allow you to call other actions for their rendered response
|
30
|
+
while executing another action
|
31
|
+
email:
|
32
|
+
- david@loudthinking.com
|
33
|
+
- sergio@programatica.es
|
34
|
+
- mathieu@justbudget.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files:
|
38
|
+
- LICENSE.txt
|
39
|
+
- README
|
40
|
+
files:
|
41
|
+
- LICENSE.txt
|
42
|
+
- README
|
43
|
+
- lib/render_component.rb
|
44
|
+
- lib/render_component/components.rb
|
45
|
+
- lib/render_component/version.rb
|
46
|
+
homepage: http://github.com/somekool/render_component
|
47
|
+
licenses:
|
48
|
+
- MIT
|
49
|
+
metadata: {}
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 2.5.1
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: render actions in other controllers for their rendered response
|
70
|
+
test_files: []
|
71
|
+
has_rdoc:
|