Module: Queries::Concerns::Users

Extended by:
ActiveSupport::Concern
Included in:
DwcOccurrence::Filter, Query::Filter
Defined in:
lib/queries/concerns/users.rb

Overview

Helpers for queries that reference created/updated fields

!! You must have ‘#base_query` defined in the module to use this concern !! You must call set_user_dates in initialize()

  • TODO: Isolate code to a gem

  • TODO: validate User/Project relationships

Concern specs are in

spec/lib/queries/person/filter_spec.rb
spec/lib/queries/collection_object/filter_spec.rb (housekeeping extensions)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.and_clausesObject



84
85
86
# File 'lib/queries/concerns/users.rb', line 84

def self.and_clauses
  [ :updated_since_facet ]
end

.merge_clausesObject



80
81
82
# File 'lib/queries/concerns/users.rb', line 80

def self.merge_clauses
  [ :created_updated_facet ]
end

.paramsObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/queries/concerns/users.rb', line 15

def self.params
  [
    :user_id,
    :user_target,
    :user_date_start,
    :user_date_end,
    :updated_since,
    :extend_housekeeping,
    user_id: [],
  ]
end

Instance Method Details

#created_updated_facet(target: base_query, disable_extension: false) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/queries/concerns/users.rb', line 93

def created_updated_facet(target: base_query, disable_extension: false)
  return nil if user_id.empty? && user_target.nil? && user_date_start.nil? && user_date_end.nil?

  if !user_date_start.nil? || !user_date_end.nil?
    q = time_scope(target:)
  else
    q = target || base_query
  end

 if !user_id.empty?
   q = user_scope(target: q)
 end

 if !disable_extension && extend_housekeeping && !housekeeping_extensions.empty?
   q = referenced_klass_union( [q] + housekeeping_extensions )
 end

  q
end

#housekeeping_extension_query(target: nil, joins: []) ⇒ Object

Parameters:

  • target (the joined class) (defaults to: nil)
  • joins (Array) (defaults to: [])

    to add to join()



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/queries/concerns/users.rb', line 120

def housekeeping_extension_query(target: nil, joins: [])
  raise 'no target' if target.nil?

  id_name = "hkx_#{target.name.downcase}_id"
  query_name = "query_#{target.name}_hkx"
  tbl = table.name

  b = created_updated_facet(
    target: target.all,
    disable_extension: true
  ).select("id #{id_name}")

  q = referenced_klass
  q = q.joins(joins) if !joins.empty?
  q = q.joins("JOIN #{query_name} as #{query_name}1 on #{query_name}1.#{id_name} = #{target.arel_table.name}.id")

  s = "WITH #{query_name} AS (" + b.to_sql + ') ' + q.to_sql

  referenced_klass.from('(' + s + ") as #{tbl}").distinct
end

#housekeeping_extensionsObject

Defined in Filters



114
115
116
# File 'lib/queries/concerns/users.rb', line 114

def housekeeping_extensions
  []
end

#set_user_dates(params) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/queries/concerns/users.rb', line 71

def set_user_dates(params)
  @extend_housekeeping = boolean_param(params, :extend_housekeeping)
  @updated_since = params[:updated_since]
  @user_date_end = params[:user_date_end]
  @user_date_start = params[:user_date_start]
  @user_id = params[:user_id]
  @user_target = params[:user_target] # Add validation ?
end

#time_scope(target: base_query) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/queries/concerns/users.rb', line 141

def time_scope(target: base_query)
  return nil if user_date_start.nil? && user_date_end.nil?

  s, e = Utilities::Dates.normalize_and_order_dates(
    user_date_start,
    user_date_end)

  # TODO: this is ultimately going to require hourly scope
  s += ' 00:00:00' # adjust dates to beginning
  e += ' 23:59:59' # and end of date days

  q = nil

  # handle date range

  # What date?
  if !user_date_start.nil? || !user_date_end.nil?
    case user_target
    when 'updated'
      q = target.updated_in_date_range(s, e)
    when 'created'
      q = target.created_in_date_range(s, e)
    else
      # TODO: UNION !!!
      q = target.updated_in_date_range(s, e).or(target.created_in_date_range(s,e))
    end
  end

  q
end

#updated_since_facetObject



88
89
90
91
# File 'lib/queries/concerns/users.rb', line 88

def updated_since_facet
  return nil if updated_since.blank?
  table[:updated_at].gteq(updated_since)
end

#user_scope(target: base_user) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/queries/concerns/users.rb', line 172

def user_scope(target: base_user)
  return nil if user_id.empty? && user_target.nil?

  q = target

  # handle user_id
  if !user_id.empty?
    case user_target
    when 'updated'
      q = q.where(updated_by_id: user_id)
    when 'created'
      q = q.where(created_by_id: user_id)
    else
      # TODO: UNION
      q = q.where(created_by_id: user_id).or(q.where(updated_by_id: user_id))
    end
  end
  q
end