1
0

Quantity parent options are updated through UJS

This commit is contained in:
cryptogopher 2019-11-27 20:43:39 +01:00
parent 6e335c243a
commit f6081276f3
6 changed files with 24 additions and 29 deletions

View File

@ -1,6 +1,6 @@
class QuantitiesController < ApplicationController class QuantitiesController < ApplicationController
before_action :init_session_filters 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 :find_quantity, only: [:edit, :update, :destroy, :toggle, :move]
before_action :authorize before_action :authorize
@ -10,6 +10,11 @@ class QuantitiesController < ApplicationController
prepare_quantities prepare_quantities
end end
def parents
@form = params[:form]
@domain = params[:quantity][:domain]
end
def create def create
@quantity = @project.quantities.new(quantity_params) @quantity = @project.quantities.new(quantity_params)
if @quantity.save if @quantity.save

View File

@ -1,35 +1,16 @@
<%= 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}, data: {
<% Quantity.domains.each_key do |d| %> remote: true,
<div id="parent_select_<%= d %>" <%= 'style=display:none;' if @quantity.domain != d %>> url: parents_project_quantities_path(@project),
<p><%= f.select :parent_id, parent_options(d), {required: true, params: "form=#{f.options[:html][:id]}"
label: :field_parent_quantity, } %></p>
include_blank: t('.null_parent')} %></p> <p><%= f.select :parent_id, parent_options(@quantity.domain),
</div> {required: true, label: :field_parent_quantity, include_blank: t('.null_parent')} %></p>
<% end %>
<p><%= f.text_field :name, size: 25, required: true %></p> <p><%= f.text_field :name, size: 25, required: true %></p>
<p><%= f.text_field :description, style: "width: 100%;" %></p> <p><%= f.text_field :description, style: "width: 100%;" %></p>
<p><%= f.text_field :formula, placeholder: t('.formula_placeholder'), <p><%= f.text_field :formula, placeholder: t('.formula_placeholder'),
style: "width: 100%;" %></p> style: "width: 100%;" %></p>
<p><%= f.check_box :primary %></p> <p><%= f.check_box :primary %></p>
</div> </div>
<%= 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 %>

View File

@ -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])
)) %>'
);

View File

@ -1,3 +1,4 @@
$('div[id^=flash_]').remove(); $('div[id^=flash_]').remove();
$('#content').prepend('<%= escape_javascript(render_flash_messages) %>'); $('#content').prepend('<%= escape_javascript(render_flash_messages) %>');
$('#quantities').html('<%= escape_javascript(render :partial => 'quantities/index') %>'); $('#quantities').html('<%= escape_javascript(render :partial => 'quantities/index') %>');
$('#quantity_domain').trigger('change');

View File

@ -25,7 +25,10 @@ resources :projects do
post 'toggle' post 'toggle'
post 'move/:direction', to: 'quantities#move', as: :move post 'move/:direction', to: 'quantities#move', as: :move
end end
get 'filter', on: :collection collection do
get 'parents'
get 'filter'
end
end end
resources :units, :only => [:index, :create, :destroy] resources :units, :only => [:index, :create, :destroy]
end end

View File

@ -19,7 +19,7 @@ Redmine::Plugin.register :body_tracking do
:measurements => [:index], :measurements => [:index],
:ingredients => [:index, :nutrients, :filter, :filter_nutrients], :ingredients => [:index, :nutrients, :filter, :filter_nutrients],
:sources => [:index], :sources => [:index],
:quantities => [:index, :filter], :quantities => [:index, :parents, :filter],
:units => [:index], :units => [:index],
}, read: true }, read: true
permission :manage_common, { permission :manage_common, {