diff --git a/app/javascript/packs/price_lists.js b/app/javascript/packs/price_lists.js index 7585ec79d..2b9bd3dbd 100644 --- a/app/javascript/packs/price_lists.js +++ b/app/javascript/packs/price_lists.js @@ -55,6 +55,11 @@ document.addEventListener('turbolinks:load', () => { }, saveProduct: function(product) { + if (product.id && product._beforeEditingCache.name !== product.name) { + if (!confirm('Weet je zeker dat je de productnaam wilt wijzigen? Pas hier mee op want dit kan problemen geven in bestaande orders. Als je twijfelt, maak dan een nieuw product aan in plaats van het bestaande te hernoemen.')) { + return; + } + } const sanitizedProduct = this.sanitizeProductInput(product); if (sanitizedProduct.id) { // Existing product this.$http.put(`/products/${sanitizedProduct.id}.json`, { product: sanitizedProduct }).then( (response) => { diff --git a/app/models/product.rb b/app/models/product.rb index 8082251ac..4ad7b4a1d 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -6,7 +6,6 @@ class Product < ApplicationRecord has_many :price_lists, through: :product_prices, dependent: :restrict_with_error validates :name, :category, presence: true - validate :name_readonly accepts_nested_attributes_for :product_prices, allow_destroy: true @@ -17,12 +16,4 @@ def requires_age def t_category I18n.t category end - - private - - def name_readonly - return if new_record? - - errors.add(:name, 'is readonly') if name_changed? - end end diff --git a/app/views/price_lists/index.html.erb b/app/views/price_lists/index.html.erb index 2998d0dba..8a0da018b 100644 --- a/app/views/price_lists/index.html.erb +++ b/app/views/price_lists/index.html.erb @@ -25,11 +25,11 @@
<% end %> - + @@ -60,7 +60,7 @@
- +
diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index e8d5c8fb2..df5dd88d4 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -21,7 +21,7 @@ context 'when updating the name' do subject(:product) { create(:product) } - it { expect(product.update(name: 'new_name')).to be false } + it { expect(product.update(name: 'new_name')).to be true } end end