Module: Shared::Identifiers::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#of_type(type_name) ⇒ Object



54
55
56
# File 'app/models/concerns/shared/identifiers.rb', line 54

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

#with_identifier(value) ⇒ Object

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’)



82
83
84
85
86
87
# File 'app/models/concerns/shared/identifiers.rb', line 82

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

#with_identifier_type_and_namespace_method(identifier_type, namespace_id, sorted = nil) ⇒ Object

!! Note that adding a sort also adds a where clause that constrains results to those that have numeric identifier.identifier

Parameters:



93
94
95
96
97
98
99
100
# File 'app/models/concerns/shared/identifiers.rb', line 93

def with_identifier_type_and_namespace_method(identifier_type, namespace_id, sorted = nil)
  return self.none if identifier_type.blank? && namespace_id.blank? && sorted.blank?
  q = nil
  q = with_identifier_type(identifier_type) if identifier_type.present?
  q = (!q.nil? ? q.with_identifier_namespace(namespace_id) :  with_identifier_namespace(namespace_id) ) if namespace_id.present?
  q = (!q.nil? ? q.with_identifiers_sorted(sorted) :  with_identifiers_sorted(sorted) ) if sorted.present?
  q
end

#with_namespaced_identifier(namespace_name, identifier) ⇒ Scope

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)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/concerns/shared/identifiers.rb', line 61

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