Class: ProjectAuthorization

Inherits:
ApplicationRecord show all
Includes:
FromUnion
Defined in:
app/models/project_authorization.rb

Class Method Summary collapse

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.insert_authorizations(rows, per_batch = 1000) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/project_authorization.rb', line 19

def self.insert_authorizations(rows, per_batch = 1000)
  rows.each_slice(per_batch) do |slice|
    tuples = slice.map do |tuple|
      tuple.map { |value| connection.quote(value) }
    end

    connection.execute <<-EOF.strip_heredoc
    INSERT INTO project_authorizations (user_id, project_id, access_level)
    VALUES #{tuples.map { |tuple| "(#{tuple.join(', ')})" }.join(', ')}
    EOF
  end
end

.select_from_union(relations) ⇒ Object


13
14
15
16
17
# File 'app/models/project_authorization.rb', line 13

def self.select_from_union(relations)
  from_union(relations)
    .select(['project_id', 'MAX(access_level) AS access_level'])
    .group(:project_id)
end