Module: SubjModels::ComprisingExternalId::ClassMethods

Defined in:
lib/subj_models/concerns/comprising_external_id.rb

Instance Method Summary collapse

Instance Method Details

#find_by_external_or_internal_id(id) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/subj_models/concerns/comprising_external_id.rb', line 13

def find_by_external_or_internal_id(id)
  if is_integer?(id)
    self.find(id)
  else
    self.find_by!(external_id: id)
  end
end

#is_integer?(value) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/subj_models/concerns/comprising_external_id.rb', line 21

def is_integer?(value)
  return true if value.is_a?(Integer)
  value.to_i.to_s == value
end

#parent_id_scope(parent, id) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/subj_models/concerns/comprising_external_id.rb', line 26

def parent_id_scope(parent, id)
  if id.is_a?(Array) || is_integer?(id)
    joins(parent.to_sym).where("#{parent.pluralize}.id = ?", id)
  else
    joins(parent.to_sym).where("#{parent.pluralize}.external_id = ?", id)
  end
end