0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 15:18:34 +08:00
discourse/app/models/group_history.rb
Jarek Radosz 8baf4d5d4c
DEV: Enable Style/RedundantSelf rubocop rule (#40098)
(to be enabled in the shared config)
2026-05-19 19:27:45 +02:00

71 lines
2 KiB
Ruby
Vendored

# frozen_string_literal: true
class GroupHistory < ActiveRecord::Base
belongs_to :group
belongs_to :acting_user, class_name: "User"
belongs_to :target_user, class_name: "User"
validates :acting_user_id, presence: true
validates :group_id, presence: true
validates :action, presence: true
def self.actions
@actions ||=
Enum.new(
change_group_setting: 1,
add_user_to_group: 2,
remove_user_from_group: 3,
make_user_group_owner: 4,
remove_user_as_group_owner: 5,
)
end
def self.filters
%i[acting_user target_user action subject]
end
def self.with_filters(group, params = {})
records =
includes(:acting_user, :target_user).where(group_id: group.id).order(
"group_histories.created_at DESC",
)
if params.present?
params = params.slice(*filters)
records = records.where(action: actions[params[:action].to_sym]) if params[:action].present?
records = records.where(subject: params[:subject]) if params[:subject].present?
%i[acting_user target_user].each do |filter|
if params[filter].present?
id = User.where(username_lower: params[filter]).pluck(:id)
records = records.where("#{filter}_id" => id)
end
end
end
records
end
end
# == Schema Information
#
# Table name: group_histories
#
# id :integer not null, primary key
# action :integer not null
# new_value :text
# prev_value :text
# subject :string
# created_at :datetime not null
# updated_at :datetime not null
# acting_user_id :integer not null
# group_id :integer not null
# target_user_id :integer
#
# Indexes
#
# index_group_histories_on_acting_user_id (acting_user_id)
# index_group_histories_on_action (action)
# index_group_histories_on_group_id (group_id)
# index_group_histories_on_target_user_id (target_user_id)
#