Skip to content

Commit

Permalink
Merge 54d160b into 0f1d352
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed May 7, 2024
2 parents 0f1d352 + 54d160b commit e1664b7
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 42 deletions.
12 changes: 0 additions & 12 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ def format_number(number)
number_to_human(number, units: {thousand: "K", million: "M", billion: "B"}, precision: 1, significant: false)
end

def is_active?(controller: "", path: "")
params[:controller].in?(Array(controller)) || (path.is_a?(Regexp) ? (path =~ request.path) : (path == request.path))
end

def page_title_tag(title)
title_suffix = " - #{t(:app_name)}"
title = "#{title}#{title_suffix unless native_app? || dialog?}"
Expand All @@ -90,12 +86,4 @@ def page_title_tag(title)
def current_url
url_for(only_path: false)
end

def filter_sort_params(args = {})
filter_params = params[:filter].presence || {}
filter_params = filter_params.merge(args.delete(:filter) || {})
new_params = params.merge(args).merge(filter: filter_params)

new_params.slice(:filter, :sort, :sort_direction).permit!
end
end
31 changes: 31 additions & 0 deletions app/presenters/filter_sort_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

class FilterSortPresenter
def initialize(params)
@params = params
end

def has_filter?
params[:filter].present?
end

def filter_value(key)
params[:filter].fetch(key, nil)
end

def sort_value
params[:sort]
end

def sort_direction_value
params[:sort_direction]
end

def params(other_hash = {})
filter_params = @params[:filter].presence || {}
filter_params = filter_params.merge(other_hash.delete(:filter) || {})
new_params = @params.merge(other_hash).merge(filter: filter_params)

new_params.slice(:filter, :sort, :sort_direction).permit!
end
end
6 changes: 3 additions & 3 deletions app/views/albums/_filters.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% if filter_sort_params[:filter].present? %>
<% if filter_sort_presenter.has_filter? %>
<%= link_to albums_path, class: "c-button c-button--outline c-button--small u-mr-small", data: {"turbo-action" => ("replace" if native_app?)} do %>
<span class='o-flex o-flex--align-center'>
<%= icon_tag "close" %>
Expand All @@ -15,7 +15,7 @@
</span>
</summary>
<div class='c-dropdown__menu c-dropdown__menu--right' data-dropdown-target="menu">
<%= turbo_frame_tag "turbo-genre-filter", src: albums_filter_genres_path(**filter_sort_params), loading: "lazy" do %>
<%= turbo_frame_tag "turbo-genre-filter", src: albums_filter_genres_path(**filter_sort_presenter.params), loading: "lazy" do %>
<div class='o-flex o-flex--justify-center u-p-medium'>
<%= loader_tag size: "small" %>
</div>
Expand All @@ -31,7 +31,7 @@
</span>
</summary>
<div class='c-dropdown__menu' data-dropdown-target="menu">
<%= turbo_frame_tag "turbo-year-filter", src: albums_filter_years_path(**filter_sort_params), loading: "lazy" do %>
<%= turbo_frame_tag "turbo-year-filter", src: albums_filter_years_path(**filter_sort_presenter.params), loading: "lazy" do %>
<div class='o-flex o-flex--justify-center u-p-medium'>
<%= loader_tag size: "small" %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/albums/filter/genres/index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= render partial: "shared/filter_options", locals: {filter_name: "genre", options: @genres, filter_controller: "/albums"} %>
<%= render partial: "shared/filter_options", locals: {filter_name: "genre", options: @genres, filter_controller: "/albums", filter_sort_presenter: FilterSortPresenter.new(params)} %>
2 changes: 1 addition & 1 deletion app/views/albums/filter/years/index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= render partial: "shared/filter_options", locals: {filter_name: "year", options: @years, filter_controller: "/albums"} %>
<%= render partial: "shared/filter_options", locals: {filter_name: "year", options: @years, filter_controller: "/albums", filter_sort_presenter: FilterSortPresenter.new(params)} %>
6 changes: 4 additions & 2 deletions app/views/albums/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<% page_title_tag t("label.albums") %>
<% filter_sort_presenter = FilterSortPresenter.new(params) %>

<div class='o-container o-container--large'>
<div class='o-flex o-flex--align-center o-flex--justify-between o-flex--wrap u-mb-large'>
<% unless native_app? %>
<h1 class='u-my-narrow u-mr-small'><%= t("label.albums") %></h1>
<% end %>
<div class='o-flex o-flex--align-center <%= "u-ml-auto" if native_app? %>'>
<%= render partial: "albums/filters" %>
<%= render partial: "shared/sort_select", locals: {option: @sort_option} %>
<%= render partial: "albums/filters", locals: {filter_sort_presenter: filter_sort_presenter} %>
<%= render partial: "shared/sort_select", locals: {option: @sort_option, sort_controller: controller_name, filter_sort_presenter: filter_sort_presenter} %>
</div>
</div>
<% if @albums.empty? %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/artists/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<h1 class='u-my-narrow u-mr-small'><%= t("label.artists") %></h1>
<% end %>
<div class='<%= "u-ml-auto" if native_app? %>'>
<%= render partial: "shared/sort_select", locals: {option: @sort_option} %>
<%= render partial: "shared/sort_select", locals: {option: @sort_option, sort_controller: controller_name, filter_sort_presenter: FilterSortPresenter.new(params)} %>
</div>
</div>
<% if @artists.empty? %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
<div>
<header class='c-nav c-nav--primary u-position-sticky-top'>
<%= render "shared/flash" %>
<%= render "shared/search_bar" %>
<%= render "shared/nav_bar" %>
<%= render partial: "shared/search_bar", locals: {current_user: Current.user, current_session: Current.session, query: params[:query]} %>
<%= render partial: "shared/nav_bar", locals: {current_controller: controller_name} %>
</header>
<main class='o-container--wide u-m-auto u-p-large u-p-small@small u-p-narrow@extra-narrow'>
<%= yield %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/playlists/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</div>

<div class='<%= "u-ml-auto" if native_app? %>'>
<%= render partial: "shared/sort_select", locals: {option: @sort_option} %>
<%= render partial: "shared/sort_select", locals: {option: @sort_option, sort_controller: controller_name, filter_sort_presenter: FilterSortPresenter.new(params)} %>
</div>
</div>

Expand Down
4 changes: 2 additions & 2 deletions app/views/shared/_filter_options.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= turbo_frame_tag "turbo-#{filter_name}-filter".dasherize do %>
<% options.each do |option| %>
<%= link_to(
url_for(controller: filter_controller, **filter_sort_params(filter: {filter_name => option})),
url_for(controller: filter_controller, **filter_sort_presenter.params(filter: {filter_name => option})),
class: "c-dropdown__item",
data: {
"turbo-frame" => "_top",
Expand All @@ -10,7 +10,7 @@
) do %>
<span class='o-flex o-flex--justify-between o-flex--align-center'>
<%= option %>
<% if params[:filter]&.fetch(filter_name, nil) == option.to_s %>
<% if filter_sort_presenter.filter_value(filter_name) == option.to_s %>
<%= icon_tag("check", size: "small", class: "u-ml-narrow") %>
<% end %>
</span>
Expand Down
4 changes: 2 additions & 2 deletions app/views/shared/_nav_bar.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<nav class='u-mt-medium'>
<ul class='c-tab'>
<li class='c-tab__item <%= "is-active" if is_active?(controller: "home") %>'>
<li class='c-tab__item <%= "is-active" if current_controller == "home" %>'>
<%= link_to t("label.home"), root_path %>
</li>
<li class='c-tab__item <%= "is-active" if is_active?(controller: "libraries") %>'>
<li class='c-tab__item <%= "is-active" if current_controller == "libraries" %>'>
<%= link_to t("label.library"), library_path %>
</li>
</ul>
Expand Down
8 changes: 4 additions & 4 deletions app/views/shared/_search_bar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<%= icon_tag "search", size: "large" %>
</div>

<%= text_field_tag "query", params[:query], class: "c-input", data: {"search-target" => "input", "test-id" => "search_input"}, autocomplete: "on" %>
<%= text_field_tag "query", query, class: "c-input", data: {"search-target" => "input", "test-id" => "search_input"}, autocomplete: "on" %>

<div class='c-input-group__icon'>
<%= loader_tag size: "small" %>
Expand All @@ -15,12 +15,12 @@

<div class='o-flex o-flex--align-center o-flex__item o-flex__item--grow-1 o-flex__item--basic-0'>
<details class='c-dropdown u-ml-auto' data-controller='dropdown' data-test-id='search_bar_menu'>
<summary><%= avatar_tag Current.user %></summary>
<summary><%= avatar_tag current_user %></summary>
<div class='c-dropdown__menu' data-dropdown-target="menu">
<%= link_to t("label.settings"), setting_path, class: "c-dropdown__item" %>
<%= link_to t("label.manage_users"), users_path, class: "c-dropdown__item" if is_admin? %>
<%= link_to t("label.update_profile"), edit_user_path(Current.user), class: "c-dropdown__item" %>
<%= button_to t("button.logout"), Current.session, method: :delete, form_class: "c-dropdown__item" %>
<%= link_to t("label.update_profile"), edit_user_path(current_user), class: "c-dropdown__item" %>
<%= button_to t("button.logout"), current_session, method: :delete, form_class: "c-dropdown__item" %>
</div>
</details>
</div>
Expand Down
8 changes: 4 additions & 4 deletions app/views/shared/_sort_select.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
</summary>
<div class='c-dropdown__menu' data-dropdown-target="menu">
<% option.values.each do |sort_value| %>
<%= link_to url_for(controller: params[:controller], action: :index, **filter_sort_params(sort: sort_value)), class: "c-dropdown__item", data: {"turbo-action" => ("replace" if native_app?)} do %>
<%= link_to url_for(controller: sort_controller, action: :index, **filter_sort_presenter.params(sort: sort_value)), class: "c-dropdown__item", data: {"turbo-action" => ("replace" if native_app?)} do %>
<span class='o-flex o-flex--justify-between o-flex--align-center'>
<%= t("field.#{sort_value}") %>
<% if params[:sort] == sort_value || (params[:sort].blank? && sort_value == option.default.name) %>
<% if filter_sort_presenter.sort_value == sort_value || (filter_sort_presenter.sort_value.blank? && sort_value == option.default.name) %>
<%= icon_tag("check", size: "small", class: "u-ml-narrow") %>
<% end %>
</span>
Expand All @@ -17,10 +17,10 @@
<hr>

<% %w[asc desc].each do |sort_direction| %>
<%= link_to url_for(controller: params[:controller], action: :index, **filter_sort_params(sort: params[:sort] || option.default.name, sort_direction: sort_direction)), class: "c-dropdown__item", data: {"turbo-action" => ("replace" if native_app?)} do %>
<%= link_to url_for(controller: sort_controller, action: :index, **filter_sort_presenter.params(sort: filter_sort_presenter.sort_value || option.default.name, sort_direction: sort_direction)), class: "c-dropdown__item", data: {"turbo-action" => ("replace" if native_app?)} do %>
<span class='o-flex o-flex--justify-between o-flex--align-center'>
<%= t("label.#{sort_direction}") %>
<% if params[:sort_direction] == sort_direction || (params[:sort_direction].blank? && option.default.direction == sort_direction) %>
<% if filter_sort_presenter.sort_direction_value == sort_direction || (filter_sort_presenter.sort_direction_value.blank? && option.default.direction == sort_direction) %>
<%= icon_tag("check", size: "small", class: "u-ml-narrow") %>
<% end %>
</span>
Expand Down
6 changes: 3 additions & 3 deletions app/views/songs/_filters.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% if filter_sort_params[:filter].present? %>
<% if filter_sort_presenter.has_filter? %>
<%= link_to songs_path, class: "c-button c-button--outline c-button--small u-mr-small", data: {"turbo-action" => ("replace" if native_app?)} do %>
<span class='o-flex o-flex--align-center'>
<%= icon_tag "close" %>
Expand All @@ -15,7 +15,7 @@
</span>
</summary>
<div class='c-dropdown__menu c-dropdown__menu--right' data-dropdown-target="menu">
<%= turbo_frame_tag "turbo-album-genre-filter", src: songs_filter_genres_path(**filter_sort_params), loading: "lazy" do %>
<%= turbo_frame_tag "turbo-album-genre-filter", src: songs_filter_genres_path(**filter_sort_presenter.params), loading: "lazy" do %>
<div class='o-flex o-flex--justify-center u-p-medium'>
<%= loader_tag size: "small" %>
</div>
Expand All @@ -31,7 +31,7 @@
</span>
</summary>
<div class='c-dropdown__menu' data-dropdown-target="menu">
<%= turbo_frame_tag "turbo-album-year-filter", src: songs_filter_years_path(**filter_sort_params), loading: "lazy" do %>
<%= turbo_frame_tag "turbo-album-year-filter", src: songs_filter_years_path(**filter_sort_presenter.params), loading: "lazy" do %>
<div class='o-flex o-flex--justify-center u-p-medium'>
<%= loader_tag size: "small" %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/songs/filter/genres/index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= render partial: "shared/filter_options", locals: {filter_name: "album_genre", options: @genres, filter_controller: "/songs"} %>
<%= render partial: "shared/filter_options", locals: {filter_name: "album_genre", options: @genres, filter_controller: "/songs", filter_sort_presenter: FilterSortPresenter.new(params)} %>
2 changes: 1 addition & 1 deletion app/views/songs/filter/years/index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= render partial: "shared/filter_options", locals: {filter_name: "album_year", options: @years, filter_controller: "/songs"} %>
<%= render partial: "shared/filter_options", locals: {filter_name: "album_year", options: @years, filter_controller: "/songs", filter_sort_presenter: FilterSortPresenter.new(params)} %>
6 changes: 4 additions & 2 deletions app/views/songs/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<% page_title_tag t("label.songs") %>
<% filter_sort_presenter = FilterSortPresenter.new(params) %>

<div class='o-container o-container--large'>
<div class='o-flex o-flex--align-center o-flex--justify-between o-flex--wrap u-mb-large'>
<% unless native_app? %>
<h1 class='u-my-narrow u-mr-small'><%= t("label.songs") %></h1>
<% end %>
<div class='o-flex o-flex--align-center <%= "u-ml-auto" if native_app? %>'>
<%= render partial: "songs/filters" %>
<%= render partial: "shared/sort_select", locals: {option: @sort_option} %>
<%= render partial: "songs/filters", locals: {filter_sort_presenter: filter_sort_presenter} %>
<%= render partial: "shared/sort_select", locals: {option: @sort_option, sort_controller: controller_name, filter_sort_presenter: filter_sort_presenter} %>
</div>
</div>
<% if @songs.empty? %>
Expand Down

0 comments on commit e1664b7

Please sign in to comment.