diff --git a/app/controllers/quantities_controller.rb b/app/controllers/quantities_controller.rb
index 39609d7..1647efd 100644
--- a/app/controllers/quantities_controller.rb
+++ b/app/controllers/quantities_controller.rb
@@ -1,6 +1,6 @@
class QuantitiesController < ApplicationController
before_action :init_session_filters
- before_action :find_project_by_project_id, only: [:index, :create, :filter]
+ before_action :find_project_by_project_id, only: [:index, :parents, :create, :filter]
before_action :find_quantity, only: [:edit, :update, :destroy, :toggle, :move]
before_action :authorize
@@ -10,6 +10,11 @@ class QuantitiesController < ApplicationController
prepare_quantities
end
+ def parents
+ @form = params[:form]
+ @domain = params[:quantity][:domain]
+ end
+
def create
@quantity = @project.quantities.new(quantity_params)
if @quantity.save
diff --git a/app/views/quantities/_form.html.erb b/app/views/quantities/_form.html.erb
index 3ce05ce..541925a 100644
--- a/app/views/quantities/_form.html.erb
+++ b/app/views/quantities/_form.html.erb
@@ -1,35 +1,16 @@
<%= error_messages_for @quantity %>
-
<%= f.select :domain, domain_options, {required: true} %>
- <% Quantity.domains.each_key do |d| %>
-
>
-
<%= f.select :parent_id, parent_options(d), {required: true,
- label: :field_parent_quantity,
- include_blank: t('.null_parent')} %>
-
- <% end %>
+
<%= f.select :domain, domain_options, {required: true}, data: {
+ remote: true,
+ url: parents_project_quantities_path(@project),
+ params: "form=#{f.options[:html][:id]}"
+ } %>
+
<%= f.select :parent_id, parent_options(@quantity.domain),
+ {required: true, label: :field_parent_quantity, include_blank: t('.null_parent')} %>
<%= f.text_field :name, size: 25, required: true %>
<%= f.text_field :description, style: "width: 100%;" %>
<%= f.text_field :formula, placeholder: t('.formula_placeholder'),
style: "width: 100%;" %>
<%= f.check_box :primary %>
-
-<%= javascript_tag do %>
- function domainChange() {
- box = $(this).closest('div');
- box.find('[id^=parent_select_]').prop('disabled', true).hide();
- box.find('[id^=parent_select_] select').attr('name', 'placeholder');
- domain = $(this).val();
- box.find('[id=parent_select_'+domain+']').prop('disabled', false).show();
- box.find('[id=parent_select_'+domain+'] select')
- .attr('name', 'quantity[parent_id]');
- }
- $('form').on('change', '#quantity_domain', domainChange);
-
- //Firefox does not emit 'change' signal when preserving form over reload
- $(document).ready(function() {
- domainChange();
- });
-<% end %>
diff --git a/app/views/quantities/parents.js.erb b/app/views/quantities/parents.js.erb
new file mode 100644
index 0000000..312710a
--- /dev/null
+++ b/app/views/quantities/parents.js.erb
@@ -0,0 +1,5 @@
+$('<%= "##{@form} " if @form %>#quantity_parent_id').empty().append(
+ '<%= escape_javascript(options_for_select(
+ parent_options(@domain).unshift([t('quantities.form.null_parent'), nil])
+ )) %>'
+);
diff --git a/app/views/quantities/toggle.js.erb b/app/views/quantities/toggle.js.erb
index 2f11725..94e3c08 100644
--- a/app/views/quantities/toggle.js.erb
+++ b/app/views/quantities/toggle.js.erb
@@ -1,3 +1,4 @@
$('div[id^=flash_]').remove();
$('#content').prepend('<%= escape_javascript(render_flash_messages) %>');
$('#quantities').html('<%= escape_javascript(render :partial => 'quantities/index') %>');
+$('#quantity_domain').trigger('change');
diff --git a/config/routes.rb b/config/routes.rb
index 4d7a9ec..bc792e0 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -25,7 +25,10 @@ resources :projects do
post 'toggle'
post 'move/:direction', to: 'quantities#move', as: :move
end
- get 'filter', on: :collection
+ collection do
+ get 'parents'
+ get 'filter'
+ end
end
resources :units, :only => [:index, :create, :destroy]
end
diff --git a/init.rb b/init.rb
index 0c03f9b..e8a1603 100644
--- a/init.rb
+++ b/init.rb
@@ -19,7 +19,7 @@ Redmine::Plugin.register :body_tracking do
:measurements => [:index],
:ingredients => [:index, :nutrients, :filter, :filter_nutrients],
:sources => [:index],
- :quantities => [:index, :filter],
+ :quantities => [:index, :parents, :filter],
:units => [:index],
}, read: true
permission :manage_common, {