Commit 4b28211ea04ff4d0bc8823e163bf0c0eb34b091c
1 parent
cbd6f636
Exists in
master
and in
20 other branches
rails4: fix a case where partial_for_class is not working for plugins
Showing
3 changed files
with
46 additions
and
31 deletions
Show diff stats
app/helpers/application_helper.rb
@@ -10,6 +10,8 @@ module ApplicationHelper | @@ -10,6 +10,8 @@ module ApplicationHelper | ||
10 | 10 | ||
11 | include UrlHelper | 11 | include UrlHelper |
12 | 12 | ||
13 | + include PartialsHelper | ||
14 | + | ||
13 | include ModalHelper | 15 | include ModalHelper |
14 | 16 | ||
15 | include BoxesHelper | 17 | include BoxesHelper |
@@ -283,36 +285,6 @@ module ApplicationHelper | @@ -283,36 +285,6 @@ module ApplicationHelper | ||
283 | concat(content_tag('div', capture(&block).to_s + tag('br', :style => 'clear: left;'), options)) | 285 | concat(content_tag('div', capture(&block).to_s + tag('br', :style => 'clear: left;'), options)) |
284 | end | 286 | end |
285 | 287 | ||
286 | - | ||
287 | - def partial_for_class_in_view_path(klass, view_path, prefix = nil, suffix = nil) | ||
288 | - return nil if klass.nil? | ||
289 | - name = [prefix, klass.name.underscore, suffix].compact.map(&:to_s).join('_') | ||
290 | - | ||
291 | - search_name = String.new(name) | ||
292 | - if search_name.include?("/") | ||
293 | - search_name.gsub!(/(\/)([^\/]*)$/,'\1_\2') | ||
294 | - name = File.join(params[:controller], name) if defined?(params) && params[:controller] | ||
295 | - else | ||
296 | - search_name = "_" + search_name | ||
297 | - end | ||
298 | - | ||
299 | - path = defined?(params) && params[:controller] ? File.join(view_path, params[:controller], search_name + '.html.erb') : File.join(view_path, search_name + '.html.erb') | ||
300 | - return name if File.exists?(File.join(path)) | ||
301 | - | ||
302 | - partial_for_class_in_view_path(klass.superclass, view_path, prefix, suffix) | ||
303 | - end | ||
304 | - | ||
305 | - def partial_for_class(klass, prefix=nil, suffix=nil) | ||
306 | - raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' if klass.nil? | ||
307 | - name = klass.name.underscore | ||
308 | - controller.view_paths.each do |view_path| | ||
309 | - partial = partial_for_class_in_view_path(klass, view_path, prefix, suffix) | ||
310 | - return partial if partial | ||
311 | - end | ||
312 | - | ||
313 | - raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' | ||
314 | - end | ||
315 | - | ||
316 | def render_profile_actions klass | 288 | def render_profile_actions klass |
317 | name = klass.to_s.underscore | 289 | name = klass.to_s.underscore |
318 | begin | 290 | begin |
@@ -0,0 +1,43 @@ | @@ -0,0 +1,43 @@ | ||
1 | +module PartialsHelper | ||
2 | + | ||
3 | + def partial_for_class_in_view_path(klass, view_path, prefix = nil, suffix = nil) | ||
4 | + return nil if klass.nil? | ||
5 | + name = [prefix, klass.name.underscore, suffix].compact.map(&:to_s).join('_') | ||
6 | + | ||
7 | + search_name = String.new(name) | ||
8 | + if search_name.include?("/") | ||
9 | + search_name.gsub!(/(\/)([^\/]*)$/,'\1_\2') | ||
10 | + name = File.join(params[:controller], name) if defined?(params) && params[:controller] | ||
11 | + else | ||
12 | + search_name = "_" + search_name | ||
13 | + end | ||
14 | + | ||
15 | + path = defined?(params) && params[:controller] ? File.join(view_path, params[:controller], search_name + '.html.erb') : File.join(view_path, search_name + '.html.erb') | ||
16 | + return name if File.exists?(File.join(path)) | ||
17 | + | ||
18 | + partial_for_class_in_view_path(klass.superclass, view_path, prefix, suffix) | ||
19 | + end | ||
20 | + | ||
21 | + def partial_for_class(klass, prefix=nil, suffix=nil) | ||
22 | + raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' if klass.nil? | ||
23 | + name = klass.name.underscore | ||
24 | + controller.view_paths.each do |view_path| | ||
25 | + partial = partial_for_class_in_view_path(klass, view_path, prefix, suffix) | ||
26 | + return partial if partial | ||
27 | + end | ||
28 | + | ||
29 | + raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' | ||
30 | + end | ||
31 | + | ||
32 | + def render_partial_for_class klass, *args | ||
33 | + raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' if klass.nil? | ||
34 | + begin | ||
35 | + partial = klass.name.underscore | ||
36 | + partial = "#{params[:controller]}/#{partial}" if params[:controller] and partial.index '/' | ||
37 | + return render partial, *args | ||
38 | + rescue ActionView::MissingTemplate | ||
39 | + return render_partial_for_class klass.superclass, *args | ||
40 | + end | ||
41 | + end | ||
42 | + | ||
43 | +end |
app/views/cms/edit.html.erb
@@ -9,7 +9,7 @@ | @@ -9,7 +9,7 @@ | ||
9 | 9 | ||
10 | <%= hidden_field_tag('success_back_to', @success_back_to) %> | 10 | <%= hidden_field_tag('success_back_to', @success_back_to) %> |
11 | 11 | ||
12 | - <%= render :partial => partial_for_class(@article.class), :locals => { :f => f } %> | 12 | + <%= render_partial_for_class @article.class, f: f %> |
13 | 13 | ||
14 | <% if environment.is_portal_community?(profile) %> | 14 | <% if environment.is_portal_community?(profile) %> |
15 | <div> | 15 | <div> |