Welcome to mirror list, hosted at ThFree Co, Russian Federation.

exportable.rb « concerns « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 066a44912bef675b7448e000cada70abf15ae066 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
# frozen_string_literal: true

module Exportable
  extend ActiveSupport::Concern

  def readable_records(association, current_user: nil)
    association_records = try(association)
    return unless association_records.present?

    if has_many_association?(association)
      DeclarativePolicy.user_scope do
        association_records.select { |record| readable_record?(record, current_user) }
      end
    else
      readable_record?(association_records, current_user) ? association_records : nil
    end
  end

  def exportable_association?(association, current_user: nil)
    return false unless respond_to?(association)
    return true if has_many_association?(association)

    readable = try(association)
    return true if readable.nil?

    readable_record?(readable, current_user)
  end

  def restricted_associations(keys)
    exportable_restricted_associations & keys
  end

  def has_many_association?(association_name)
    self.class.reflect_on_association(association_name)&.macro == :has_many
  end

  private

  def exportable_restricted_associations
    []
  end

  def readable_record?(record, user)
    if record.respond_to?(:exportable_record?)
      record.exportable_record?(user)
    else
      record.readable_by?(user)
    end
  end
end