Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add in views for HABTM associations

  • Loading branch information...
commit 265d893d9ccafaa20ad93e591bfbc09f2a8b2586 1 parent 7bda6fb
@jackdempsey jackdempsey authored committed
View
65 app/views/rails_admin/_has_and_belongs_to_many_association.html.erb
@@ -0,0 +1,65 @@
+<%
+ association_name = has_and_belongs_to_many_association[:name]
+ child_key = has_and_belongs_to_many_association[:child_key].first
+ collection = RailsAdmin::AbstractModel.new(has_and_belongs_to_many_association[:child_model]).all.map{|object| [object_label(object), object.id]}.sort_by{|object| object.first}
+
+ # for edit action
+ selected = @object.send(association_name).map{|object|
+ [object_label(object), object.id]
+ }.sort_by{|object| object.first}
+
+ # if error accurs - show the selected associations
+ if params["associations"]
+ select = params["associations"][association_name.to_param.pluralize].to_a.map{|o| o.to_i}
+ selected = collection.map{|t|
+ t if select.include?(t[1])
+ }.compact
+
+ end
+
+ # exclude selected from the original collection
+ collection = collection.map{|t|
+ t if not selected.include?(t)
+ }.compact
+
+ label = has_and_belongs_to_many_association[:pretty_name]
+ errors_exist = !(@object.errors[child_key].nil? || @object.errors[child_key].empty?)
+%>
+ <h2><%= label %></h2>
+ <div class="formSection">
+ <%= label_tag label %>
+ <div class="many_header">
+ <input type="text" class="searchMany" value="Search <%= association_name %>" ref="<%= association_name %>" class="many_text" used="0" />
+ <p>
+ <strong><%= t("admin.new.chosen", :name => association_name) %></strong>
+ <%= t("admin.new.select_choise")%> <img src="/images/selector-add.gif" alt="Add" />
+ </p>
+ </div>
+ <div class="manySelector">
+ <select multiple="multiple" size="8" class="firstSelect">
+ <%= options_for_select(collection) %>
+ </select>
+ <a href="javascript:void(0)" class="addAssoc"><img src="/images/selector-add.gif" alt="Add element" class="add_elem"/></a>
+ <a href="javascript:void(0)" class="removeAssoc"><img src="/images/selector-remove.gif" alt="Remove element" class="remove_elem"/></a>
+ <select id="associations_<%= association_name %>" multiple="multiple" class="secondSelect" size="8">
+ <%= options_for_select(selected) %>
+ </select>
+ </div>
+ <div class="many_controls">
+ <a href="javascript:void(0)" class="addAssoc"><img src="/images/selector-addall.gif" alt="Add all" class="add_all" /> <%= t("admin.new.chose_all")%></a>
+ <a href="javascript:void(0)" class="clearAssoc"><img src="/images/selector-removeall.gif" alt="Clear all" class="clear_all"/><%= t("admin.new.clear_all")%></a>
+ </div>
+ <div class="manyHidden">
+ <% if selected.empty? %>
+ <input type="hidden" name="associations[<%= association_name %>][]" />
+ <% else %>
+ <% selected.each do |t|%>
+ <input type="hidden" name="associations[<%= association_name %>][]" value="<%= t[1] %>" />
+ <% end %>
+ <% end %>
+ </div>
+ <span class="breakSpan"></span>
+ </div>
+ <% if errors_exist %>
+ <span class="errorMessage"><%= "#{association_name.to_param.capitalize} #{@object.errors[association_name].first}" %></span>
+ <% end %>
View
1  app/views/rails_admin/edit.html.erb
@@ -23,6 +23,7 @@
<%= render(:partial => 'belongs_to_association', :collection => @abstract_model.belongs_to_associations) -%>
<%= render(:partial => 'has_one_association', :collection => @abstract_model.has_one_associations) -%>
<%= render(:partial => 'has_many_association', :collection => @abstract_model.has_many_associations) -%>
+<%= render(:partial => 'has_and_belongs_to_many_association', :collection => @abstract_model.has_and_belongs_to_many_associations) -%>
<ul id="submitArea">
<li id="saveContainer">
View
1  app/views/rails_admin/new.html.erb
@@ -13,6 +13,7 @@
<%= render(:partial => 'belongs_to_association', :collection => @abstract_model.belongs_to_associations) -%>
<%= render(:partial => 'has_one_association', :collection => @abstract_model.has_one_associations) -%>
<%= render(:partial => 'has_many_association', :collection => @abstract_model.has_many_associations) -%>
+<%= render(:partial => 'has_and_belongs_to_many_association', :collection => @abstract_model.has_and_belongs_to_many_associations) -%>
<ul id="submitArea">
<li id="saveContainer">
Please sign in to comment.
Something went wrong with that request. Please try again.