Skip to content

Commit

Permalink
close #1523 create thumbnail model for video on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
SreyMochOrng committed Jul 1, 2024
1 parent 81fea20 commit c023b45
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
10 changes: 10 additions & 0 deletions app/controllers/spree/admin/video_on_demands_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
5 changes: 4 additions & 1 deletion app/models/spree_cm_commissioner/video_on_demand.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 11 additions & 0 deletions app/models/spree_cm_commissioner/video_on_demand_thumbnail.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module SpreeCmCommissioner
class VideoOnDemandThumbnail < SpreeCmCommissioner::Asset
def asset_styles
{
mini: '160x90>',
small: '240x135>',
large: '360x202>'
}
end
end
end
5 changes: 2 additions & 3 deletions app/views/spree/admin/video_on_demands/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
<div class="col-12 col-md-6">
<div class="form-group">
<%= f.label :thumbnail, Spree.t(:thumbnail) %>
<div class="small-img">
<%= image_tag (main_app.rails_blob_url(@video_on_demand.thumbnail)), height: "200px" if @video_on_demand&.thumbnail %>
</div>
<%= image_tag main_app.cdn_image_url(@video_on_demand.thumbnail.url(:medium)) if @video_on_demand.thumbnail %>

</div>
</div>
<div class="col-12 col-md-6">
Expand Down
6 changes: 2 additions & 4 deletions app/views/spree/admin/video_on_demands/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<% if !@video_on_demands.any? %>
<div class="text-center no-objects-found m-5">
<%= 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 %>!
</div>
<% else %>
<div class="table-responsive border rounded bg-white">
Expand All @@ -28,9 +28,7 @@
<td class="text-center"><%= video_on_demand.title %></td>
<td class="text-center"><%= video_on_demand.variant.options_text %></td>
<td class="image text-center">
<div class="admin-product-image-container small-img">
<%= image_tag (main_app.rails_blob_url(video_on_demand.thumbnail)) if video_on_demand&.thumbnail&.attached? %>
</div>
<%= image_tag main_app.cdn_image_url(video_on_demand.thumbnail.url(:mini)) if video_on_demand.thumbnail %>
</td>
<td class="image text-center">
<% if video_on_demand&.file.attached? %>
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20240531074327_create_cm_video_on_demands.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit c023b45

Please sign in to comment.