Skip to content

Commit

Permalink
Make it possible for ProductVideo to be loaded by Admin
Browse files Browse the repository at this point in the history
  • Loading branch information
janwerkhoven committed Dec 29, 2023
1 parent b0914d4 commit 0c42365
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 53 deletions.
49 changes: 49 additions & 0 deletions app/controllers/v1/admin/product_videos_controller.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 10 additions & 7 deletions app/controllers/v1/admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions app/controllers/v1/admin/videos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def serializer_class
def permitted_includes
%i[
webinar
products
product_videos
]
end

Expand Down
16 changes: 9 additions & 7 deletions app/controllers/v1/public/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) }
Expand All @@ -93,6 +100,5 @@ def todo_after_save
# TODO: drop ProductSubstitute
# TODO: drop ProductRelatedArticle ?
# TODO: drop ProductFeature ?
# TODO: drop ProductVideo ?
end
end
23 changes: 2 additions & 21 deletions app/models/product_video.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/models/video.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions app/serializers/v1/admin/product_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions app/serializers/v1/admin/product_video_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module V1
module Admin
class ProductVideoSerializer < ApplicationSerializer
attributes :rank,
:public

belongs_to :product
belongs_to :video
end
end
end
3 changes: 3 additions & 0 deletions app/serializers/v1/admin/video_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 4 additions & 10 deletions app/serializers/v1/public/product_video_serializer.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20231229041605_add_rank_public_to_product_video.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions db/migrate/20231229043350_change_product_video_uuids.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 5 additions & 7 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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|
Expand Down

0 comments on commit 0c42365

Please sign in to comment.