Module: Shared::Identifiable::ClassMethods

Defined in:
app/models/concerns/shared/identifiable.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) of_type(type_name)



14
15
16
# File 'app/models/concerns/shared/identifiable.rb', line 14

def of_type(type_name)
  where(type: type_name)
end

- (Object) with_identifier(value)

Exact match on the full identifier (use for any class of identifiers) 'value' is the cached value with includes the namespace (see spec) example Serial.with_identifier('MX serial ID 8740')



42
43
44
45
46
47
# File 'app/models/concerns/shared/identifiable.rb', line 42

def with_identifier(value)
  value = [value] if value.class == String
  t = Identifier.arel_table
  a = t[:cached].eq_any(value)
  self.joins(:identifiers).where(a.to_sql).references(:identifiers)
end

- (Scope) with_namespaced_identifier(namespace_name, identifier)

Exact match on identifier + namespace, return an Array, not Arel

Parameters:

  • namespace_name (String, String)

    is either the long or short namespace name.

Returns:

  • (Scope)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/concerns/shared/identifiable.rb', line 21

def with_namespaced_identifier(namespace_name, identifier)
  i = Identifier.arel_table
  n = Namespace.arel_table
  s = self.arel_table

  # conditions
  c1 = n[:name].eq(namespace_name).or(n[:short_name].eq(namespace_name))
  c2 = i[:identifier].eq(identifier)

  # join identifiers to namespaces
  j = i.join(n).on(i[:namespace_id].eq(n[:id]))

  # join self to identifiers
  l = s.join(i).on(s[:id].eq(i[:identifier_object_id]).and(i[:identifier_object_type].eq(self.base_class.name)))

  self.joins(l.join_sources, j.join_sources).where(c1.and(c2).to_sql)
end