Commit 6baff650 authored by Camil Staps's avatar Camil Staps

Change sorting options for starrers: name (asc/desc), most/least recent star

parent 91f574b8
...@@ -10,12 +10,9 @@ class Projects::StarrersController < Projects::ApplicationController ...@@ -10,12 +10,9 @@ class Projects::StarrersController < Projects::ApplicationController
def index def index
@sort = params[:sort].presence || sort_value_name @sort = params[:sort].presence || sort_value_name
params[:has_starred] = @project params[:project] = @project
@starrers = UsersFinder.new(current_user, params).execute @starrers = UsersStarProjectsFinder.new(params).execute
@starrers = @starrers.joins(:users_star_projects)
.select('"users".*, "users_star_projects"."created_at" as "starred_since"')
.where(users_star_projects: { project_id: @project.project_id })
@starrers = @starrers.sort_by_attribute(@sort) @starrers = @starrers.sort_by_attribute(@sort)
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
......
...@@ -37,7 +37,6 @@ class UsersFinder ...@@ -37,7 +37,6 @@ class UsersFinder
users = by_2fa(users) users = by_2fa(users)
users = by_created_at(users) users = by_created_at(users)
users = by_custom_attributes(users) users = by_custom_attributes(users)
users = by_has_starred(users)
users users
end end
...@@ -95,8 +94,4 @@ class UsersFinder ...@@ -95,8 +94,4 @@ class UsersFinder
users users
end end
end end
def by_has_starred(items)
params[:has_starred].present? ? items.has_starred(params[:has_starred]) : items
end
end end
# frozen_string_literal: true
class UsersStarProjectsFinder
include CustomAttributesFilter
attr_accessor :params
def initialize(params = {})
@params = params
end
def execute
stars = UsersStarProject.all.order_id_desc
stars = by_search(stars)
stars = by_project(stars)
stars
end
private
def by_search(items)
return items unless params[:search].present?
items.search(params[:search])
end
def by_project(items)
params[:project].present? ? items.by_project(params[:project]) : items
end
end
...@@ -167,6 +167,15 @@ module SortingHelper ...@@ -167,6 +167,15 @@ module SortingHelper
} }
end end
def starrers_sort_options_hash
{
sort_value_name => sort_title_name,
sort_value_name_desc => sort_title_name_desc,
sort_value_recently_created => sort_title_recently_starred,
sort_value_oldest_created => sort_title_oldest_starred
}
end
def sortable_item(item, path, sorted_by) def sortable_item(item, path, sorted_by)
link_to item, path, class: sorted_by == item ? 'is-active' : '' link_to item, path, class: sorted_by == item ? 'is-active' : ''
end end
...@@ -327,6 +336,10 @@ module SortingHelper ...@@ -327,6 +336,10 @@ module SortingHelper
s_('SortOptions|Oldest sign in') s_('SortOptions|Oldest sign in')
end end
def sort_title_oldest_starred
s_('SortOptions|Oldest starred')
end
def sort_title_oldest_updated def sort_title_oldest_updated
s_('SortOptions|Oldest updated') s_('SortOptions|Oldest updated')
end end
...@@ -347,6 +360,10 @@ module SortingHelper ...@@ -347,6 +360,10 @@ module SortingHelper
s_('SortOptions|Recent sign in') s_('SortOptions|Recent sign in')
end end
def sort_title_recently_starred
s_('SortOptions|Recently starred')
end
def sort_title_recently_updated def sort_title_recently_updated
s_('SortOptions|Last updated') s_('SortOptions|Last updated')
end end
......
...@@ -282,7 +282,6 @@ class User < ApplicationRecord ...@@ -282,7 +282,6 @@ class User < ApplicationRecord
scope :for_todos, -> (todos) { where(id: todos.select(:user_id)) } scope :for_todos, -> (todos) { where(id: todos.select(:user_id)) }
scope :with_emails, -> { preload(:emails) } scope :with_emails, -> { preload(:emails) }
scope :with_dashboard, -> (dashboard) { where(dashboard: dashboard) } scope :with_dashboard, -> (dashboard) { where(dashboard: dashboard) }
scope :has_starred, ->(project) { joins(:users_star_projects).where('users_star_projects.project_id': project.id) }
# Limits the users to those that have TODOs, optionally in the given state. # Limits the users to those that have TODOs, optionally in the given state.
# #
......
# frozen_string_literal: true # frozen_string_literal: true
class UsersStarProject < ApplicationRecord class UsersStarProject < ApplicationRecord
include Sortable
belongs_to :project, counter_cache: :star_count, touch: true belongs_to :project, counter_cache: :star_count, touch: true
belongs_to :user belongs_to :user
validates :user, presence: true validates :user, presence: true
validates :user_id, uniqueness: { scope: [:project_id] } validates :user_id, uniqueness: { scope: [:project_id] }
validates :project, presence: true validates :project, presence: true
alias_attribute :starred_since, :created_at
scope :order_user_name_asc, -> { joins(:user).reorder('"users"."name" ASC') }
scope :order_user_name_desc, -> { joins(:user).reorder('"users"."name" DESC') }
scope :by_project, -> (project) { where(project_id: project.id) }
class << self
def sort_by_attribute(method)
order_method = method || 'id_desc'
case order_method.to_s
when 'name_asc' then order_user_name_asc
when 'name_desc' then order_user_name_desc
else
order_by(order_method)
end
end
def search(query)
joins(:user).merge(User.search(query))
end
end
end end
- user = local_assigns.fetch(:user) - starrer = local_assigns.fetch(:starrer)
.col-lg-3.col-md-4.col-sm-12 .col-lg-3.col-md-4.col-sm-12
.card .card
.card-body .card-body
= image_tag avatar_icon_for_user(user, 40), class: "avatar s40", alt: '' = image_tag avatar_icon_for_user(starrer.user, 40), class: "avatar s40", alt: ''
.user-info .user-info
.block-truncated .block-truncated
= link_to user.name, user_path(user), class: 'user js-user-link', data: { user_id: user.id } = link_to starrer.user.name, user_path(starrer.user), class: 'user js-user-link', data: { user_id: starrer.user.id }
.block-truncated .block-truncated
%span.cgray= user.to_reference %span.cgray= starrer.user.to_reference
- if user == current_user - if starrer.user == current_user
%span.badge.badge-success.prepend-left-5= _("It's you") %span.badge.badge-success.prepend-left-5= _("It's you")
.block-truncated .block-truncated
= time_ago_with_tooltip(user.starred_since) = time_ago_with_tooltip(starrer.starred_since)
...@@ -14,16 +14,16 @@ ...@@ -14,16 +14,16 @@
%button.user-search-btn{ type: "submit", "aria-label" => _("Submit search") } %button.user-search-btn{ type: "submit", "aria-label" => _("Submit search") }
= icon("search") = icon("search")
.dropdown.inline.user-sort-dropdown .dropdown.inline.user-sort-dropdown
= dropdown_toggle(users_sort_options_hash[@sort], { toggle: 'dropdown' }) = dropdown_toggle(starrers_sort_options_hash[@sort], { toggle: 'dropdown' })
%ul.dropdown-menu.dropdown-menu-right.dropdown-menu-selectable %ul.dropdown-menu.dropdown-menu-right.dropdown-menu-selectable
%li.dropdown-header %li.dropdown-header
= _("Sort by") = _("Sort by")
- users_sort_options_hash.each do |value, title| - starrers_sort_options_hash.each do |value, title|
%li %li
= link_to filter_user_path(sort: value), class: ("is-active" if @sort == value) do = link_to filter_user_path(sort: value), class: ("is-active" if @sort == value) do
= title = title
- if @starrers.size > 0 - if @starrers.size > 0
.row.prepend-top-10 .row.prepend-top-10
= render partial: 'starrer', collection: @starrers, as: :user = render partial: 'starrer', collection: @starrers, as: :starrer
- else - else
.nothing-here-block Nobody has starred this repository yet .nothing-here-block Nobody has starred this repository yet
...@@ -10268,6 +10268,9 @@ msgstr "" ...@@ -10268,6 +10268,9 @@ msgstr ""
msgid "SortOptions|Oldest sign in" msgid "SortOptions|Oldest sign in"
msgstr "" msgstr ""
msgid "SortOptions|Oldest starred"
msgstr ""
msgid "SortOptions|Oldest updated" msgid "SortOptions|Oldest updated"
msgstr "" msgstr ""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment