Skip to content

Commit

Permalink
Implemented edit all form
Browse files Browse the repository at this point in the history
  • Loading branch information
lentschi committed Oct 27, 2023
1 parent 3353cc7 commit 0002749
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
3 changes: 2 additions & 1 deletion app/assets/javascripts/article-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ArticleForm {
this.unitRatiosTable$ = $('#fc_base_price', this.articleForm$);
this.minimumOrderQuantity$ = $(`#${this.unitFieldsIdPrefix}_minimum_order_quantity`, this.articleForm$);
this.billingUnit$ = $(`#${this.unitFieldsIdPrefix}_billing_unit`, this.articleForm$);
this.groupOrderGranularity$ = $(`#${this.unitFieldsIdPrefix}_group_order_granularity`, this.articleForm$);
this.groupOrderUnit$ = $(`#${this.unitFieldsIdPrefix}_group_order_unit`, this.articleForm$);
this.price$ = $(`#${this.unitFieldsIdPrefix}_price`, this.articleForm$);
this.priceUnit$ = $(`#${this.unitFieldsIdPrefix}_price_unit`, this.articleForm$);
Expand Down Expand Up @@ -121,7 +122,7 @@ class ArticleForm {
return true;
}

if (this.minimumOrderQuantity$.val().trim() != '') {
if (this.minimumOrderQuantity$.val().trim() !== '' || parseFloat(this.groupOrderGranularity$.val().trim()) !== 1) {
return true;
}

Expand Down
10 changes: 7 additions & 3 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class ArticlesController < ApplicationController
before_action :authenticate_article_meta, :find_supplier

before_action :load_article, only: [:edit, :update]
before_action :load_article_units, only: [:edit, :update, :new, :create, :parse_upload, :sync, :update_synchronized]
before_action :load_article_units, only: [:edit, :update, :new, :create, :parse_upload, :sync, :update_synchronized, :edit_all]
before_action :new_empty_article_ratio, only: [:edit, :update, :new, :create, :parse_upload, :sync, :update_synchronized]

def index
Expand Down Expand Up @@ -102,9 +102,13 @@ def update_all
Article.transaction do
unless params[:articles].blank?
# Update other article attributes...
@articles = Article.find(params[:articles].keys)
@articles = Article.with_latest_versions_and_categories
.includes(latest_article_version: [:article_unit_ratios])
.find(params[:articles].keys)
@articles.each do |article|
unless article.update_attributes(params[:articles][article.id.to_s])
article_version_params = params[:articles][article.id.to_s]
article_version_params["id"] = article.latest_article_version.id
unless article.update_attributes(latest_article_version_attributes: article_version_params)
invalid_articles = true unless invalid_articles # Remember that there are validation errors
end
end
Expand Down
56 changes: 50 additions & 6 deletions app/views/articles/_edit_all_table.html.haml
Original file line number Diff line number Diff line change
@@ -1,27 +1,71 @@
:javascript
const units = #{raw(ArticleUnits.units.to_json)};
- price_markup = FoodsoftConfig[:price_markup].to_f
%table.table
%thead
%tr
%th= heading_helper Article, :availability, short: true
%th= heading_helper Article, :name
%th= heading_helper Article, :unit
%th= heading_helper Article, :price, short: true
%th= heading_helper Article, :unit_quantity, short: true
%th= heading_helper Article, :order_number, short: true
%th= heading_helper Article, :note
%th= heading_helper Article, :article_category
%th= heading_helper Article, :tax
%th= heading_helper Article, :deposit
%tbody
- @articles.each_with_index do |article, index|
= fields_for "articles[#{article.id || index}]", article do |form|
%tr
%tr{:id => "article_row_#{article.id}"}
= render 'shared/js_templates/unit_conversion_popover_template'
= simple_fields_for "articles[#{article.id}]", article.latest_article_version do |form|
- content_for :javascript do
:javascript
articleUnitRatioTemplate$ = $($.parseHTML("#{escape_javascript(render(partial: 'shared/article_unit_ratio', locals: {article_unit_ratio: @empty_article_unit_ratio, f: form, article_unit_ratio_counter: -1}))}"));
new ArticleForm(articleUnitRatioTemplate$, $('#article_row_#{article.id}'), units, #{price_markup}, $('#article_row_#{article.id}').closest('form'), 'articles_#{article.id}', 'articles[#{article.id}]');
- if index + 1 === @articles.length
- content_for :javascript do
:javascript
$('input[name="commit"]').removeAttr('disabled');
%td
= yield form # allow to add hidden fields to form
= form.check_box 'availability'
%td= form.text_field 'name', class: 'input-medium'
%td= form.text_field 'unit', class: 'input-mini'
%td= form.text_field 'price', class: 'input-mini'
%td= form.text_field 'unit_quantity', class: 'input-mini'
%td
.d-flex.gap-1.align-items-center
= form.input :supplier_order_unit, as: :select, collection: @article_units, label: false, value: article.supplier_order_unit, include_blank: t('Custom (avoid if possible!)') + ':', input_html: {class: 'input-medium'}
= form.input :unit, input_html: {class: 'input-mini ml-1'}, label: false
%div.btn-link.toggle-extra-units.text-decoration-none.default-values
%i.icon-cog
%div.extra-unit-fields.form-horizontal
.fold-line
.control-group
%label.control-label{for: 'unit_ratios'}
= "Unit ratios"
%table#fc_base_price{:class => "controls"}
%tbody
- ratios = article.article_unit_ratios
= render :partial => 'shared/article_unit_ratio', :as => 'article_unit_ratio', :collection => ratios, locals: {f: form, original_ratios: article&.article_unit_ratios}
%tfoot
%tr
%td{:colspan => 6}
= link_to t('.add_ratio'), '#', 'data-add-ratio' => true, class: 'btn', title: "add ratio"
.fold-line
= form.input :minimum_order_quantity, label: "Mininum order quantity" do
.input-append
= form.input_field :minimum_order_quantity, class: 'input-mini', title: "total minimum order quantity for this article"
.fold-line
= form.input :billing_unit, as: :select, collection: [], input_html: {class: 'input-medium'}, include_blank: false
.fold-line
= form.input :group_order_granularity, label: "Allow orders per", input_html: {class: 'input-mini', title: "steps in which ordergroups can order this article"}
= form.input :group_order_unit, as: :select, collection: [], input_html: {class: 'input-medium'}, label: '&times;'.html_safe, include_blank: false
%td
.d-flex.gap-1
.input-prepend
%span.add-on= t 'number.currency.format.unit'
= form.text_field 'price', class: 'input-mini', style: 'width: 45px'
.input.d-flex.gap-1
%label=t('articles.form.per')
= form.select :price_unit, [], {include_blank: false}, {class: 'input-medium'}
%td= form.text_field 'order_number', class: 'input-mini'
%td= form.text_field 'note', class: 'input-medium'
%td= form.collection_select 'article_category_id', ArticleCategory.all,
Expand Down
2 changes: 1 addition & 1 deletion app/views/articles/edit_all.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
%br/
%i= t '.warning'
.form-actions
= submit_tag t('.submit'), class: 'btn btn-primary'
= submit_tag t('.submit'), class: 'btn btn-primary', disabled: true
= link_to t('ui.or_cancel'), supplier_articles_path(@supplier)

0 comments on commit 0002749

Please sign in to comment.