actionview 4.1.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of actionview might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/CHANGELOG.md +274 -0
- data/MIT-LICENSE +21 -0
- data/README.rdoc +34 -0
- data/lib/action_view.rb +97 -0
- data/lib/action_view/base.rb +205 -0
- data/lib/action_view/buffers.rb +49 -0
- data/lib/action_view/context.rb +36 -0
- data/lib/action_view/dependency_tracker.rb +93 -0
- data/lib/action_view/digestor.rb +116 -0
- data/lib/action_view/flows.rb +76 -0
- data/lib/action_view/helpers.rb +64 -0
- data/lib/action_view/helpers/active_model_helper.rb +49 -0
- data/lib/action_view/helpers/asset_tag_helper.rb +322 -0
- data/lib/action_view/helpers/asset_url_helper.rb +355 -0
- data/lib/action_view/helpers/atom_feed_helper.rb +203 -0
- data/lib/action_view/helpers/cache_helper.rb +200 -0
- data/lib/action_view/helpers/capture_helper.rb +216 -0
- data/lib/action_view/helpers/controller_helper.rb +25 -0
- data/lib/action_view/helpers/csrf_helper.rb +30 -0
- data/lib/action_view/helpers/date_helper.rb +1075 -0
- data/lib/action_view/helpers/debug_helper.rb +39 -0
- data/lib/action_view/helpers/form_helper.rb +1876 -0
- data/lib/action_view/helpers/form_options_helper.rb +843 -0
- data/lib/action_view/helpers/form_tag_helper.rb +746 -0
- data/lib/action_view/helpers/javascript_helper.rb +75 -0
- data/lib/action_view/helpers/number_helper.rb +425 -0
- data/lib/action_view/helpers/output_safety_helper.rb +38 -0
- data/lib/action_view/helpers/record_tag_helper.rb +108 -0
- data/lib/action_view/helpers/rendering_helper.rb +90 -0
- data/lib/action_view/helpers/sanitize_helper.rb +256 -0
- data/lib/action_view/helpers/tag_helper.rb +176 -0
- data/lib/action_view/helpers/tags.rb +41 -0
- data/lib/action_view/helpers/tags/base.rb +148 -0
- data/lib/action_view/helpers/tags/check_box.rb +64 -0
- data/lib/action_view/helpers/tags/checkable.rb +16 -0
- data/lib/action_view/helpers/tags/collection_check_boxes.rb +44 -0
- data/lib/action_view/helpers/tags/collection_helpers.rb +85 -0
- data/lib/action_view/helpers/tags/collection_radio_buttons.rb +36 -0
- data/lib/action_view/helpers/tags/collection_select.rb +28 -0
- data/lib/action_view/helpers/tags/color_field.rb +25 -0
- data/lib/action_view/helpers/tags/date_field.rb +13 -0
- data/lib/action_view/helpers/tags/date_select.rb +72 -0
- data/lib/action_view/helpers/tags/datetime_field.rb +22 -0
- data/lib/action_view/helpers/tags/datetime_local_field.rb +19 -0
- data/lib/action_view/helpers/tags/datetime_select.rb +8 -0
- data/lib/action_view/helpers/tags/email_field.rb +8 -0
- data/lib/action_view/helpers/tags/file_field.rb +8 -0
- data/lib/action_view/helpers/tags/grouped_collection_select.rb +29 -0
- data/lib/action_view/helpers/tags/hidden_field.rb +8 -0
- data/lib/action_view/helpers/tags/label.rb +65 -0
- data/lib/action_view/helpers/tags/month_field.rb +13 -0
- data/lib/action_view/helpers/tags/number_field.rb +18 -0
- data/lib/action_view/helpers/tags/password_field.rb +12 -0
- data/lib/action_view/helpers/tags/radio_button.rb +31 -0
- data/lib/action_view/helpers/tags/range_field.rb +8 -0
- data/lib/action_view/helpers/tags/search_field.rb +24 -0
- data/lib/action_view/helpers/tags/select.rb +41 -0
- data/lib/action_view/helpers/tags/tel_field.rb +8 -0
- data/lib/action_view/helpers/tags/text_area.rb +18 -0
- data/lib/action_view/helpers/tags/text_field.rb +29 -0
- data/lib/action_view/helpers/tags/time_field.rb +13 -0
- data/lib/action_view/helpers/tags/time_select.rb +8 -0
- data/lib/action_view/helpers/tags/time_zone_select.rb +20 -0
- data/lib/action_view/helpers/tags/url_field.rb +8 -0
- data/lib/action_view/helpers/tags/week_field.rb +13 -0
- data/lib/action_view/helpers/text_helper.rb +447 -0
- data/lib/action_view/helpers/translation_helper.rb +111 -0
- data/lib/action_view/helpers/url_helper.rb +625 -0
- data/lib/action_view/layouts.rb +426 -0
- data/lib/action_view/locale/en.yml +56 -0
- data/lib/action_view/log_subscriber.rb +44 -0
- data/lib/action_view/lookup_context.rb +249 -0
- data/lib/action_view/model_naming.rb +12 -0
- data/lib/action_view/path_set.rb +77 -0
- data/lib/action_view/railtie.rb +49 -0
- data/lib/action_view/record_identifier.rb +84 -0
- data/lib/action_view/renderer/abstract_renderer.rb +47 -0
- data/lib/action_view/renderer/partial_renderer.rb +492 -0
- data/lib/action_view/renderer/renderer.rb +50 -0
- data/lib/action_view/renderer/streaming_template_renderer.rb +103 -0
- data/lib/action_view/renderer/template_renderer.rb +96 -0
- data/lib/action_view/rendering.rb +145 -0
- data/lib/action_view/routing_url_for.rb +109 -0
- data/lib/action_view/tasks/dependencies.rake +17 -0
- data/lib/action_view/template.rb +340 -0
- data/lib/action_view/template/error.rb +141 -0
- data/lib/action_view/template/handlers.rb +53 -0
- data/lib/action_view/template/handlers/builder.rb +26 -0
- data/lib/action_view/template/handlers/erb.rb +145 -0
- data/lib/action_view/template/handlers/raw.rb +11 -0
- data/lib/action_view/template/resolver.rb +329 -0
- data/lib/action_view/template/text.rb +34 -0
- data/lib/action_view/template/types.rb +57 -0
- data/lib/action_view/test_case.rb +272 -0
- data/lib/action_view/testing/resolvers.rb +50 -0
- data/lib/action_view/vendor/html-scanner.rb +20 -0
- data/lib/action_view/vendor/html-scanner/html/document.rb +68 -0
- data/lib/action_view/vendor/html-scanner/html/node.rb +532 -0
- data/lib/action_view/vendor/html-scanner/html/sanitizer.rb +188 -0
- data/lib/action_view/vendor/html-scanner/html/selector.rb +830 -0
- data/lib/action_view/vendor/html-scanner/html/tokenizer.rb +107 -0
- data/lib/action_view/vendor/html-scanner/html/version.rb +11 -0
- data/lib/action_view/version.rb +11 -0
- data/lib/action_view/view_paths.rb +96 -0
- metadata +218 -0
@@ -0,0 +1,47 @@
|
|
1
|
+
module ActionView
|
2
|
+
# This class defines the interface for a renderer. Each class that
|
3
|
+
# subclasses +AbstractRenderer+ is used by the base +Renderer+ class to
|
4
|
+
# render a specific type of object.
|
5
|
+
#
|
6
|
+
# The base +Renderer+ class uses its +render+ method to delegate to the
|
7
|
+
# renderers. These currently consist of
|
8
|
+
#
|
9
|
+
# PartialRenderer - Used for rendering partials
|
10
|
+
# TemplateRenderer - Used for rendering other types of templates
|
11
|
+
# StreamingTemplateRenderer - Used for streaming
|
12
|
+
#
|
13
|
+
# Whenever the +render+ method is called on the base +Renderer+ class, a new
|
14
|
+
# renderer object of the correct type is created, and the +render+ method on
|
15
|
+
# that new object is called in turn. This abstracts the setup and rendering
|
16
|
+
# into a separate classes for partials and templates.
|
17
|
+
class AbstractRenderer #:nodoc:
|
18
|
+
delegate :find_template, :template_exists?, :with_fallbacks, :with_layout_format, :formats, :to => :@lookup_context
|
19
|
+
|
20
|
+
def initialize(lookup_context)
|
21
|
+
@lookup_context = lookup_context
|
22
|
+
end
|
23
|
+
|
24
|
+
def render
|
25
|
+
raise NotImplementedError
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def extract_details(options)
|
31
|
+
@lookup_context.registered_details.each_with_object({}) do |key, details|
|
32
|
+
next unless value = options[key]
|
33
|
+
details[key] = Array(value)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def instrument(name, options={})
|
38
|
+
ActiveSupport::Notifications.instrument("render_#{name}.action_view", options){ yield }
|
39
|
+
end
|
40
|
+
|
41
|
+
def prepend_formats(formats)
|
42
|
+
formats = Array(formats)
|
43
|
+
return if formats.empty? || @lookup_context.html_fallback_for_js
|
44
|
+
@lookup_context.formats = formats | @lookup_context.formats
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,492 @@
|
|
1
|
+
require 'thread_safe'
|
2
|
+
|
3
|
+
module ActionView
|
4
|
+
# = Action View Partials
|
5
|
+
#
|
6
|
+
# There's also a convenience method for rendering sub templates within the current controller that depends on a
|
7
|
+
# single object (we call this kind of sub templates for partials). It relies on the fact that partials should
|
8
|
+
# follow the naming convention of being prefixed with an underscore -- as to separate them from regular
|
9
|
+
# templates that could be rendered on their own.
|
10
|
+
#
|
11
|
+
# In a template for Advertiser#account:
|
12
|
+
#
|
13
|
+
# <%= render partial: "account" %>
|
14
|
+
#
|
15
|
+
# This would render "advertiser/_account.html.erb".
|
16
|
+
#
|
17
|
+
# In another template for Advertiser#buy, we could have:
|
18
|
+
#
|
19
|
+
# <%= render partial: "account", locals: { account: @buyer } %>
|
20
|
+
#
|
21
|
+
# <% @advertisements.each do |ad| %>
|
22
|
+
# <%= render partial: "ad", locals: { ad: ad } %>
|
23
|
+
# <% end %>
|
24
|
+
#
|
25
|
+
# This would first render "advertiser/_account.html.erb" with @buyer passed in as the local variable +account+, then
|
26
|
+
# render "advertiser/_ad.html.erb" and pass the local variable +ad+ to the template for display.
|
27
|
+
#
|
28
|
+
# == The :as and :object options
|
29
|
+
#
|
30
|
+
# By default <tt>ActionView::PartialRenderer</tt> doesn't have any local variables.
|
31
|
+
# The <tt>:object</tt> option can be used to pass an object to the partial. For instance:
|
32
|
+
#
|
33
|
+
# <%= render partial: "account", object: @buyer %>
|
34
|
+
#
|
35
|
+
# would provide the <tt>@buyer</tt> object to the partial, available under the local variable +account+ and is
|
36
|
+
# equivalent to:
|
37
|
+
#
|
38
|
+
# <%= render partial: "account", locals: { account: @buyer } %>
|
39
|
+
#
|
40
|
+
# With the <tt>:as</tt> option we can specify a different name for said local variable. For example, if we
|
41
|
+
# wanted it to be +user+ instead of +account+ we'd do:
|
42
|
+
#
|
43
|
+
# <%= render partial: "account", object: @buyer, as: 'user' %>
|
44
|
+
#
|
45
|
+
# This is equivalent to
|
46
|
+
#
|
47
|
+
# <%= render partial: "account", locals: { user: @buyer } %>
|
48
|
+
#
|
49
|
+
# == Rendering a collection of partials
|
50
|
+
#
|
51
|
+
# The example of partial use describes a familiar pattern where a template needs to iterate over an array and
|
52
|
+
# render a sub template for each of the elements. This pattern has been implemented as a single method that
|
53
|
+
# accepts an array and renders a partial by the same name as the elements contained within. So the three-lined
|
54
|
+
# example in "Using partials" can be rewritten with a single line:
|
55
|
+
#
|
56
|
+
# <%= render partial: "ad", collection: @advertisements %>
|
57
|
+
#
|
58
|
+
# This will render "advertiser/_ad.html.erb" and pass the local variable +ad+ to the template for display. An
|
59
|
+
# iteration counter will automatically be made available to the template with a name of the form
|
60
|
+
# +partial_name_counter+. In the case of the example above, the template would be fed +ad_counter+.
|
61
|
+
#
|
62
|
+
# The <tt>:as</tt> option may be used when rendering partials.
|
63
|
+
#
|
64
|
+
# You can specify a partial to be rendered between elements via the <tt>:spacer_template</tt> option.
|
65
|
+
# The following example will render <tt>advertiser/_ad_divider.html.erb</tt> between each ad partial:
|
66
|
+
#
|
67
|
+
# <%= render partial: "ad", collection: @advertisements, spacer_template: "ad_divider" %>
|
68
|
+
#
|
69
|
+
# If the given <tt>:collection</tt> is nil or empty, <tt>render</tt> will return nil. This will allow you
|
70
|
+
# to specify a text which will displayed instead by using this form:
|
71
|
+
#
|
72
|
+
# <%= render(partial: "ad", collection: @advertisements) || "There's no ad to be displayed" %>
|
73
|
+
#
|
74
|
+
# NOTE: Due to backwards compatibility concerns, the collection can't be one of hashes. Normally you'd also
|
75
|
+
# just keep domain objects, like Active Records, in there.
|
76
|
+
#
|
77
|
+
# == Rendering shared partials
|
78
|
+
#
|
79
|
+
# Two controllers can share a set of partials and render them like this:
|
80
|
+
#
|
81
|
+
# <%= render partial: "advertisement/ad", locals: { ad: @advertisement } %>
|
82
|
+
#
|
83
|
+
# This will render the partial "advertisement/_ad.html.erb" regardless of which controller this is being called from.
|
84
|
+
#
|
85
|
+
# == Rendering objects that respond to `to_partial_path`
|
86
|
+
#
|
87
|
+
# Instead of explicitly naming the location of a partial, you can also let PartialRenderer do the work
|
88
|
+
# and pick the proper path by checking `to_partial_path` method.
|
89
|
+
#
|
90
|
+
# # @account.to_partial_path returns 'accounts/account', so it can be used to replace:
|
91
|
+
# # <%= render partial: "accounts/account", locals: { account: @account} %>
|
92
|
+
# <%= render partial: @account %>
|
93
|
+
#
|
94
|
+
# # @posts is an array of Post instances, so every post record returns 'posts/post' on `to_partial_path`,
|
95
|
+
# # that's why we can replace:
|
96
|
+
# # <%= render partial: "posts/post", collection: @posts %>
|
97
|
+
# <%= render partial: @posts %>
|
98
|
+
#
|
99
|
+
# == Rendering the default case
|
100
|
+
#
|
101
|
+
# If you're not going to be using any of the options like collections or layouts, you can also use the short-hand
|
102
|
+
# defaults of render to render partials. Examples:
|
103
|
+
#
|
104
|
+
# # Instead of <%= render partial: "account" %>
|
105
|
+
# <%= render "account" %>
|
106
|
+
#
|
107
|
+
# # Instead of <%= render partial: "account", locals: { account: @buyer } %>
|
108
|
+
# <%= render "account", account: @buyer %>
|
109
|
+
#
|
110
|
+
# # @account.to_partial_path returns 'accounts/account', so it can be used to replace:
|
111
|
+
# # <%= render partial: "accounts/account", locals: { account: @account} %>
|
112
|
+
# <%= render @account %>
|
113
|
+
#
|
114
|
+
# # @posts is an array of Post instances, so every post record returns 'posts/post' on `to_partial_path`,
|
115
|
+
# # that's why we can replace:
|
116
|
+
# # <%= render partial: "posts/post", collection: @posts %>
|
117
|
+
# <%= render @posts %>
|
118
|
+
#
|
119
|
+
# == Rendering partials with layouts
|
120
|
+
#
|
121
|
+
# Partials can have their own layouts applied to them. These layouts are different than the ones that are
|
122
|
+
# specified globally for the entire action, but they work in a similar fashion. Imagine a list with two types
|
123
|
+
# of users:
|
124
|
+
#
|
125
|
+
# <%# app/views/users/index.html.erb &>
|
126
|
+
# Here's the administrator:
|
127
|
+
# <%= render partial: "user", layout: "administrator", locals: { user: administrator } %>
|
128
|
+
#
|
129
|
+
# Here's the editor:
|
130
|
+
# <%= render partial: "user", layout: "editor", locals: { user: editor } %>
|
131
|
+
#
|
132
|
+
# <%# app/views/users/_user.html.erb &>
|
133
|
+
# Name: <%= user.name %>
|
134
|
+
#
|
135
|
+
# <%# app/views/users/_administrator.html.erb &>
|
136
|
+
# <div id="administrator">
|
137
|
+
# Budget: $<%= user.budget %>
|
138
|
+
# <%= yield %>
|
139
|
+
# </div>
|
140
|
+
#
|
141
|
+
# <%# app/views/users/_editor.html.erb &>
|
142
|
+
# <div id="editor">
|
143
|
+
# Deadline: <%= user.deadline %>
|
144
|
+
# <%= yield %>
|
145
|
+
# </div>
|
146
|
+
#
|
147
|
+
# ...this will return:
|
148
|
+
#
|
149
|
+
# Here's the administrator:
|
150
|
+
# <div id="administrator">
|
151
|
+
# Budget: $<%= user.budget %>
|
152
|
+
# Name: <%= user.name %>
|
153
|
+
# </div>
|
154
|
+
#
|
155
|
+
# Here's the editor:
|
156
|
+
# <div id="editor">
|
157
|
+
# Deadline: <%= user.deadline %>
|
158
|
+
# Name: <%= user.name %>
|
159
|
+
# </div>
|
160
|
+
#
|
161
|
+
# If a collection is given, the layout will be rendered once for each item in
|
162
|
+
# the collection. For example, these two snippets have the same output:
|
163
|
+
#
|
164
|
+
# <%# app/views/users/_user.html.erb %>
|
165
|
+
# Name: <%= user.name %>
|
166
|
+
#
|
167
|
+
# <%# app/views/users/index.html.erb %>
|
168
|
+
# <%# This does not use layouts %>
|
169
|
+
# <ul>
|
170
|
+
# <% users.each do |user| -%>
|
171
|
+
# <li>
|
172
|
+
# <%= render partial: "user", locals: { user: user } %>
|
173
|
+
# </li>
|
174
|
+
# <% end -%>
|
175
|
+
# </ul>
|
176
|
+
#
|
177
|
+
# <%# app/views/users/_li_layout.html.erb %>
|
178
|
+
# <li>
|
179
|
+
# <%= yield %>
|
180
|
+
# </li>
|
181
|
+
#
|
182
|
+
# <%# app/views/users/index.html.erb %>
|
183
|
+
# <ul>
|
184
|
+
# <%= render partial: "user", layout: "li_layout", collection: users %>
|
185
|
+
# </ul>
|
186
|
+
#
|
187
|
+
# Given two users whose names are Alice and Bob, these snippets return:
|
188
|
+
#
|
189
|
+
# <ul>
|
190
|
+
# <li>
|
191
|
+
# Name: Alice
|
192
|
+
# </li>
|
193
|
+
# <li>
|
194
|
+
# Name: Bob
|
195
|
+
# </li>
|
196
|
+
# </ul>
|
197
|
+
#
|
198
|
+
# The current object being rendered, as well as the object_counter, will be
|
199
|
+
# available as local variables inside the layout template under the same names
|
200
|
+
# as available in the partial.
|
201
|
+
#
|
202
|
+
# You can also apply a layout to a block within any template:
|
203
|
+
#
|
204
|
+
# <%# app/views/users/_chief.html.erb &>
|
205
|
+
# <%= render(layout: "administrator", locals: { user: chief }) do %>
|
206
|
+
# Title: <%= chief.title %>
|
207
|
+
# <% end %>
|
208
|
+
#
|
209
|
+
# ...this will return:
|
210
|
+
#
|
211
|
+
# <div id="administrator">
|
212
|
+
# Budget: $<%= user.budget %>
|
213
|
+
# Title: <%= chief.name %>
|
214
|
+
# </div>
|
215
|
+
#
|
216
|
+
# As you can see, the <tt>:locals</tt> hash is shared between both the partial and its layout.
|
217
|
+
#
|
218
|
+
# If you pass arguments to "yield" then this will be passed to the block. One way to use this is to pass
|
219
|
+
# an array to layout and treat it as an enumerable.
|
220
|
+
#
|
221
|
+
# <%# app/views/users/_user.html.erb &>
|
222
|
+
# <div class="user">
|
223
|
+
# Budget: $<%= user.budget %>
|
224
|
+
# <%= yield user %>
|
225
|
+
# </div>
|
226
|
+
#
|
227
|
+
# <%# app/views/users/index.html.erb &>
|
228
|
+
# <%= render layout: @users do |user| %>
|
229
|
+
# Title: <%= user.title %>
|
230
|
+
# <% end %>
|
231
|
+
#
|
232
|
+
# This will render the layout for each user and yield to the block, passing the user, each time.
|
233
|
+
#
|
234
|
+
# You can also yield multiple times in one layout and use block arguments to differentiate the sections.
|
235
|
+
#
|
236
|
+
# <%# app/views/users/_user.html.erb &>
|
237
|
+
# <div class="user">
|
238
|
+
# <%= yield user, :header %>
|
239
|
+
# Budget: $<%= user.budget %>
|
240
|
+
# <%= yield user, :footer %>
|
241
|
+
# </div>
|
242
|
+
#
|
243
|
+
# <%# app/views/users/index.html.erb &>
|
244
|
+
# <%= render layout: @users do |user, section| %>
|
245
|
+
# <%- case section when :header -%>
|
246
|
+
# Title: <%= user.title %>
|
247
|
+
# <%- when :footer -%>
|
248
|
+
# Deadline: <%= user.deadline %>
|
249
|
+
# <%- end -%>
|
250
|
+
# <% end %>
|
251
|
+
class PartialRenderer < AbstractRenderer
|
252
|
+
PREFIXED_PARTIAL_NAMES = ThreadSafe::Cache.new do |h, k|
|
253
|
+
h[k] = ThreadSafe::Cache.new
|
254
|
+
end
|
255
|
+
|
256
|
+
def initialize(*)
|
257
|
+
super
|
258
|
+
@context_prefix = @lookup_context.prefixes.first
|
259
|
+
end
|
260
|
+
|
261
|
+
def render(context, options, block)
|
262
|
+
setup(context, options, block)
|
263
|
+
identifier = (@template = find_partial) ? @template.identifier : @path
|
264
|
+
|
265
|
+
@lookup_context.rendered_format ||= begin
|
266
|
+
if @template && @template.formats.present?
|
267
|
+
@template.formats.first
|
268
|
+
else
|
269
|
+
formats.first
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
if @collection
|
274
|
+
instrument(:collection, :identifier => identifier || "collection", :count => @collection.size) do
|
275
|
+
render_collection
|
276
|
+
end
|
277
|
+
else
|
278
|
+
instrument(:partial, :identifier => identifier) do
|
279
|
+
render_partial
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
def render_collection
|
285
|
+
return nil if @collection.blank?
|
286
|
+
|
287
|
+
if @options.key?(:spacer_template)
|
288
|
+
spacer = find_template(@options[:spacer_template], @locals.keys).render(@view, @locals)
|
289
|
+
end
|
290
|
+
|
291
|
+
result = @template ? collection_with_template : collection_without_template
|
292
|
+
result.join(spacer).html_safe
|
293
|
+
end
|
294
|
+
|
295
|
+
def render_partial
|
296
|
+
view, locals, block = @view, @locals, @block
|
297
|
+
object, as = @object, @variable
|
298
|
+
|
299
|
+
if !block && (layout = @options[:layout])
|
300
|
+
layout = find_template(layout.to_s, @template_keys)
|
301
|
+
end
|
302
|
+
|
303
|
+
object ||= locals[as]
|
304
|
+
locals[as] = object
|
305
|
+
|
306
|
+
content = @template.render(view, locals) do |*name|
|
307
|
+
view._layout_for(*name, &block)
|
308
|
+
end
|
309
|
+
|
310
|
+
content = layout.render(view, locals){ content } if layout
|
311
|
+
content
|
312
|
+
end
|
313
|
+
|
314
|
+
private
|
315
|
+
|
316
|
+
# Sets up instance variables needed for rendering a partial. This method
|
317
|
+
# finds the options and details and extracts them. The method also contains
|
318
|
+
# logic that handles the type of object passed in as the partial.
|
319
|
+
#
|
320
|
+
# If +options[:partial]+ is a string, then the +@path+ instance variable is
|
321
|
+
# set to that string. Otherwise, the +options[:partial]+ object must
|
322
|
+
# respond to +to_partial_path+ in order to setup the path.
|
323
|
+
def setup(context, options, block)
|
324
|
+
@view = context
|
325
|
+
partial = options[:partial]
|
326
|
+
|
327
|
+
@options = options
|
328
|
+
@locals = options[:locals] || {}
|
329
|
+
@block = block
|
330
|
+
@details = extract_details(options)
|
331
|
+
|
332
|
+
prepend_formats(options[:formats])
|
333
|
+
|
334
|
+
if String === partial
|
335
|
+
@object = options[:object]
|
336
|
+
@path = partial
|
337
|
+
@collection = collection
|
338
|
+
else
|
339
|
+
@object = partial
|
340
|
+
|
341
|
+
if @collection = collection_from_object || collection
|
342
|
+
paths = @collection_data = @collection.map { |o| partial_path(o) }
|
343
|
+
@path = paths.uniq.size == 1 ? paths.first : nil
|
344
|
+
else
|
345
|
+
@path = partial_path
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
if as = options[:as]
|
350
|
+
raise_invalid_identifier(as) unless as.to_s =~ /\A[a-z_]\w*\z/
|
351
|
+
as = as.to_sym
|
352
|
+
end
|
353
|
+
|
354
|
+
if @path
|
355
|
+
@variable, @variable_counter = retrieve_variable(@path, as)
|
356
|
+
@template_keys = retrieve_template_keys
|
357
|
+
else
|
358
|
+
paths.map! { |path| retrieve_variable(path, as).unshift(path) }
|
359
|
+
end
|
360
|
+
|
361
|
+
self
|
362
|
+
end
|
363
|
+
|
364
|
+
def collection
|
365
|
+
if @options.key?(:collection)
|
366
|
+
collection = @options[:collection]
|
367
|
+
collection.respond_to?(:to_ary) ? collection.to_ary : []
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
371
|
+
def collection_from_object
|
372
|
+
@object.to_ary if @object.respond_to?(:to_ary)
|
373
|
+
end
|
374
|
+
|
375
|
+
def find_partial
|
376
|
+
if path = @path
|
377
|
+
find_template(path, @template_keys)
|
378
|
+
end
|
379
|
+
end
|
380
|
+
|
381
|
+
def find_template(path, locals)
|
382
|
+
prefixes = path.include?(?/) ? [] : @lookup_context.prefixes
|
383
|
+
@lookup_context.find_template(path, prefixes, true, locals, @details)
|
384
|
+
end
|
385
|
+
|
386
|
+
def collection_with_template
|
387
|
+
view, locals, template = @view, @locals, @template
|
388
|
+
as, counter = @variable, @variable_counter
|
389
|
+
|
390
|
+
if layout = @options[:layout]
|
391
|
+
layout = find_template(layout, @template_keys)
|
392
|
+
end
|
393
|
+
|
394
|
+
index = -1
|
395
|
+
@collection.map do |object|
|
396
|
+
locals[as] = object
|
397
|
+
locals[counter] = (index += 1)
|
398
|
+
|
399
|
+
content = template.render(view, locals)
|
400
|
+
content = layout.render(view, locals) { content } if layout
|
401
|
+
content
|
402
|
+
end
|
403
|
+
end
|
404
|
+
|
405
|
+
def collection_without_template
|
406
|
+
view, locals, collection_data = @view, @locals, @collection_data
|
407
|
+
cache = {}
|
408
|
+
keys = @locals.keys
|
409
|
+
|
410
|
+
index = -1
|
411
|
+
@collection.map do |object|
|
412
|
+
index += 1
|
413
|
+
path, as, counter = collection_data[index]
|
414
|
+
|
415
|
+
locals[as] = object
|
416
|
+
locals[counter] = index
|
417
|
+
|
418
|
+
template = (cache[path] ||= find_template(path, keys + [as, counter]))
|
419
|
+
template.render(view, locals)
|
420
|
+
end
|
421
|
+
end
|
422
|
+
|
423
|
+
# Obtains the path to where the object's partial is located. If the object
|
424
|
+
# responds to +to_partial_path+, then +to_partial_path+ will be called and
|
425
|
+
# will provide the path. If the object does not respond to +to_partial_path+,
|
426
|
+
# then an +ArgumentError+ is raised.
|
427
|
+
#
|
428
|
+
# If +prefix_partial_path_with_controller_namespace+ is true, then this
|
429
|
+
# method will prefix the partial paths with a namespace.
|
430
|
+
def partial_path(object = @object)
|
431
|
+
object = object.to_model if object.respond_to?(:to_model)
|
432
|
+
|
433
|
+
path = if object.respond_to?(:to_partial_path)
|
434
|
+
object.to_partial_path
|
435
|
+
else
|
436
|
+
raise ArgumentError.new("'#{object.inspect}' is not an ActiveModel-compatible object. It must implement :to_partial_path.")
|
437
|
+
end
|
438
|
+
|
439
|
+
if @view.prefix_partial_path_with_controller_namespace
|
440
|
+
prefixed_partial_names[path] ||= merge_prefix_into_object_path(@context_prefix, path.dup)
|
441
|
+
else
|
442
|
+
path
|
443
|
+
end
|
444
|
+
end
|
445
|
+
|
446
|
+
def prefixed_partial_names
|
447
|
+
@prefixed_partial_names ||= PREFIXED_PARTIAL_NAMES[@context_prefix]
|
448
|
+
end
|
449
|
+
|
450
|
+
def merge_prefix_into_object_path(prefix, object_path)
|
451
|
+
if prefix.include?(?/) && object_path.include?(?/)
|
452
|
+
prefixes = []
|
453
|
+
prefix_array = File.dirname(prefix).split('/')
|
454
|
+
object_path_array = object_path.split('/')[0..-3] # skip model dir & partial
|
455
|
+
|
456
|
+
prefix_array.each_with_index do |dir, index|
|
457
|
+
break if dir == object_path_array[index]
|
458
|
+
prefixes << dir
|
459
|
+
end
|
460
|
+
|
461
|
+
(prefixes << object_path).join("/")
|
462
|
+
else
|
463
|
+
object_path
|
464
|
+
end
|
465
|
+
end
|
466
|
+
|
467
|
+
def retrieve_template_keys
|
468
|
+
keys = @locals.keys
|
469
|
+
keys << @variable if @object || @collection
|
470
|
+
keys << @variable_counter if @collection
|
471
|
+
keys
|
472
|
+
end
|
473
|
+
|
474
|
+
def retrieve_variable(path, as)
|
475
|
+
variable = as || begin
|
476
|
+
base = path[-1] == "/" ? "" : File.basename(path)
|
477
|
+
raise_invalid_identifier(path) unless base =~ /\A_?([a-z]\w*)(\.\w+)*\z/
|
478
|
+
$1.to_sym
|
479
|
+
end
|
480
|
+
variable_counter = :"#{variable}_counter" if @collection
|
481
|
+
[variable, variable_counter]
|
482
|
+
end
|
483
|
+
|
484
|
+
IDENTIFIER_ERROR_MESSAGE = "The partial name (%s) is not a valid Ruby identifier; " +
|
485
|
+
"make sure your partial name starts with a lowercase letter or underscore, " +
|
486
|
+
"and is followed by any combination of letters, numbers and underscores."
|
487
|
+
|
488
|
+
def raise_invalid_identifier(path)
|
489
|
+
raise ArgumentError.new(IDENTIFIER_ERROR_MESSAGE % (path))
|
490
|
+
end
|
491
|
+
end
|
492
|
+
end
|