Fixed Quantity parent list display for new/edit forms
This commit is contained in:
parent
6433621fa7
commit
6e335c243a
@ -6,7 +6,7 @@ class QuantitiesController < ApplicationController
|
|||||||
|
|
||||||
def index
|
def index
|
||||||
@quantity = @project.quantities.new
|
@quantity = @project.quantities.new
|
||||||
@quantity.domain = Quantity.domains[session[:q_filters][:domain]]
|
@quantity.domain = Quantity.domains[session[:q_filters][:domain]] || @quantity.domain
|
||||||
prepare_quantities
|
prepare_quantities
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -11,9 +11,8 @@ module QuantitiesHelper
|
|||||||
end
|
end
|
||||||
|
|
||||||
def parent_options(domain)
|
def parent_options(domain)
|
||||||
options = nested_set_options(@quantities.send(domain), @quantity) do |i|
|
options = nested_set_options(@project.quantities.send(domain), @quantity) do |i|
|
||||||
raw("#{' ' * i.level}#{i.name}")
|
raw("#{' ' * i.level}#{i.name}")
|
||||||
end
|
end
|
||||||
options.unshift([t('.null_parent'), nil])
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -19,6 +19,7 @@ class Quantity < ActiveRecord::Base
|
|||||||
|
|
||||||
after_initialize do
|
after_initialize do
|
||||||
if new_record?
|
if new_record?
|
||||||
|
self.domain ||= :diet
|
||||||
self.primary = false if self.primary.nil?
|
self.primary = false if self.primary.nil?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
<%= error_messages_for @quantity %>
|
<%= error_messages_for @quantity %>
|
||||||
|
|
||||||
<div class="box tabular">
|
<div class="box tabular">
|
||||||
<p><%= f.select :domain, domain_options, required: true %></p>
|
<p><%= f.select :domain, domain_options, {required: true} %></p>
|
||||||
<% Quantity.domains.each_key do |d| %>
|
<% Quantity.domains.each_key do |d| %>
|
||||||
<div id="parent_select_<%= d %>" style="display: none">
|
<div id="parent_select_<%= d %>" <%= 'style=display:none;' if @quantity.domain != d %>>
|
||||||
<p><%= f.select :parent_id, parent_options(d),
|
<p><%= f.select :parent_id, parent_options(d), {required: true,
|
||||||
{required: true, label: :field_parent_quantity} %></p>
|
label: :field_parent_quantity,
|
||||||
|
include_blank: t('.null_parent')} %></p>
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
<p><%= f.text_field :name, size: 25, required: true %></p>
|
<p><%= f.text_field :name, size: 25, required: true %></p>
|
||||||
@ -17,15 +18,17 @@
|
|||||||
|
|
||||||
<%= javascript_tag do %>
|
<%= javascript_tag do %>
|
||||||
function domainChange() {
|
function domainChange() {
|
||||||
$('[id^=parent_select_]').prop('disabled', true).hide();
|
box = $(this).closest('div');
|
||||||
$('[id^=parent_select_] select').attr('name', 'placeholder');
|
box.find('[id^=parent_select_]').prop('disabled', true).hide();
|
||||||
$('[id=parent_select_'+$('#quantity_domain option:selected').val()+']')
|
box.find('[id^=parent_select_] select').attr('name', 'placeholder');
|
||||||
.prop('disabled', false).show();
|
domain = $(this).val();
|
||||||
$('[id=parent_select_'+$('#quantity_domain option:selected').val()+'] select')
|
box.find('[id=parent_select_'+domain+']').prop('disabled', false).show();
|
||||||
|
box.find('[id=parent_select_'+domain+'] select')
|
||||||
.attr('name', 'quantity[parent_id]');
|
.attr('name', 'quantity[parent_id]');
|
||||||
}
|
}
|
||||||
$('#quantity-form').on('change', '#quantity_domain', domainChange);
|
$('form').on('change', '#quantity_domain', domainChange);
|
||||||
|
|
||||||
|
//Firefox does not emit 'change' signal when preserving form over reload
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
domainChange();
|
domainChange();
|
||||||
});
|
});
|
||||||
|
@ -72,7 +72,8 @@
|
|||||||
<%=
|
<%=
|
||||||
link_to l(:button_cancel), "#",
|
link_to l(:button_cancel), "#",
|
||||||
onclick: '$(this).closest("tr").remove();
|
onclick: '$(this).closest("tr").remove();
|
||||||
$("#quantities a.icon-edit").css("visibility", "visible");'
|
$("#quantities a.icon-edit").css("visibility", "visible");
|
||||||
|
return false;'
|
||||||
%>
|
%>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
@ -14,10 +14,10 @@
|
|||||||
|
|
||||||
<%= labelled_form_for @quantity,
|
<%= labelled_form_for @quantity,
|
||||||
:url => project_quantities_path(@project),
|
:url => project_quantities_path(@project),
|
||||||
:html => {:id => 'quantity-form'} do |f| %>
|
:html => {:id => 'quantity-add-form'} do |f| %>
|
||||||
<%= render :partial => 'quantities/form', :locals => { :f => f } %>
|
<%= render :partial => 'quantities/form', :locals => { :f => f } %>
|
||||||
<%= submit_tag l(:button_create) %>
|
<%= submit_tag l(:button_create) %>
|
||||||
<%= link_to l(:button_cancel), "#", :onclick => '$("#add-quantity").hide()' %>
|
<%= link_to l(:button_cancel), "#", onclick: '$("#add-quantity").hide(); return false;' %>
|
||||||
<% end %>
|
<% end %>
|
||||||
<hr>
|
<hr>
|
||||||
</div>
|
</div>
|
||||||
|
Reference in New Issue
Block a user