Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Look for partial in all view paths.

Clean up view code.
  • Loading branch information...
commit d9a9740fd8216c72b5a4025809f36dc3ad64eb22 1 parent f32d338
@wulffeld authored
View
26 app/helpers/easy_admin_ui/easy_admin_ui_helper.rb
@@ -92,5 +92,31 @@ def delete_link_with_confirmation(obj, delete_path=nil)
link_to("Yes", delete_path, :method => :delete, :remote => true) + ' ' +
link_to_function("No", "$('#cnf_#{obj.id}').hide();") + raw('</span>')
end
+
+ def partial_exists?(filename)
+ ActionController::Base.view_paths.any? {|path| File.exists?(File.join(path, self.controller_path, filename)) }
+ end
+
+ def filename_from_partial(partial)
+ "_#{partial}.html.erb"
+ end
+
+ # Render partial if it exists.
+ def render_optional(partial)
+ return '' unless partial_exists?(filename_from_partial(partial))
+ render partial: partial
+ end
+
+ # Render the first partial it finds.
+ def render_optional_either(*partials)
+ ''.tap do |html|
+ partials.each do |partial|
+ if partial_exists?(filename_from_partial(partial))
+ html << render(partial: partial)
+ break
+ end
+ end
+ end.html_safe
+ end
end
end
View
22 app/views/easy_admin_ui/edit.html.erb
@@ -1,14 +1,7 @@
<h1>Editing <%= @item.class.to_s.tableize.singularize.humanize %></h1>
-<% if File.exists?(File.join(controller_full_path, '_modify_after_title.html.erb')) -%>
- <%= render :partial => 'modify_after_title' %>
-<% elsif File.exists?(File.join(controller_full_path, '_edit_after_title.html.erb')) -%>
- <%= render :partial => 'edit_after_title' %>
-<% end -%>
-
-<% if File.exists?(File.join(controller_full_path, '_after_title.html.erb')) -%>
- <%= render :partial => 'after_title' %>
-<% end -%>
+<%= render_optional_either('modify_after_title', 'edit_after_title') %>
+<%= render_optional('after_title') %>
<%= semantic_form_for(@item, :url => eval("admin_#{@item.class.to_s.underscore}_path(@item)"), :html => { :multipart => true }) do |f| %>
<%= f.inputs do %>
@@ -19,12 +12,5 @@
<% end -%>
<% end -%>
-<% if File.exists?(File.join(controller_full_path, '_modify_after_form.html.erb')) -%>
- <%= render :partial => 'modify_after_form' %>
-<% elsif File.exists?(File.join(controller_full_path, '_edit_after_form.html.erb')) -%>
- <%= render :partial => 'edit_after_form' %>
-<% end -%>
-
-<% if File.exists?(File.join(controller_full_path, '_after_form.html.erb')) -%>
- <%= render :partial => 'after_form' %>
-<% end -%>
+<%= render_optional_either('modify_after_form', 'edit_after_form') %>
+<%= render_optional('after_form') %>
View
12 app/views/easy_admin_ui/index.html.erb
@@ -1,10 +1,6 @@
<h1><%= @options[:page_title] %> <%= admin_new_link unless @options[:skip_new] %></h1>
-<% if File.exists?(File.join(controller_full_path, '_index_after_title.html.erb')) -%>
- <%= render :partial => 'index_after_title' %>
-<% elsif File.exists?(File.join(controller_full_path, '_after_title.html.erb')) -%>
- <%= render :partial => 'after_title' %>
-<% end -%>
+<%= render_optional_either('index_after_title', 'after_title') %>
<%= paginate @items if @items.respond_to?(:per) %>
@@ -12,8 +8,4 @@
<%= paginate @items if @items.respond_to?(:per) %>
-<% if File.exists?(File.join(controller_full_path, '_index_after_table.html.erb')) -%>
- <%= render :partial => 'index_after_table' %>
-<% elsif File.exists?(File.join(controller_full_path, '_after_table.html.erb')) -%>
- <%= render :partial => 'after_table' %>
-<% end -%>
+<%= render_optional_either('index_after_table', 'after_table') %>
View
22 app/views/easy_admin_ui/new.html.erb
@@ -1,14 +1,7 @@
<h1>New <%= @item.class.to_s.tableize.singularize.humanize %></h1>
-<% if File.exists?(File.join(controller_full_path, '_modify_after_title.html.erb')) -%>
- <%= render :partial => 'modify_after_title' %>
-<% elsif File.exists?(File.join(controller_full_path, '_new_after_title.html.erb')) -%>
- <%= render :partial => 'new_after_title' %>
-<% end -%>
-
-<% if File.exists?(File.join(controller_full_path, '_after_title.html.erb')) -%>
- <%= render :partial => 'after_title' %>
-<% end -%>
+<%= render_optional_either('modify_after_title', 'new_after_title') %>
+<%= render_optional('after_title') %>
<%= semantic_form_for(@item, :url => eval("admin_#{@item.class.to_s.underscore.pluralize}_path"), :html => { :multipart => true }) do |f| %>
<%= f.inputs do %>
@@ -19,12 +12,5 @@
<% end -%>
<% end -%>
-<% if File.exists?(File.join(controller_full_path, '_modify_after_form.html.erb')) -%>
- <%= render :partial => 'modify_after_form' %>
-<% elsif File.exists?(File.join(controller_full_path, '_new_after_form.html.erb')) -%>
- <%= render :partial => 'new_after_form' %>
-<% end -%>
-
-<% if File.exists?(File.join(controller_full_path, '_after_form.html.erb')) -%>
- <%= render :partial => 'after_form' %>
-<% end -%>
+<%= render_optional_either('modify_after_form', 'new_after_form') %>
+<%= render_optional('after_form') %>
Please sign in to comment.
Something went wrong with that request. Please try again.