From c023b4506e3a9d6e0f84a95088860a32a9bdf01f Mon Sep 17 00:00:00 2001 From: OrngSreyMoch Date: Wed, 12 Jun 2024 16:17:49 +0700 Subject: [PATCH] close #1523 create thumbnail model for video on demand --- .../spree/admin/video_on_demands_controller.rb | 10 ++++++++++ app/models/spree_cm_commissioner/video_on_demand.rb | 5 ++++- .../video_on_demand_thumbnail.rb | 11 +++++++++++ app/views/spree/admin/video_on_demands/edit.html.erb | 5 ++--- app/views/spree/admin/video_on_demands/index.html.erb | 6 ++---- .../20240531074327_create_cm_video_on_demands.rb | 2 +- 6 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 app/models/spree_cm_commissioner/video_on_demand_thumbnail.rb diff --git a/app/controllers/spree/admin/video_on_demands_controller.rb b/app/controllers/spree/admin/video_on_demands_controller.rb index 9b7c267c7..c5d3b0460 100644 --- a/app/controllers/spree/admin/video_on_demands_controller.rb +++ b/app/controllers/spree/admin/video_on_demands_controller.rb @@ -3,6 +3,16 @@ module Admin class VideoOnDemandsController < Spree::Admin::ResourceController before_action :load_parent before_action :load_video_on_demands, only: %i[index new edit] + before_action :build_thumbnails, only: :create + before_action :create_thumbnails, only: :update + + def build_thumbnails + @object.build_thumbnail(attachment: permitted_resource_params.delete(:thumbnail)) if permitted_resource_params[:thumbnail] + end + + def create_thumbnails + @object.create_thumbnail(attachment: permitted_resource_params.delete(:thumbnail)) if permitted_resource_params[:thumbnail] + end def edit @video_on_demand = SpreeCmCommissioner::VideoOnDemand.find(params[:id]) diff --git a/app/models/spree_cm_commissioner/video_on_demand.rb b/app/models/spree_cm_commissioner/video_on_demand.rb index f69f39429..c2cedb51d 100644 --- a/app/models/spree_cm_commissioner/video_on_demand.rb +++ b/app/models/spree_cm_commissioner/video_on_demand.rb @@ -1,8 +1,11 @@ module SpreeCmCommissioner class VideoOnDemand < SpreeCmCommissioner::Base belongs_to :variant, class_name: 'Spree::Variant' + has_one_attached :file - has_one_attached :thumbnail + has_one :thumbnail, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::VideoOnDemandThumbnail' + + validates_associated :thumbnail validates :title, :description, :file, :thumbnail, presence: true validates :variant_id, uniqueness: true diff --git a/app/models/spree_cm_commissioner/video_on_demand_thumbnail.rb b/app/models/spree_cm_commissioner/video_on_demand_thumbnail.rb new file mode 100644 index 000000000..0aae6aa65 --- /dev/null +++ b/app/models/spree_cm_commissioner/video_on_demand_thumbnail.rb @@ -0,0 +1,11 @@ +module SpreeCmCommissioner + class VideoOnDemandThumbnail < SpreeCmCommissioner::Asset + def asset_styles + { + mini: '160x90>', + small: '240x135>', + large: '360x202>' + } + end + end +end diff --git a/app/views/spree/admin/video_on_demands/edit.html.erb b/app/views/spree/admin/video_on_demands/edit.html.erb index 05ba2ee0d..ff09f10b2 100644 --- a/app/views/spree/admin/video_on_demands/edit.html.erb +++ b/app/views/spree/admin/video_on_demands/edit.html.erb @@ -8,9 +8,8 @@
<%= f.label :thumbnail, Spree.t(:thumbnail) %> -
- <%= image_tag (main_app.rails_blob_url(@video_on_demand.thumbnail)), height: "200px" if @video_on_demand&.thumbnail %> -
+ <%= image_tag main_app.cdn_image_url(@video_on_demand.thumbnail.url(:medium)) if @video_on_demand.thumbnail %> +
diff --git a/app/views/spree/admin/video_on_demands/index.html.erb b/app/views/spree/admin/video_on_demands/index.html.erb index 951f9a287..c79dd68de 100644 --- a/app/views/spree/admin/video_on_demands/index.html.erb +++ b/app/views/spree/admin/video_on_demands/index.html.erb @@ -8,7 +8,7 @@ <% if !@video_on_demands.any? %>
<%= Spree.t(:no_resource_found, resource: plural_resource_name(SpreeCmCommissioner::VideoOnDemand)) %>, - <%= link_to(Spree.t(:add_one), admin_product_video_on_demands_url(@product)) if can? :create, SpreeCmCommissioner::VideoOnDemand %>! + <%= link_to(Spree.t(:add_one), new_admin_product_video_on_demand_url(@product)) if can? :create, SpreeCmCommissioner::VideoOnDemand %>!
<% else %>
@@ -28,9 +28,7 @@ <%= video_on_demand.title %> <%= video_on_demand.variant.options_text %> -
- <%= image_tag (main_app.rails_blob_url(video_on_demand.thumbnail)) if video_on_demand&.thumbnail&.attached? %> -
+ <%= image_tag main_app.cdn_image_url(video_on_demand.thumbnail.url(:mini)) if video_on_demand.thumbnail %> <% if video_on_demand&.file.attached? %> diff --git a/db/migrate/20240531074327_create_cm_video_on_demands.rb b/db/migrate/20240531074327_create_cm_video_on_demands.rb index dc3494fce..5267768de 100644 --- a/db/migrate/20240531074327_create_cm_video_on_demands.rb +++ b/db/migrate/20240531074327_create_cm_video_on_demands.rb @@ -1,6 +1,6 @@ class CreateCmVideoOnDemands < ActiveRecord::Migration[7.0] def change - create_table :cm_video_on_demands do |t| + create_table :cm_video_on_demands, if_not_exists: true do |t| t.string :title t.text :description