From 2164a1baeaa96910d0eb83d7b02cc8441911a74a Mon Sep 17 00:00:00 2001 From: Tbadru Date: Tue, 27 Jan 2026 16:19:08 +0000 Subject: [PATCH 1/2] wishlist models wishlist models --- app/models/wishlist.rb | 3 +++ app/models/wishlist_product.rb | 4 ++++ db/migrate/20260127155829_create_wishlists.rb | 11 +++++++++++ .../20260127155849_create_wishlist_products.rb | 13 +++++++++++++ test/fixtures/wishlist_products.yml | 9 +++++++++ test/fixtures/wishlists.yml | 11 +++++++++++ test/models/wishlist_product_test.rb | 7 +++++++ test/models/wishlist_test.rb | 7 +++++++ 8 files changed, 65 insertions(+) create mode 100644 app/models/wishlist.rb create mode 100644 app/models/wishlist_product.rb create mode 100644 db/migrate/20260127155829_create_wishlists.rb create mode 100644 db/migrate/20260127155849_create_wishlist_products.rb create mode 100644 test/fixtures/wishlist_products.yml create mode 100644 test/fixtures/wishlists.yml create mode 100644 test/models/wishlist_product_test.rb create mode 100644 test/models/wishlist_test.rb diff --git a/app/models/wishlist.rb b/app/models/wishlist.rb new file mode 100644 index 0000000..1fa7f0e --- /dev/null +++ b/app/models/wishlist.rb @@ -0,0 +1,3 @@ +class Wishlist < ApplicationRecord + belongs_to :user +end diff --git a/app/models/wishlist_product.rb b/app/models/wishlist_product.rb new file mode 100644 index 0000000..146e2e5 --- /dev/null +++ b/app/models/wishlist_product.rb @@ -0,0 +1,4 @@ +class WishlistProduct < ApplicationRecord + belongs_to :product + belongs_to :wishlist +end diff --git a/db/migrate/20260127155829_create_wishlists.rb b/db/migrate/20260127155829_create_wishlists.rb new file mode 100644 index 0000000..9600ec3 --- /dev/null +++ b/db/migrate/20260127155829_create_wishlists.rb @@ -0,0 +1,11 @@ +class CreateWishlists < ActiveRecord::Migration[8.1] + def change + create_table :wishlists do |t| + t.belongs_to :user, null: false, foreign_key: true + t.string :name + t.integer :products_count + + t.timestamps + end + end +end diff --git a/db/migrate/20260127155849_create_wishlist_products.rb b/db/migrate/20260127155849_create_wishlist_products.rb new file mode 100644 index 0000000..8c33d9e --- /dev/null +++ b/db/migrate/20260127155849_create_wishlist_products.rb @@ -0,0 +1,13 @@ +class CreateWishlistProducts < ActiveRecord::Migration[8.1] + def change + create_table :wishlist_products do |t| + t.belongs_to :product, null: false, foreign_key: true + t.belongs_to :wishlist, null: false, foreign_key: true + + t.timestamps + end + + add index :wishlist_products, [:product_id, :wishlist_id], + unique: true + end +end diff --git a/test/fixtures/wishlist_products.yml b/test/fixtures/wishlist_products.yml new file mode 100644 index 0000000..48caec4 --- /dev/null +++ b/test/fixtures/wishlist_products.yml @@ -0,0 +1,9 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + product: one + wishlist: one + +two: + product: two + wishlist: two diff --git a/test/fixtures/wishlists.yml b/test/fixtures/wishlists.yml new file mode 100644 index 0000000..ffbe0ad --- /dev/null +++ b/test/fixtures/wishlists.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + user: one + name: MyString + products_count: 1 + +two: + user: two + name: MyString + products_count: 1 diff --git a/test/models/wishlist_product_test.rb b/test/models/wishlist_product_test.rb new file mode 100644 index 0000000..9dac4b8 --- /dev/null +++ b/test/models/wishlist_product_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class WishlistProductTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/wishlist_test.rb b/test/models/wishlist_test.rb new file mode 100644 index 0000000..18a1ffa --- /dev/null +++ b/test/models/wishlist_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class WishlistTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From f7fe21e9197add10ceb70b72c737741f1226e057 Mon Sep 17 00:00:00 2001 From: Tbadru Date: Tue, 27 Jan 2026 16:57:10 +0000 Subject: [PATCH 2/2] wishlist models 2 --- app/models/product.rb | 3 ++- app/models/user.rb | 1 + app/models/wishlist.rb | 4 ++++ app/models/wishlist_product.rb | 6 ++++-- db/migrate/20260127155829_create_wishlists.rb | 2 +- .../20260127162030_add_wishlists_count_to_products.rb | 5 +++++ test/integration/settings_test.rb | 2 +- 7 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20260127162030_add_wishlists_count_to_products.rb diff --git a/app/models/product.rb b/app/models/product.rb index 9d28b4b..b38096e 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -1,7 +1,8 @@ class Product < ApplicationRecord include Notifications - + has_many :wishlist_products, dependent: :destroy + has_many :wishlists, through: :wishlist_products has_one_attached :featured_image has_rich_text :description has_many :subscribers, dependent: :destroy diff --git a/app/models/user.rb b/app/models/user.rb index a983083..7fd0ede 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,7 @@ class User < ApplicationRecord has_secure_password has_many :sessions, dependent: :destroy + has_many :wishlists, dependent: :destroy attr_readonly :admin diff --git a/app/models/wishlist.rb b/app/models/wishlist.rb index 1fa7f0e..8a68130 100644 --- a/app/models/wishlist.rb +++ b/app/models/wishlist.rb @@ -1,3 +1,7 @@ class Wishlist < ApplicationRecord belongs_to :user + has_many :wishlist_products, dependent: :destroy + has_many :products, through: :wishlist_products + +to_param :name end diff --git a/app/models/wishlist_product.rb b/app/models/wishlist_product.rb index 146e2e5..0271d3f 100644 --- a/app/models/wishlist_product.rb +++ b/app/models/wishlist_product.rb @@ -1,4 +1,6 @@ class WishlistProduct < ApplicationRecord - belongs_to :product - belongs_to :wishlist + belongs_to :product, counter_cache: :wishlists_count + belongs_to :wishlist, counter_cache: :products_count + + validates :product_id, uniqueness: { scope: :wishlist_id } end diff --git a/db/migrate/20260127155829_create_wishlists.rb b/db/migrate/20260127155829_create_wishlists.rb index 9600ec3..d50da09 100644 --- a/db/migrate/20260127155829_create_wishlists.rb +++ b/db/migrate/20260127155829_create_wishlists.rb @@ -3,7 +3,7 @@ def change create_table :wishlists do |t| t.belongs_to :user, null: false, foreign_key: true t.string :name - t.integer :products_count + t.integer :products_count, default:0 t.timestamps end diff --git a/db/migrate/20260127162030_add_wishlists_count_to_products.rb b/db/migrate/20260127162030_add_wishlists_count_to_products.rb new file mode 100644 index 0000000..d14718b --- /dev/null +++ b/db/migrate/20260127162030_add_wishlists_count_to_products.rb @@ -0,0 +1,5 @@ +class AddWishlistsCountToProducts < ActiveRecord::Migration[8.1] + def change + add_column :products, :wishlists_count, :integer, default:0 + end +end diff --git a/test/integration/settings_test.rb b/test/integration/settings_test.rb index 1e41277..a3bbe28 100644 --- a/test/integration/settings_test.rb +++ b/test/integration/settings_test.rb @@ -34,7 +34,7 @@ class SettingsTest < ActionDispatch::IntegrationTest get store_products_path assert_response :success end - + test "admins can access /store/users" do sign_in_as users(:admin) get store_users_path