2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 13:17:42 +00:00
|
|
|
class UsersStarProject < ApplicationRecord
|
2019-01-28 12:19:53 +00:00
|
|
|
include Sortable
|
|
|
|
|
2015-12-01 20:11:12 +00:00
|
|
|
belongs_to :project, counter_cache: :star_count, touch: true
|
2014-06-26 07:49:14 +00:00
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
validates :user, presence: true
|
|
|
|
validates :user_id, uniqueness: { scope: [:project_id] }
|
|
|
|
validates :project, presence: true
|
2019-01-28 12:19:53 +00:00
|
|
|
|
|
|
|
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
|
2014-06-26 07:49:14 +00:00
|
|
|
end
|