Class: GroupHistory

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/group_history.rb

Class Method Summary collapse

Class Method Details

.actionsObject



12
13
14
15
16
17
18
19
20
21
# File 'app/models/group_history.rb', line 12

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

.filtersObject



23
24
25
# File 'app/models/group_history.rb', line 23

def self.filters
  %i[acting_user target_user action subject]
end

.with_filters(group, params = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/group_history.rb', line 27

def self.with_filters(group, params = {})
  records =
    self
      .includes(:acting_user, :target_user)
      .where(group_id: group.id)
      .order("group_histories.created_at DESC")

  if !params.blank?
    params = params.slice(*filters)
    records = records.where(action: self.actions[params[:action].to_sym]) unless params[
      :action
    ].blank?
    records = records.where(subject: params[:subject]) unless params[:subject].blank?

    %i[acting_user target_user].each do |filter|
      unless params[filter].blank?
        id = User.where(username_lower: params[filter]).pluck(:id)
        records = records.where("#{filter}_id" => id)
      end
    end
  end

  records
end