From 0c42365c5fc3338e5cfb551f3507984b953988eb Mon Sep 17 00:00:00 2001 From: Jan Werkhoven Date: Fri, 29 Dec 2023 16:21:56 +1100 Subject: [PATCH] Make it possible for ProductVideo to be loaded by Admin --- .../v1/admin/product_videos_controller.rb | 49 +++++++++++++++++++ .../v1/admin/products_controller.rb | 17 ++++--- app/controllers/v1/admin/videos_controller.rb | 2 + .../v1/public/products_controller.rb | 16 +++--- app/models/product.rb | 8 ++- app/models/product_video.rb | 23 +-------- app/models/video.rb | 2 + .../v1/admin/product_serializer.rb | 3 ++ .../v1/admin/product_video_serializer.rb | 11 +++++ app/serializers/v1/admin/video_serializer.rb | 3 ++ .../v1/public/product_video_serializer.rb | 14 ++---- config/routes.rb | 1 + ...041605_add_rank_public_to_product_video.rb | 12 +++++ ...231229043350_change_product_video_uuids.rb | 6 +++ db/schema.rb | 12 ++--- 15 files changed, 126 insertions(+), 53 deletions(-) create mode 100644 app/controllers/v1/admin/product_videos_controller.rb create mode 100644 app/serializers/v1/admin/product_video_serializer.rb create mode 100644 db/migrate/20231229041605_add_rank_public_to_product_video.rb create mode 100644 db/migrate/20231229043350_change_product_video_uuids.rb diff --git a/app/controllers/v1/admin/product_videos_controller.rb b/app/controllers/v1/admin/product_videos_controller.rb new file mode 100644 index 00000000..90513a39 --- /dev/null +++ b/app/controllers/v1/admin/product_videos_controller.rb @@ -0,0 +1,49 @@ +module V1 + module Admin + class ProductVideosController < V1::AdminController + def index + allow_index + end + + def show + allow_show + end + + def create + allow_create + end + + def update + allow_update + end + + def destroy + allow_destroy + end + + private + + def model_class + ProductVideo + end + + def serializer_class + V1::Admin::ProductVideoSerializer + end + + def creatable_attributes + %i[ + rank + public + ] + end + + def creatable_relationships + %i[ + product + video + ] + end + end + end +end diff --git a/app/controllers/v1/admin/products_controller.rb b/app/controllers/v1/admin/products_controller.rb index 3119ae0b..5cd86a82 100644 --- a/app/controllers/v1/admin/products_controller.rb +++ b/app/controllers/v1/admin/products_controller.rb @@ -39,21 +39,24 @@ def permitted_filters def permitted_includes %i[ - image - images documents documents.language - qualities - uses - product_images.image + image + images + main_family + product_documents product_family product_family.product_family product_images - product_documents + product_images.image product_qualities product_uses - main_family + product_videos + product_videos.video + qualities sub_family + uses + videos ] end diff --git a/app/controllers/v1/admin/videos_controller.rb b/app/controllers/v1/admin/videos_controller.rb index 8eebf9cb..f0d092c9 100644 --- a/app/controllers/v1/admin/videos_controller.rb +++ b/app/controllers/v1/admin/videos_controller.rb @@ -34,6 +34,8 @@ def serializer_class def permitted_includes %i[ webinar + products + product_videos ] end diff --git a/app/controllers/v1/public/products_controller.rb b/app/controllers/v1/public/products_controller.rb index 39260ad7..0f36c3ee 100644 --- a/app/controllers/v1/public/products_controller.rb +++ b/app/controllers/v1/public/products_controller.rb @@ -50,22 +50,24 @@ def permanent_filters def permitted_includes %i[ - image - images documents documents.language - qualities - uses - product_images.image + image + images + main_family + product_documents product_family product_family.product_family product_images - product_documents + product_images.image product_qualities product_uses product_uses.image - main_family + product_videos + qualities sub_family + uses + videos ] end end diff --git a/app/models/product.rb b/app/models/product.rb index cae8e641..85a65acb 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -21,6 +21,9 @@ class Product < ApplicationRecord has_many :product_images, dependent: :destroy has_many :images, through: :product_images, source: :image + has_many :product_videos, dependent: :destroy + has_many :videos, through: :product_videos, source: :video + has_many :product_documents, dependent: :destroy has_many :documents, through: :product_documents, source: :document @@ -80,6 +83,10 @@ def todo_after_save .where(product_id: old_slug) .find_each { |x| x.update(product_id: new_slug) } + ProductVideo + .where(product_id: old_slug) + .find_each { |x| x.update(product_id: new_slug) } + ProductQuality .where(product_id: old_slug) .find_each { |x| x.update(product_id: new_slug) } @@ -93,6 +100,5 @@ def todo_after_save # TODO: drop ProductSubstitute # TODO: drop ProductRelatedArticle ? # TODO: drop ProductFeature ? - # TODO: drop ProductVideo ? end end diff --git a/app/models/product_video.rb b/app/models/product_video.rb index 1a8ee3db..248b3b4f 100644 --- a/app/models/product_video.rb +++ b/app/models/product_video.rb @@ -1,23 +1,4 @@ class ProductVideo < ApplicationRecord - # Relate to a model - # belongs_to :company - # belongs_to :main_group, class_name: 'ProductGroup', foreign_key: 'main_group_id' - # belongs_to :sub_group, class_name: 'ProductGroup', foreign_key: 'sub_group_id' - - # Relate to many of another model - # Requires intermediary model ProductRelatedArticle - # has_many :related_articles_association, class_name: 'ProductRelatedArticle' - # has_many :related_articles, through: :related_articles_association, source: :article - - # Relate to many of same model - # Requires intermediary model ProductRelatedProduct - # has_many :related_products_association, class_name: 'ProductRelatedProduct' - # has_many :related_products, through: :related_products_association, source: :related_product - # has_many :inverse_related_products_association, class_name: 'ProductRelatedProduct', foreign_key: 'related_product_id' - # has_many :inverse_related_products, through: :inverse_related_products_association, source: :product - - # validates :name, presence: true, uniqueness: true - - # If foo `belongs_to :bar`, then the foos table has a bar_id column. - # If foo `has_one :bar`, then the bars table has a foo_id column. + belongs_to :product + belongs_to :video end diff --git a/app/models/video.rb b/app/models/video.rb index 799b89b7..b067a541 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -1,3 +1,5 @@ class Video < ApplicationRecord belongs_to :webinar, optional: true + has_many :product_videos, dependent: :destroy + has_many :products, through: :product_videos, source: :product end diff --git a/app/serializers/v1/admin/product_serializer.rb b/app/serializers/v1/admin/product_serializer.rb index 98f83939..2fa42e76 100644 --- a/app/serializers/v1/admin/product_serializer.rb +++ b/app/serializers/v1/admin/product_serializer.rb @@ -38,6 +38,9 @@ class ProductSerializer < ApplicationSerializer has_many :images, if: requested?('images') has_many :product_images, if: requested?('product_images') + has_many :videos, if: requested?('images') + has_many :product_videos, if: requested?('product_images') + has_many :documents, if: requested?('documents') has_many :product_documents, if: requested?('product_documents') end diff --git a/app/serializers/v1/admin/product_video_serializer.rb b/app/serializers/v1/admin/product_video_serializer.rb new file mode 100644 index 00000000..55ff973f --- /dev/null +++ b/app/serializers/v1/admin/product_video_serializer.rb @@ -0,0 +1,11 @@ +module V1 + module Admin + class ProductVideoSerializer < ApplicationSerializer + attributes :rank, + :public + + belongs_to :product + belongs_to :video + end + end +end diff --git a/app/serializers/v1/admin/video_serializer.rb b/app/serializers/v1/admin/video_serializer.rb index ffb17faa..fe97cea8 100644 --- a/app/serializers/v1/admin/video_serializer.rb +++ b/app/serializers/v1/admin/video_serializer.rb @@ -5,6 +5,9 @@ class VideoSerializer < ApplicationSerializer :variations, :title, :notes + + has_many :product_videos, if: requested?('product_videos') + has_many :products, if: requested?('products') end end end diff --git a/app/serializers/v1/public/product_video_serializer.rb b/app/serializers/v1/public/product_video_serializer.rb index dfee743e..26d151af 100644 --- a/app/serializers/v1/public/product_video_serializer.rb +++ b/app/serializers/v1/public/product_video_serializer.rb @@ -1,17 +1,11 @@ module V1 module Public class ProductVideoSerializer < ApplicationSerializer - # attributes :slug, - # :name, - # :public + attributes :rank, + :public - # belongs_to :thing - # belongs_to :main_group, record_type: :product_group, serializer: :product_group - # belongs_to :sub_group, record_type: :product_group, serializer: :product_group - - # has_many :things - # has_many :related_products, record_type: :product, serializer: :related_products - # has_many :related_articles, record_type: :article, serializer: :related_articles + belongs_to :product + belongs_to :video end end end diff --git a/config/routes.rb b/config/routes.rb index e51a9040..77f001d0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -75,6 +75,7 @@ resources :product_family_images, path: '/product-family-images' resources :product_features, path: '/product-features' resources :product_images, path: '/product-images' + resources :product_videos, path: '/product-videos' resources :product_qualities, path: '/product-qualities' resources :product_uses, path: '/product-uses' resources :products, path: '/products' diff --git a/db/migrate/20231229041605_add_rank_public_to_product_video.rb b/db/migrate/20231229041605_add_rank_public_to_product_video.rb new file mode 100644 index 00000000..f8db2f01 --- /dev/null +++ b/db/migrate/20231229041605_add_rank_public_to_product_video.rb @@ -0,0 +1,12 @@ +class AddRankPublicToProductVideo < ActiveRecord::Migration[6.1] + def change + add_column :product_videos, :rank, :integer + add_column :product_videos, :public, :boolean + + remove_index :product_videos, name: 'index_product_videos_on_product_id' + remove_index :product_videos, name: 'index_product_videos_on_video_id' + + remove_column :product_videos, :created_at + remove_column :product_videos, :updated_at + end +end diff --git a/db/migrate/20231229043350_change_product_video_uuids.rb b/db/migrate/20231229043350_change_product_video_uuids.rb new file mode 100644 index 00000000..9d1a6d5a --- /dev/null +++ b/db/migrate/20231229043350_change_product_video_uuids.rb @@ -0,0 +1,6 @@ +class ChangeProductVideoUuids < ActiveRecord::Migration[6.1] + def change + change_column(:product_videos, :product_id, :string) + change_column(:product_videos, :video_id, :string) + end +end diff --git a/db/schema.rb b/db/schema.rb index 7b2e84ff..55bd96ea 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: 2023_12_11_090932) do +ActiveRecord::Schema.define(version: 2023_12_29_043350) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -404,12 +404,10 @@ end create_table "product_videos", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| - t.uuid "product_id" - t.uuid "video_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["product_id"], name: "index_product_videos_on_product_id" - t.index ["video_id"], name: "index_product_videos_on_video_id" + t.string "product_id" + t.string "video_id" + t.integer "rank" + t.boolean "public" end create_table "products", primary_key: "slug", id: :string, force: :cascade do |t|