Module: Shared::Identifiers::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#of_type(type_name) ⇒ Object



37
38
39
# File 'app/models/concerns/shared/identifiers.rb', line 37

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



65
66
67
68
69
70
# File 'app/models/concerns/shared/identifiers.rb', line 65

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:



76
77
78
79
80
81
82
83
# File 'app/models/concerns/shared/identifiers.rb', line 76

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)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/concerns/shared/identifiers.rb', line 44

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