Commit bb150fb82c7be2e5b6952b38e7a1b40c432c5859
1 parent
2b56f641
Exists in
send_email_to_admins
and in
5 other branches
Fix render partial for class to work with searches
Showing
1 changed file
with
40 additions
and
5 deletions
Show diff stats
app/helpers/partials_helper.rb
| @@ -29,14 +29,49 @@ module PartialsHelper | @@ -29,14 +29,49 @@ module PartialsHelper | ||
| 29 | raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' | 29 | raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' |
| 30 | end | 30 | end |
| 31 | 31 | ||
| 32 | - def render_partial_for_class klass, *args | 32 | + |
| 33 | + def partial_for_class(klass, prefix=nil, suffix=nil) | ||
| 33 | raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' if klass.nil? | 34 | raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' if klass.nil? |
| 35 | + name = klass.name.underscore | ||
| 36 | + controller.view_paths.each do |view_path| | ||
| 37 | + partial = partial_for_class_in_view_path(klass, view_path, prefix, suffix) | ||
| 38 | + return partial if partial | ||
| 39 | + end | ||
| 40 | + | ||
| 41 | + raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' | ||
| 42 | + end | ||
| 43 | + | ||
| 44 | + ## | ||
| 45 | + # Calculate partial name with prefix and suffix | ||
| 46 | + # Togheter with render_partial_for_class, | ||
| 47 | + # it should replace #partial_for_class_in_view_path in the future | ||
| 48 | + # | ||
| 49 | + def partial_name_for underscore, prefix = nil, suffix = nil | ||
| 50 | + parts = underscore.split '/' | ||
| 51 | + if prefix or suffix | ||
| 52 | + partial = [prefix, parts.last, suffix].compact.map(&:to_s).join '_' | ||
| 53 | + else | ||
| 54 | + partial = parts.last | ||
| 55 | + end | ||
| 56 | + if parts.size > 1 | ||
| 57 | + "#{params[:controller]}/#{parts.first}/#{partial}" | ||
| 58 | + else | ||
| 59 | + partial | ||
| 60 | + end | ||
| 61 | + end | ||
| 62 | + | ||
| 63 | + def render_for_class klass, *args, &block | ||
| 64 | + raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' unless klass | ||
| 34 | begin | 65 | begin |
| 35 | - partial = klass.name.underscore | ||
| 36 | - partial = "#{params[:controller]}/#{partial}" if params[:controller] and partial.index '/' | ||
| 37 | - return render partial, *args | 66 | + capture klass, &block |
| 38 | rescue ActionView::MissingTemplate | 67 | rescue ActionView::MissingTemplate |
| 39 | - return render_partial_for_class klass.superclass, *args | 68 | + render_for_class klass.superclass, *args, &block |
| 69 | + end | ||
| 70 | + end | ||
| 71 | + | ||
| 72 | + def render_partial_for_class klass, *args | ||
| 73 | + render_for_class klass do |klass| | ||
| 74 | + render partial_name_for(klass.name.underscore), *args | ||
| 40 | end | 75 | end |
| 41 | end | 76 | end |
| 42 | 77 |