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 new file mode 100644 index 0000000..8a68130 --- /dev/null +++ b/app/models/wishlist.rb @@ -0,0 +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 new file mode 100644 index 0000000..0271d3f --- /dev/null +++ b/app/models/wishlist_product.rb @@ -0,0 +1,6 @@ +class WishlistProduct < ApplicationRecord + 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 new file mode 100644 index 0000000..d50da09 --- /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, default:0 + + 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/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/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/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 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