diff options
author | Camil Staps <info@camilstaps.nl> | 2019-01-28 15:19:53 +0300 |
---|---|---|
committer | Camil Staps <info@camilstaps.nl> | 2019-08-07 21:49:14 +0300 |
commit | 6baff6504b234e209a5e1d78349dbd2589d5d7e8 (patch) | |
tree | 809269461586e00974cb8c309c77aca77b058e49 /app/models/users_star_project.rb | |
parent | 91f574b820e03b4abb97a8351e420d1ef24e8505 (diff) |
Change sorting options for starrers: name (asc/desc), most/least recent star
Diffstat (limited to 'app/models/users_star_project.rb')
-rw-r--r-- | app/models/users_star_project.rb | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/app/models/users_star_project.rb b/app/models/users_star_project.rb index 9be6bd2e6f3..62d44aaf955 100644 --- a/app/models/users_star_project.rb +++ b/app/models/users_star_project.rb @@ -1,10 +1,35 @@ # frozen_string_literal: true class UsersStarProject < ApplicationRecord + include Sortable + belongs_to :project, counter_cache: :star_count, touch: true belongs_to :user validates :user, presence: true validates :user_id, uniqueness: { scope: [:project_id] } 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 |