1
0

Updated Quantity to use Formula

and upgraded controller actions to match Measurement/Ingredient
This commit is contained in:
cryptogopher 2020-02-19 17:57:29 +01:00
parent 544c0c5293
commit b7b7401833
12 changed files with 90 additions and 73 deletions

View File

@ -2,29 +2,48 @@ class QuantitiesController < ApplicationController
menu_item :body_trackers menu_item :body_trackers
before_action :init_session_filters before_action :init_session_filters
before_action :find_project_by_project_id, only: [:index, :parents, :create, :filter] before_action :find_project_by_project_id, only: [:index, :new, :create, :filter, :parents]
before_action :find_quantity, only: [:edit, :update, :destroy, :move] before_action :find_quantity, only: [:edit, :update, :destroy, :move]
before_action :authorize before_action :authorize
def index def index
@quantity = @project.quantities.new
@quantity.domain = Quantity.domains[session[:q_filters][:domain]] || @quantity.domain
prepare_quantities prepare_quantities
end end
def parents def new
@form = params[:form] @quantity = @project.quantities.new
@domain = params[:quantity][:domain] @quantity.domain = Quantity.domains[session[:q_filters][:domain]] || @quantity.domain
@quantity.build_formula
end end
def create def create
@quantity = @project.quantities.new(quantity_params) @quantity = @project.quantities.new(quantity_params)
if @quantity.save if @quantity.save
flash[:notice] = 'Created new quantity' flash[:notice] = 'Created new quantity'
redirect_to project_quantities_url(@project) prepare_quantities
else else
render :new
end
end
def edit
@quantity.build_formula unless @quantity.formula
end
def update
if @quantity.update(quantity_params)
flash[:notice] = 'Updated quantity'
prepare_quantities prepare_quantities
render :index render :index
else
render :edit
end
end
def destroy
@quantity_tree = @quantity.self_and_descendants.load
if @quantity.destroy
flash[:notice] = 'Deleted quantity'
end end
end end
@ -34,25 +53,9 @@ class QuantitiesController < ApplicationController
render :index render :index
end end
def edit def parents
prepare_quantities @form = params[:form]
render :index @domain = params[:quantity][:domain]
end
def update
if @quantity.update(quantity_params)
flash[:notice] = 'Updated quantity'
end
prepare_quantities
render :index
end
def destroy
if @quantity.destroy
flash[:notice] = 'Deleted quantity'
end
prepare_quantities
render :index
end end
def move def move

View File

@ -0,0 +1,15 @@
<%= labelled_form_for @quantity,
url: quantity_path(@quantity),
method: :patch, remote: true,
html: {id: 'edit-quantity-form', name: 'edit-quantity-form'} do |f| %>
<%= render partial: 'quantities/form', locals: {f: f} %>
<div class="tabular">
<p>
<%= submit_tag l(:button_save) %>
<%= link_to l(:button_cancel), "#",
onclick: '$(this).closest("tr").remove(); return false;' %>
</p>
</div>
<% end %>

View File

@ -18,6 +18,7 @@
<% <%
next if q.new_record? next if q.new_record?
quantity_class = "quantity" quantity_class = "quantity"
quantity_class += " primary" unless q.column_views.empty?
quantity_class += " project idnt idnt-#{level+1}" quantity_class += " project idnt idnt-#{level+1}"
%> %>
<tr id="quantity-<%= q.id %>" class="<%= quantity_class %>"> <tr id="quantity-<%= q.id %>" class="<%= quantity_class %>">
@ -52,30 +53,6 @@
<%= delete_link quantity_path(q), {remote: true, data: {}} %> <%= delete_link quantity_path(q), {remote: true, data: {}} %>
</td> </td>
</tr> </tr>
<% next unless (q == @quantity) &&
((action_name == "edit") ||
(action_name == "update" && @quantity.errors.present?)) %>
<tr>
<td class="form" colspan="6">
<div id="edit-quantity">
<%= labelled_form_for @quantity,
url: quantity_path(@quantity),
method: :patch, remote: true,
html: {id: 'quantity-edit-form', name: 'quantity-edit-form'} do |f| %>
<%= render partial: 'quantities/form', locals: {f: f} %>
<div class="tabular">
<p>
<%= submit_tag l(:button_save) %>
<%= link_to l(:button_cancel), "#",
onclick: '$(this).closest("tr").remove(); return false;' %>
</p>
</div>
<% end %>
</div>
</td>
</tr>
<% end %> <% end %>
</tbody> </tbody>
</table> </table>

View File

@ -0,0 +1,18 @@
<h2><%= t ".heading_new_quantity" %></h2>
<%= labelled_form_for @quantity,
url: project_quantities_path(@project),
remote: true,
html: {id: 'new-quantity-form', name: 'new-quantity-form'} do |f| %>
<%= render partial: 'quantities/form', locals: {f: f} %>
<div class="tabular">
<p>
<%= submit_tag l(:button_create) %>
<%= link_to l(:button_cancel), "#",
onclick: '$("#new-quantity").empty(); return false;' %>
</p>
</div>
<% end %>
<hr>

View File

@ -0,0 +1,3 @@
<%= render partial: 'body_trackers/flash' %>
$('#new-quantity').empty();
$('#quantities').html('<%= j render partial: 'quantities/index' %>');

View File

@ -0,0 +1,6 @@
<%= render partial: 'body_trackers/flash' %>
<% if @quantity.destroyed? %>
<% @quantity_tree.each do |q| %>
$('tr[id=quantity-<%= q.id %>]').nextUntil('tr.quantity').addBack().remove();
<% end %>
<% end %>

View File

@ -0,0 +1,8 @@
<%= render partial: 'body_trackers/flash' %>
$('tr[id=quantity-<%= @quantity.id %>]').nextUntil('tr.quantity').remove();
var columns = $('table > thead > tr > th').length;
$('tr[id=quantity-<%= @quantity.id %>]').nextAll('tr.quantity').first().before(
'<tr><td class="form" colspan="'+columns+'">' +
'<div id="edit-quantity"><%= j render partial: "quantities/edit_form" %></div>' +
'</td></tr>'
);

View File

@ -1,28 +1,11 @@
<div class="contextual"> <div class="contextual">
<% if User.current.allowed_to?(:manage_common, @project) %> <% if User.current.allowed_to?(:manage_common, @project) %>
<%= link_to t(".link_new_quantity"), '#', class: 'icon icon-add', <%= link_to t(".link_new_quantity"), new_project_quantity_path(@project),
onclick: '$("#add-quantity").toggle(); $("#quantity_name").focus(); return false;' %> {remote: true, class: 'icon icon-add'} %>
<% end %> <% end %>
</div> </div>
<div id="add-quantity" <%= 'style=display:none;' if @quantity.errors.empty? %>> <div id="new-quantity">
<h2><%= t ".heading_new_quantity" %></h2>
<%= labelled_form_for @quantity,
url: project_quantities_path(@project),
html: {id: 'quantity-add-form', name: 'quantity-add-form'} do |f| %>
<%= render partial: 'quantities/form', locals: {f: f} %>
<div class="tabular">
<p>
<%= submit_tag l(:button_create) %>
<%= link_to l(:button_cancel), "#",
onclick: '$("#add-quantity").hide(); return false;' %>
</p>
</div>
<% end %>
<hr>
</div> </div>
<h2><%= t ".heading" %></h2> <h2><%= t ".heading" %></h2>

View File

@ -0,0 +1,2 @@
<%= render partial: 'body_trackers/flash' %>
$('#new-quantity').html('<%= j render partial: 'quantities/new_form' %>');

View File

@ -33,7 +33,9 @@ en:
attributes: attributes:
parent: parent:
parent_domain_mismatch: 'parent quantity has to be in the same domain' parent_domain_mismatch: 'parent quantity has to be in the same domain'
formula: formula:
attributes:
code:
disallowed_syntax: 'cannot be parsed: %{syntax}' disallowed_syntax: 'cannot be parsed: %{syntax}'
disallowed_token: 'includes disallowed token: "%{token}"' disallowed_token: 'includes disallowed token: "%{token}"'
disallowed_keyword: 'includes disallowed keyword: "%{keyword}"' disallowed_keyword: 'includes disallowed keyword: "%{keyword}"'

View File

@ -25,7 +25,7 @@ resources :projects, shallow: true do
end end
end end
resources :sources, only: [:index, :create, :destroy] resources :sources, only: [:index, :create, :destroy]
resources :quantities, only: [:index, :create, :edit, :update, :destroy] do resources :quantities, only: [:index, :new, :create, :edit, :update, :destroy] do
member do member do
post 'move/:direction', to: 'quantities#move', as: :move post 'move/:direction', to: 'quantities#move', as: :move
end end

View File

@ -28,7 +28,7 @@ Redmine::Plugin.register :body_tracking do
ingredients: [:new, :create, :edit, :update, :destroy, :toggle, :toggle_column, ingredients: [:new, :create, :edit, :update, :destroy, :toggle, :toggle_column,
:import], :import],
sources: [:create, :destroy], sources: [:create, :destroy],
quantities: [:create, :edit, :update, :destroy, :move], quantities: [:new, :create, :edit, :update, :destroy, :move],
units: [:create, :destroy], units: [:create, :destroy],
}, require: :loggedin }, require: :loggedin
end end