diff --git a/app/controllers/quantities_controller.rb b/app/controllers/quantities_controller.rb index 3e054bc..043b9de 100644 --- a/app/controllers/quantities_controller.rb +++ b/app/controllers/quantities_controller.rb @@ -2,29 +2,48 @@ class QuantitiesController < ApplicationController menu_item :body_trackers 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 :authorize def index - @quantity = @project.quantities.new - @quantity.domain = Quantity.domains[session[:q_filters][:domain]] || @quantity.domain prepare_quantities end - def parents - @form = params[:form] - @domain = params[:quantity][:domain] + def new + @quantity = @project.quantities.new + @quantity.domain = Quantity.domains[session[:q_filters][:domain]] || @quantity.domain + @quantity.build_formula end def create @quantity = @project.quantities.new(quantity_params) if @quantity.save flash[:notice] = 'Created new quantity' - redirect_to project_quantities_url(@project) + prepare_quantities 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 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 @@ -34,25 +53,9 @@ class QuantitiesController < ApplicationController render :index end - def edit - prepare_quantities - render :index - 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 + def parents + @form = params[:form] + @domain = params[:quantity][:domain] end def move diff --git a/app/views/quantities/_edit_form.html.erb b/app/views/quantities/_edit_form.html.erb new file mode 100644 index 0000000..681a718 --- /dev/null +++ b/app/views/quantities/_edit_form.html.erb @@ -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} %> + +
+

+ <%= submit_tag l(:button_save) %> + <%= link_to l(:button_cancel), "#", + onclick: '$(this).closest("tr").remove(); return false;' %> +

+
+<% end %> diff --git a/app/views/quantities/_index.html.erb b/app/views/quantities/_index.html.erb index e345f54..3d509d1 100644 --- a/app/views/quantities/_index.html.erb +++ b/app/views/quantities/_index.html.erb @@ -18,6 +18,7 @@ <% next if q.new_record? quantity_class = "quantity" + quantity_class += " primary" unless q.column_views.empty? quantity_class += " project idnt idnt-#{level+1}" %> @@ -52,30 +53,6 @@ <%= delete_link quantity_path(q), {remote: true, data: {}} %> - <% next unless (q == @quantity) && - ((action_name == "edit") || - (action_name == "update" && @quantity.errors.present?)) %> - - -
- <%= 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} %> - -
-

- <%= submit_tag l(:button_save) %> - <%= link_to l(:button_cancel), "#", - onclick: '$(this).closest("tr").remove(); return false;' %> -

-
- <% end %> -
- - <% end %> diff --git a/app/views/quantities/_new_form.html.erb b/app/views/quantities/_new_form.html.erb new file mode 100644 index 0000000..abed99b --- /dev/null +++ b/app/views/quantities/_new_form.html.erb @@ -0,0 +1,18 @@ +

<%= t ".heading_new_quantity" %>

+ +<%= 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} %> + +
+

+ <%= submit_tag l(:button_create) %> + <%= link_to l(:button_cancel), "#", + onclick: '$("#new-quantity").empty(); return false;' %> +

+
+<% end %> +
diff --git a/app/views/quantities/create.js.erb b/app/views/quantities/create.js.erb new file mode 100644 index 0000000..7eb20de --- /dev/null +++ b/app/views/quantities/create.js.erb @@ -0,0 +1,3 @@ +<%= render partial: 'body_trackers/flash' %> +$('#new-quantity').empty(); +$('#quantities').html('<%= j render partial: 'quantities/index' %>'); diff --git a/app/views/quantities/destroy.js.erb b/app/views/quantities/destroy.js.erb new file mode 100644 index 0000000..d71c52d --- /dev/null +++ b/app/views/quantities/destroy.js.erb @@ -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 %> diff --git a/app/views/quantities/edit.js.erb b/app/views/quantities/edit.js.erb new file mode 100644 index 0000000..b81888b --- /dev/null +++ b/app/views/quantities/edit.js.erb @@ -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( + '' + + '
<%= j render partial: "quantities/edit_form" %>
' + + '' +); diff --git a/app/views/quantities/index.html.erb b/app/views/quantities/index.html.erb index 54d1486..64aa6e4 100644 --- a/app/views/quantities/index.html.erb +++ b/app/views/quantities/index.html.erb @@ -1,28 +1,11 @@
<% if User.current.allowed_to?(:manage_common, @project) %> - <%= link_to t(".link_new_quantity"), '#', class: 'icon icon-add', - onclick: '$("#add-quantity").toggle(); $("#quantity_name").focus(); return false;' %> + <%= link_to t(".link_new_quantity"), new_project_quantity_path(@project), + {remote: true, class: 'icon icon-add'} %> <% end %>
-
> -

<%= t ".heading_new_quantity" %>

- - <%= 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} %> - -
-

- <%= submit_tag l(:button_create) %> - <%= link_to l(:button_cancel), "#", - onclick: '$("#add-quantity").hide(); return false;' %> -

-
- <% end %> -
+

<%= t ".heading" %>

diff --git a/app/views/quantities/new.js.erb b/app/views/quantities/new.js.erb new file mode 100644 index 0000000..4083893 --- /dev/null +++ b/app/views/quantities/new.js.erb @@ -0,0 +1,2 @@ +<%= render partial: 'body_trackers/flash' %> +$('#new-quantity').html('<%= j render partial: 'quantities/new_form' %>'); diff --git a/config/locales/en.yml b/config/locales/en.yml index 53cb108..795d2d0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -33,7 +33,9 @@ en: attributes: parent: parent_domain_mismatch: 'parent quantity has to be in the same domain' - formula: + formula: + attributes: + code: disallowed_syntax: 'cannot be parsed: %{syntax}' disallowed_token: 'includes disallowed token: "%{token}"' disallowed_keyword: 'includes disallowed keyword: "%{keyword}"' diff --git a/config/routes.rb b/config/routes.rb index b03b8fb..9702467 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -25,7 +25,7 @@ resources :projects, shallow: true do end end 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 post 'move/:direction', to: 'quantities#move', as: :move end diff --git a/init.rb b/init.rb index f306c43..419aaef 100644 --- a/init.rb +++ b/init.rb @@ -28,7 +28,7 @@ Redmine::Plugin.register :body_tracking do ingredients: [:new, :create, :edit, :update, :destroy, :toggle, :toggle_column, :import], sources: [:create, :destroy], - quantities: [:create, :edit, :update, :destroy, :move], + quantities: [:new, :create, :edit, :update, :destroy, :move], units: [:create, :destroy], }, require: :loggedin end