2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
class UsersStarProject < ApplicationRecord
|
2019-10-12 21:52:04 +05:30
|
|
|
include Sortable
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
belongs_to :project
|
2014-09-02 18:07:02 +05:30
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
validates :user, presence: true
|
|
|
|
validates :user_id, uniqueness: { scope: [:project_id] }
|
|
|
|
validates :project, presence: true
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
alias_attribute :starred_since, :created_at
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
after_create :increment_project_star_count
|
|
|
|
after_destroy :decrement_project_star_count
|
|
|
|
|
|
|
|
scope :with_active_user, -> { joins(:user).merge(User.with_state(:active)) }
|
2019-10-12 21:52:04 +05:30
|
|
|
scope :order_user_name_asc, -> { joins(:user).merge(User.order_name_asc) }
|
|
|
|
scope :order_user_name_desc, -> { joins(:user).merge(User.order_name_desc) }
|
|
|
|
scope :by_project, -> (project) { where(project_id: project.id) }
|
|
|
|
scope :with_visible_profile, -> (user) { joins(:user).merge(User.with_visible_profile(user)) }
|
|
|
|
scope :with_public_profile, -> { joins(:user).merge(User.with_public_profile) }
|
2019-12-04 20:38:33 +05:30
|
|
|
scope :preload_users, -> { preload(:user) }
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
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-04-04 11:22:00 +05:30
|
|
|
joins(:user).merge(User.search(query, use_minimum_char_limit: false))
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def increment_project_star_count
|
|
|
|
Project.update_counters(project, star_count: 1) if user.active?
|
|
|
|
end
|
|
|
|
|
|
|
|
def decrement_project_star_count
|
|
|
|
Project.update_counters(project, star_count: -1) if user.active?
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|