Class: Audiences::Criterion

Inherits:
ApplicationRecord show all
Defined in:
app/models/audiences/criterion.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.map(criteria) ⇒ Array<Criterion>

Maps an array of attribute hashes to Criterion objects.

Each attribute hash should have a :groups key, whose value is a hash mapping resource types (e.g., “Departments”, “Territories”) to arrays of group hashes, each containing an :id key (the SCIM group ID).

Example input:

[
  { groups: { Departments: [{ id: "1" }] } },
  { groups: { Territories: [{ id: "2" }], Departments: [{ id: "3" }] } },
]

Returns an array of new Criterion objects, each initialized with the corresponding group criterion.

Parameters:

  • criteria (Array<Hash>)

    Array of attribute hashes describing groups

Returns:

  • (Array<Criterion>)

    Array of Criterion objects



32
33
34
35
36
37
38
39
# File 'app/models/audiences/criterion.rb', line 32

def self.map(criteria)
  Array(criteria).map do |attrs|
    groups = attrs["groups"]&.flat_map do |resource_type, scim_groups|
      Audiences::Group.from_scim(resource_type, *scim_groups).to_a
    end
    new(groups: groups)
  end
end

Instance Method Details

#as_jsonObject



41
42
43
44
45
# File 'app/models/audiences/criterion.rb', line 41

def as_json(...)
  groups = self.groups.group_by(&:resource_type)

  { id: id, count: count, groups: groups }.as_json(...)
end

#usersObject



47
48
49
50
# File 'app/models/audiences/criterion.rb', line 47

def users
  Audiences::ExternalUser.matching(self)
                         .instance_exec(&Audiences.default_users_scope)
end