Skip to content

Commit

Permalink
Add tag filter to song page
Browse files Browse the repository at this point in the history
  • Loading branch information
jcraigk committed Oct 19, 2023
1 parent 2de8328 commit d091b2c
Show file tree
Hide file tree
Showing 19 changed files with 209 additions and 141 deletions.
8 changes: 7 additions & 1 deletion app/controllers/ambiguity_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ def resolve
redirect_to :root
end

private
protected

def apply_shows_tag_filter
@all_shows = @shows
return if params[:tag_slug].blank? || params[:tag_slug] == 'all'
@shows = @shows.tagged_with(params[:tag_slug])
end

def validate_sorting_for_tracks
params[:sort] = 'shows.date desc' unless
Expand Down
14 changes: 7 additions & 7 deletions app/controllers/concerns/ambiguity/day_of_year.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def slug_as_day_of_year

validate_sorting_for_shows
fetch_shows_on_day_of_year
apply_shows_tag_filter
hydrate_day_of_year

true
Expand All @@ -31,13 +32,12 @@ def fetch_shows_on_day_of_year
end

def day_of_year_sections
@shows.group_by(&:tour_name)
.each_with_object({}) do |(tour, shows), sections|
sections[tour] = {
shows:,
likes: user_likes_for_shows(shows)
}
end
{
'Today in History' => {
shows: @shows,
likes: user_likes_for_shows(@shows)
}
}
end

def month
Expand Down
16 changes: 12 additions & 4 deletions app/controllers/concerns/ambiguity/song_title.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ def hydrate_song_page
@next_song = next_song
@view = 'songs/show'
@ambiguity_controller = 'songs'
@tracks = song.tracks
.includes(:songs, show: :venue, track_tags: :tag)
.order(@order_by)
.paginate(page: params[:page], per_page: params[:per_page].presence || 20)
@tracks = fetch_song_tracks
@tracks_likes = user_likes_for_tracks(@tracks)
end

def fetch_song_tracks
tracks = song.tracks.includes(:songs, show: :venue, track_tags: :tag).order(@order_by)
tracks = tagged_tracks(tracks)
tracks.paginate(page: params[:page], per_page: params[:per_page].presence || 20)
end

def tagged_tracks(tracks)
return tracks if params[:tag_slug].blank? || params[:tag_slug] == 'all'
tracks.tagged_with(params[:tag_slug])
end

def previous_song
Song.where('title < ?', @song.title)
.order(title: :desc)
Expand Down
1 change: 1 addition & 0 deletions app/controllers/concerns/ambiguity/tour_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def tour
def hydrate_tour_page
@ogp_title = "Listen to shows from #{tour.name}"
@shows = tour.shows.order(@order_by)
apply_shows_tag_filter
@shows_likes = user_likes_for_shows(@shows)
@sections = tour_sections
@title = tour.name
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/concerns/ambiguity/venue_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def venue

def hydrate_venue_page
@ogp_title = "Listen to shows from #{venue.name}"
@shows = venue.shows.includes(show_tags: :tag).order(@order_by)
@shows = fetch_shows
@shows_likes = user_likes_for_shows(@shows)
@previous_venue = prev_venue
@next_venue = next_venue
Expand All @@ -25,6 +25,12 @@ def hydrate_venue_page
@ambiguity_controller = 'venues'
end

def fetch_shows
shows = venue.shows.includes(show_tags: :tag).order(@order_by)
return shows if params[:tag_slug].blank? || params[:tag_slug] == 'all'
shows.tagged_with(params[:tag_slug])
end

def prev_venue
Venue.where('name < ?', venue.name).order(name: :desc).first ||
Venue.order(name: :desc).first
Expand Down
1 change: 1 addition & 0 deletions app/controllers/concerns/ambiguity/year.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def shows_during_year
def hydrate_year_page # rubocop:disable Metrics/MethodLength
@ogp_title = "Listen to shows from #{current_slug}"
@shows = shows_during_year
apply_shows_tag_filter
@sections =
@shows.group_by(&:tour_name)
.each_with_object({}) do |(tour, shows), sections|
Expand Down
1 change: 1 addition & 0 deletions app/controllers/concerns/ambiguity/year_range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def slug_as_year_range
def hydrate_year_range_page # rubocop:disable Metrics/MethodLength
@ogp_title = "Listen to shows from #{year1} to #{year2}"
@shows = shows_during_year_range
apply_shows_tag_filter
@sections =
@shows.group_by(&:tour_name)
.each_with_object({}) do |(tour, shows), sections|
Expand Down
120 changes: 120 additions & 0 deletions app/helpers/filter_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
module FilterHelper
def filter_title(param_name, items)
item = items.find { |_k, v| params[param_name] == v }&.first&.html_safe
item.presence || items.first.first.html_safe
end

def filter(param_name, items) # rubocop:disable Metrics/AbcSize
skippables = %w[controller action name t slug] + [param_name.to_s]
items.map do |k, v|
link = params[param_name] == v ? "<strong>#{k}</strong>" : k
param_str = "?#{param_name}=#{CGI.escape(v)}"
params.each do |key, val|
next if key.in?(skippables)
param_str += "&#{key}=#{val}" if val.present?
end
tag.li(link_to(link.html_safe, param_str))
end.join.html_safe
end

def sort_songs_and_venues_links(item_hash)
item_hash.map do |k, v|
link = params[:sort] == v ? "<strong>#{k}</strong>" : k
tag.li(link_to(link.html_safe, "?char=#{params[:char]}&sort=#{CGI.escape(v)}"))
end.join.html_safe
end

def sort_tags_title(item_hash)
item_hash.each_with_index do |(key, val), idx|
if (idx.zero? && params[:sort].blank?) ||
params[:sort] == val
return "<strong>#{key}</strong>".html_safe
end
end
end

def sort_tags_links(item_hash)
str = ''
item_hash.each do |k, v|
link = params[:sort] == v ? "<strong>#{k}</strong>" : k
str += tag.li(link_to(link.html_safe, "?sort=#{CGI.escape(v)}"))
end
str.html_safe
end

def sort_songs_title(items)
items.each_with_index do |(key, val), i|
return key.html_safe if (i.zero? && params[:sort].empty?) ||
params[:sort] == val
end
end

def show_sort_items
{
'Reverse Date' => 'date desc',
'Date' => 'date asc',
'Likes' => 'likes',
'Duration' => 'duration'
}
end

def my_track_sort_items
{ 'Title' => 'title' }.merge(track_sort_items)
end

def track_sort_items
{
'Reverse Date' => 'shows.date desc',
'Date' => 'shows.date asc',
'Likes' => 'likes',
'Duration' => 'duration'
}
end

def songs_and_venues_sort_items
{
'Title' => 'title',
'Track Count' => 'performances'
}
end

def tag_sort_items
{
'Name' => 'name',
'Track Count' => 'tracks_count',
'Show Count' => 'shows_count'
}
end

def venues_sort_items
{
'Name' => 'name',
'Show Count' => 'performances'
}
end

def stored_playlist_sort_items
{
'Name' => 'name',
'Duration' => 'duration'
}
end

def song_track_tag_items(song)
tag_data = song.tracks.joins(:tags).order('tags.name').pluck('tags.name', 'tags.slug').uniq
generic_track_items(tag_data)
end

def shows_tag_items(shows)
tag_data = shows.joins(:tags).order('tags.name').pluck('tags.name', 'tags.slug').uniq
generic_track_items(tag_data)
end

def generic_track_items(tag_data)
items = { 'All Tags' => 'all' }
tag_data.each do |name, slug|
items[name] = slug
end
items
end
end
103 changes: 0 additions & 103 deletions app/helpers/sort_helper.rb

This file was deleted.

15 changes: 14 additions & 1 deletion app/javascript/packs/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ html {
.current {
color: $nav_highlight !important;
font-weight: bold !important;
margin-right: 3px;
}
a:hover, a:focus {
text-decoration: underline !important;
Expand Down Expand Up @@ -1086,7 +1087,6 @@ html {
width: 100%;
border-bottom: 1px solid $hr_gray;
margin-top: 10px;
margin-bottom: 5px;
}
.next_item_link {
float: right;
Expand Down Expand Up @@ -1145,6 +1145,19 @@ html {
font-size: 18px;
}
}
.filter_label {
float: left;
width: 30px;
position: relative;
top: 7px;
font-size: 1.2em;
}
.filter_dropdown {
float: left;
}
.hr {
margin-bottom: 15px;
}
.app_callout {
* {
float: left;
Expand Down
9 changes: 5 additions & 4 deletions app/views/shared/_sort_filter.html.slim
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
h3
|> Sort by
.filter_label Sort
.filter_dropdown
.btn-group
button.btn.btn-default.dropdown-toggle type='button' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'
=> sort_filter_link_title(item_hash)
=> filter_title(:sort, item_hash)
span.caret
ul.dropdown-menu = sort_filter(item_hash)
ul.dropdown-menu = filter(:sort, item_hash)
= clear_both
5 changes: 3 additions & 2 deletions app/views/shared/_sort_songs_and_venues_filter.html.slim
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
h3
|> Sort by
.filter_label Sort
.filter_dropdown
.btn-group
button.btn.btn-default.dropdown-toggle type='button' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'
=> sort_songs_title(item_hash)
span.caret
ul.dropdown-menu = sort_songs_and_venues_links(item_hash)
= clear_both
Loading

0 comments on commit d091b2c

Please sign in to comment.