2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class UsersStarProject < ApplicationRecord
|
2019-01-28 07:19:53 -05:00
|
|
|
include Sortable
|
|
|
|
|
2015-12-01 15:11:12 -05:00
|
|
|
belongs_to :project, counter_cache: :star_count, touch: true
|
2014-06-26 03:49:14 -04:00
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
validates :user, presence: true
|
|
|
|
validates :user_id, uniqueness: { scope: [:project_id] }
|
|
|
|
validates :project, presence: true
|
2019-01-28 07:19:53 -05:00
|
|
|
|
|
|
|
alias_attribute :starred_since, :created_at
|
|
|
|
|
2019-08-03 06:57:33 -04:00
|
|
|
scope :order_user_name_asc, -> { joins(:user).merge(User.order_name_asc) }
|
|
|
|
scope :order_user_name_desc, -> { joins(:user).merge(User.order_name_desc) }
|
2019-01-28 07:19:53 -05:00
|
|
|
scope :by_project, -> (project) { where(project_id: project.id) }
|
2019-02-02 14:48:27 -05:00
|
|
|
scope :with_visible_profile, -> (user) { joins(:user).merge(User.with_visible_profile(user)) }
|
2019-07-27 02:26:53 -04:00
|
|
|
scope :with_public_profile, -> { joins(:user).merge(User.with_public_profile) }
|
2019-08-19 16:33:07 -04:00
|
|
|
scope :preload_users, -> { preload(:user) }
|
2019-01-28 07:19:53 -05:00
|
|
|
|
|
|
|
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)
|
2022-02-07 10:15:53 -05:00
|
|
|
joins(:user).merge(User.search(query, use_minimum_char_limit: false))
|
2019-01-28 07:19:53 -05:00
|
|
|
end
|
|
|
|
end
|
2014-06-26 03:49:14 -04:00
|
|
|
end
|