Quantity parent options are updated through UJS
This commit is contained in:
parent
6e335c243a
commit
f6081276f3
@ -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
|
||||
|
@ -1,35 +1,16 @@
|
||||
<%= error_messages_for @quantity %>
|
||||
|
||||
<div class="box tabular">
|
||||
<p><%= f.select :domain, domain_options, {required: true} %></p>
|
||||
<% Quantity.domains.each_key do |d| %>
|
||||
<div id="parent_select_<%= d %>" <%= 'style=display:none;' if @quantity.domain != d %>>
|
||||
<p><%= f.select :parent_id, parent_options(d), {required: true,
|
||||
label: :field_parent_quantity,
|
||||
include_blank: t('.null_parent')} %></p>
|
||||
</div>
|
||||
<% end %>
|
||||
<p><%= f.select :domain, domain_options, {required: true}, data: {
|
||||
remote: true,
|
||||
url: parents_project_quantities_path(@project),
|
||||
params: "form=#{f.options[:html][:id]}"
|
||||
} %></p>
|
||||
<p><%= f.select :parent_id, parent_options(@quantity.domain),
|
||||
{required: true, label: :field_parent_quantity, include_blank: t('.null_parent')} %></p>
|
||||
<p><%= f.text_field :name, size: 25, required: true %></p>
|
||||
<p><%= f.text_field :description, style: "width: 100%;" %></p>
|
||||
<p><%= f.text_field :formula, placeholder: t('.formula_placeholder'),
|
||||
style: "width: 100%;" %></p>
|
||||
<p><%= f.check_box :primary %></p>
|
||||
</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 %>
|
||||
|
5
app/views/quantities/parents.js.erb
Normal file
5
app/views/quantities/parents.js.erb
Normal 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])
|
||||
)) %>'
|
||||
);
|
@ -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');
|
||||
|
@ -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
|
||||
|
2
init.rb
2
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, {
|
||||
|
Reference in New Issue
Block a user