Class: Fae::Change

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ChangeConcern, Sortable
Defined in:
app/models/fae/change.rb

Class Method Summary collapse

Class Method Details

.current_userObject



18
19
20
# File 'app/models/fae/change.rb', line 18

def current_user
  Thread.current[:current_user]
end

.current_user=(user) ⇒ Object

writing current_user to thread for thread safety



14
15
16
# File 'app/models/fae/change.rb', line 14

def current_user=(user)
  Thread.current[:current_user] = user
end

.filter(params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/fae/change.rb', line 26

def filter(params)
  # build conditions if specific params are present
  conditions = {}
  conditions[:user_id] = params['user'] if params['user'].present?
  conditions[:changeable_type] = params['model'] if params['model'].present?
  conditions[:change_type] = params['type'] if params['type'].present?
  params['date'] ||= ''

  date_scope = []
  if params['start_date'].present? || params['end_date'].present?
    start_date = params['start_date'].present? ? CGI.unescape(params['start_date']).to_date : nil
    end_date   = params['end_date'].present? ? CGI.unescape(params['end_date']).to_date : nil
    date_scope = ['fae_changes.updated_at >= ?', start_date] if start_date.present?
    date_scope = ['fae_changes.updated_at <= ?', end_date] if end_date.present?
    date_scope = ['fae_changes.updated_at >= ? AND fae_changes.updated_at <= ?', start_date, end_date] if start_date.present? && end_date.present?
  else
    # normalize url param strings because "Last Hour" != "Last%20Hour"
    date_scope = case params['date'].downcase.gsub('%20', '-')
    when 'last-hour' then   ['fae_changes.updated_at >= ?', 60.minutes.ago]
    when 'last-day' then    ['fae_changes.updated_at >= ?', 1.day.ago]
    when 'last-week' then   ['fae_changes.updated_at >= ?', 1.week.ago]
    when 'last-month' then  ['fae_changes.updated_at >= ?', 1.month.ago]
    else
      []
    end
  end

  # use good 'ol MySQL to search if search param is present
  search = []
  if params['search'].present?
    search = ["fae_users.first_name LIKE ? OR fae_users.last_name LIKE ? OR fae_changes.updated_attributes LIKE ? OR fae_changes.changeable_type LIKE ? OR fae_changes.change_type LIKE ?", "%#{params['search']}%", "%#{params['search']}%", "%#{params['search']}%", "%#{params['search']}%", "%#{params['search']}%"]
  end

  # apply conditions and search from above to our scope
  order(id: :desc)
    .includes(:user).references(:user)
    .where(date_scope).where(conditions).where(search)
end

.unique_changeable_typesObject



22
23
24
# File 'app/models/fae/change.rb', line 22

def unique_changeable_types
  pluck(:changeable_type).uniq.sort.map{ |changeable_type| [changeable_type.gsub('Fae::',''), changeable_type] }
end