Skip to content

Instantly share code, notes, and snippets.

@jmarsh24
Created March 5, 2021 17:02
Show Gist options
  • Save jmarsh24/46a93588e3852ac0112543761249466e to your computer and use it in GitHub Desktop.
Save jmarsh24/46a93588e3852ac0112543761249466e to your computer and use it in GitHub Desktop.
<%= turbo_frame_tag dom_id(@video) do %>
<div class="video-info-details-main">
<h4>Performers</h4>
</div>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Leader",
value: link_to(@video.leader.try(:name),
root_path(leader: @video.leader.name), {'data-turbo-frame': "_top"}) } if @video.leader.present? %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Follower",
value: link_to(@video.follower.try(:name),
root_path(follower: @video.follower.name), {'data-turbo-frame': "_top"}) } if @video.follower.present? %>
<% if @video.event.present? %>
<div class="detail-section-label">
<h4>Event Info</h4>
</div>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Title",
value: link_to(@video.event.title.to_s.titleize, root_path(event_id: @video.event.id)) } %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Location",
value: "#{@video.event.city.to_s.titleize}, #{@video.event.country.to_s.titleize}" } %>
<% end %>
<% if @video.song.present? || @video.spotify_track_name.present? || @video.youtube_song.present? %>
<div class="detail-section-label">
<h4>Music</h4>
</div>
<% end %>
<% if @video.song.present? %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Title",
value: link_to(@video.song.try(:title).to_s.titleize,
root_path(song_id: @video.song.id), {'data-turbo-frame': "_top"}) } %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Genre",
value: @video.song.try(:genre).to_s.titleize } %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Orchestra",
value: link_to(@video.song.try(:artist).to_s.titleize,
root_path(orchestra: @video.song.artist), {'data-turbo-frame': "_top"}) } %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Singer",
value: @video.song.try(:artist_2).to_s.titleize } if @video.song.artist_2.present? %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Recording Date",
value: @video.song.date.strftime('%B %d, %Y') } if @video.song.date.present? %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Composer",
value: @video.song.composer.to_s.titleize } if @video.song.composer.present? %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Author",
value: @video.song.author.to_s.titleize } if @video.song.author.present? %>
<% end %>
<% if @video.song.blank? && @video.spotify_track_name.present? %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Title",
value: link_to(@video.spotify_track_name.to_s.titleize,
root_path(query: @video.spotify_track_name.to_s.titleize + ' ' + @video.spotify_artist_name.to_s.titleize), {'data-turbo-frame': "_top"}) } %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Artist",
value: link_to(@video.spotify_artist_name.to_s.titleize,
root_path(query: @video.spotify_artist_name.to_s.titleize), {'data-turbo-frame': "_top"}) } %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Artist 2",
value: link_to(@video.spotify_artist_name_2.to_s.titleize,
root_path(query: @video.spotify_artist_name_2.to_s.titleize), {'data-turbo-frame': "_top"}) } if @video.spotify_artist_name_2.present? %>
<% end %>
<% if @video.song.blank? && @video.spotify_track_name.blank? && @video.youtube_song.present? %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Title",
value: link_to(@video.youtube_song.to_s.titleize,
root_path(query: @video.youtube_song.to_s.titleize), {'data-turbo-frame': "_top"}) } %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Artist",
value: link_to(@video.youtube_artist.to_s.titleize,
root_path(query: @video.youtube_artist.to_s.titleize), {'data-turbo-frame': "_top"}) } %>
<% end %>
<% if user_signed_in? %>
<%= link_to 'Edit', edit_video_path(@video) %>
<% end %>
<% end %>
<div class="detail-section-label">
<h4>Video Info</h4>
</div>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Title",
value: @video.title } %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Description",
value: auto_link(simple_format(truncate(@video.description, length: 200, omission: ''), {'data-turbo-frame': "_top"}), html: { target: '_blank' }) } if @video.description.present? %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Upload Date",
value: @video.upload_date.to_formatted_s(:long) } %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Duration",
value: Time.at(@video.duration).utc.strftime("%M:%S") } %>
<div class="detail-section-label">
</div>
<%= turbo_frame_tag dom_id(@video) do %>
<div class="video-info-details-main">
<div class="detail-section-label">
<h4>Performers</h4>
</div>
<%= form_with model: @video do |f| %>
<%= render partial: "videos/edit/video_info_detail_item_edit",
locals: { params_name: :leader_id,
id: 'leader',
f: f,
url: '/leaders',
value: @video.leader.try(:name) } %>
<%= render partial: "videos/edit/video_info_detail_item_edit",
locals: { params_name: :follower_id,
id: 'follower',
f: f,
url: '/followers',
value: @video.follower.try(:name) } %>
<div class="detail-section-label">
<h4>Event Info</h4>
</div>
<%= render partial: "videos/edit/video_info_detail_item_edit",
locals: { params_name: :event_id,
id: 'event',
f: f,
url: '/events',
value: @video.event.try(:title) } %>
<h4>Music</h4>
</div>
<%= render partial: "videos/edit/video_info_detail_item_edit",
locals: { params_name: :song_id,
id: 'song',
f: f,
url: '/songs',
value: @video.song.try(:title) } %>
<div>
<%= f.label 'hidden?' %>
<%= f.check_box :hidden ,{}, "true", "false" %>
</div>
<div>
<%= f.submit 'Update Video' %>
</div>
<% end %>
<div>
<%= link_to 'Back', watch_path(v: @video.youtube_id) %>
</div>
<div class="detail-section-label">
<h4>Video Info</h4>
</div>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Title",
value: @video.title } %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Description",
value: auto_link(simple_format(truncate(@video.description, length: 200, omission: '')), html: { target: '_blank' }) } if @video.description.present? %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Upload Date",
value: @video.upload_date.to_formatted_s(:long) } %>
<%= render partial: "videos/show/video_info_item",
locals: { header: "Duration",
value: Time.at(@video.duration).utc.strftime("%M:%S") } %>
<div class="edit-video-item-container">
</div>
<% end %>
class VideosController < ApplicationController
before_action :authenticate_user!, only: %i[edit update]
before_action :set_video, only: %i[show edit]
NUMBER_OF_VIDEOS_PER_PAGE = 120
helper_method :sort_column, :sort_direction
def index
@videos_total = Video.filter_by_not_hidden.size
@videos = Video.filter_by_not_hidden
.includes(:leader, :follower, :channel, :song, :event)
.order("#{sort_column} #{sort_direction}")
.filter_videos(filtering_params)
@videos_paginated = @videos.paginate(page, NUMBER_OF_VIDEOS_PER_PAGE)
@videos_paginated = @videos_paginated.shuffle if filtering_params.blank?
@next_page_items = @videos.paginate(page + 1, NUMBER_OF_VIDEOS_PER_PAGE)
@items_display_count = (@videos.size - (@videos.size - (page * NUMBER_OF_VIDEOS_PER_PAGE).clamp(0, @videos.size)))
@leaders = @videos.joins(:leader).pluck("leaders.name").uniq.sort.map(&:titleize)
@followers = @videos.joins(:follower).pluck("followers.name").uniq.sort.map(&:titleize)
@channels = @videos.joins(:channel).pluck("channels.title").uniq.compact.sort
@artists = @videos.joins(:song).pluck("songs.artist").uniq.compact.sort.map(&:titleize)
@genres = @videos.joins(:song).pluck("songs.genre").uniq.compact.sort.map(&:titleize).uniq
respond_to do |format|
format.html
format.json do
render json: { videos: render_to_string(partial: "videos/index/videos", formats: [:html]),
loadmore: render_to_string(partial: "videos/index/load_more", formats: [:html]) }
end
end
end
def show
@videos_total = Video.filter_by_not_hidden.size
videos = if Video.where(song_id: @video.song_id).size > 3
Video.where(song_id: @video.song_id)
else
Video.where(channel_id: @video.channel_id)
end
@recommended_videos = videos.where(hidden: false)
.where.not(youtube_id: @video.youtube_id)
.order("popularity DESC")
.limit(3)
@video.clicked!
end
def edit
@video = Video.find_by(id: params[:id])
end
def update
@video = Video.find_by(id: params[:id])
@video.update(video_params)
redirect_to watch_path(v: @video.youtube_id)
end
private
def current_search
@current_search = params[:query]
end
def set_video
@video = Video.find_by(youtube_id: params[:v])
end
def sort_column
acceptable_cols = ["songs.title",
"songs.artist",
"songs.genre",
"leaders.name",
"followers.name",
"channels.title",
"videos.upload_date",
"videos.view_count",
"songs.last_name_search",
"videos.popularity"]
acceptable_cols.include?(params[:sort]) ? params[:sort] : "videos.popularity"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end
def page
@page ||= params.permit(:page).fetch(:page, 1).to_i
end
def video_params
params.require(:video).permit(:leader_id, :follower_id, :song_id, :event_id, :hidden)
end
def filtering_params
params.permit(:leader, :follower, :channel, :genre, :orchestra, :song_id, :query, :hd, :event_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment