diff --git a/app/models/product.rb b/app/models/product.rb index ae69b960..5342ee06 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -73,7 +73,7 @@ class Product < ApplicationRecord # has_many :containers, through: :product_containers, source: :container validates :name, :slug, presence: true - validates :status, inclusion: { in: %w[new popular recommended outdated discontinued offline] } + validates :status, inclusion: { in: %w[new popular promoted demoted replaced discontinued offline] } after_save :todo_after_save diff --git a/db/migrate/20260820161727_update_recommended_products_to_promoted.rb b/db/migrate/20260820161727_update_recommended_products_to_promoted.rb new file mode 100644 index 00000000..2362fe3e --- /dev/null +++ b/db/migrate/20260820161727_update_recommended_products_to_promoted.rb @@ -0,0 +1,9 @@ +class UpdateRecommendedProductsToPromoted < ActiveRecord::Migration[6.1] + def up + Product.where(status: 'recommended').update_all(status: 'promoted') + end + + def down + Product.where(status: 'promoted').update_all(status: 'recommended') + end +end diff --git a/db/migrate/20260820162054_update_outdated_products_to_demoted_or_replaced.rb b/db/migrate/20260820162054_update_outdated_products_to_demoted_or_replaced.rb new file mode 100644 index 00000000..61fb543c --- /dev/null +++ b/db/migrate/20260820162054_update_outdated_products_to_demoted_or_replaced.rb @@ -0,0 +1,10 @@ +class UpdateOutdatedProductsToDemotedOrReplaced < ActiveRecord::Migration[6.1] + def up + Product.where(status: 'outdated', superior_product_id: nil).update_all(status: 'demoted') + Product.where(status: 'outdated').where.not(superior_product_id: nil).update_all(status: 'replaced') + end + + def down + Product.where(status: %w[demoted replaced]).update_all(status: 'outdated') + end +end diff --git a/db/schema.rb b/db/schema.rb index cd2aef1d..380b2139 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2025_06_04_083038) do +ActiveRecord::Schema.define(version: 2026_08_20_162054) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" diff --git a/test/fixtures/products.yml b/test/fixtures/products.yml index ee4239e1..d78d585b 100644 --- a/test/fixtures/products.yml +++ b/test/fixtures/products.yml @@ -39,39 +39,46 @@ LMPA_Q6: slug: LMPA-Q6 public: true product_family: solder_paste - status: 'new' + status: new IF_2005M: name: IF 2005M slug: IF-2005M public: true product_family: soldering_flux - status: 'popular' + status: popular Pacific_2009M: name: PacIFic 2009M slug: PacIFic-2009M public: true product_family: soldering_flux - status: 'recommended' + status: promoted + +WSF-7700: + name: WSF-7700 + slug: WSF-7700 + public: true + product_family: soldering_flux + status: demoted LMPA_Q5: name: LMPA Q5 slug: LMPA-Q5 public: true product_family: solder_paste - status: 'discontinued' + status: discontinued LMPA_Q4: name: LMPA Q4 slug: LMPA-Q4 public: true product_family: solder_paste - status: 'outdated' + status: replaced LMPA_Q7: name: LMPA Q7 slug: LMPA-Q7 public: false product_family: solder_paste - status: 'offline' + status: offline diff --git a/test/integration/v1/public/products_integration_test.rb b/test/integration/v1/public/products_integration_test.rb index fd52efc7..ad67636b 100644 --- a/test/integration/v1/public/products_integration_test.rb +++ b/test/integration/v1/public/products_integration_test.rb @@ -16,8 +16,8 @@ def setup json = JSON.parse(@response.body) - # Should return 5 out of 6 products (should filter out public=false) - assert_equal 5, json['data'].length + # Should return 6 out of 7 products (should filter out public=false) + assert_equal 6, json['data'].length end test 'can fetch one by slug' do @@ -44,7 +44,7 @@ def setup end test 'can include product family' do - skip("TODO: improve test") + skip('TODO: improve test') get '/v1/public/products/LMPA-Q6?include=product_family', headers: @header @@ -61,7 +61,7 @@ def setup end test 'can include family and images' do - skip("TODO: includes become optional in the payload") + skip('TODO: includes become optional in the payload') # curl "http://localhost:3000/v1/public/products?slug=LMPA-Q6&include=product-family,product-images,product-images.image" -H "Content-Type: application/vnd.api+json" get '/v1/public/products?slug=IF-2005M&include=product-family,product-images,product-images.image', headers: @header diff --git a/test/models/product_test.rb b/test/models/product_test.rb index 0e720176..1a578fbe 100644 --- a/test/models/product_test.rb +++ b/test/models/product_test.rb @@ -78,7 +78,7 @@ class ProductTest < ActiveSupport::TestCase assert_not product.valid? end - test 'valid with 5 statuses' do + test 'valid with 6 statuses' do product = Product.new( name: 'Foo 2000', slug: 'Foo-2000', @@ -89,12 +89,16 @@ class ProductTest < ActiveSupport::TestCase assert product.valid? product.update(status: 'popular') assert product.valid? - product.update(status: 'recommended') + product.update(status: 'promoted') assert product.valid? - product.update(status: 'outdated') + product.update(status: 'demoted') + assert product.valid? + product.update(status: 'replaced') assert product.valid? product.update(status: 'discontinued') assert product.valid? + product.update(status: 'bogus') + assert_not product.valid? end test 'defaults new products to offline' do