Class: SupplejackApi::ConceptSerializer

Inherits:
ActiveModel::Serializer
  • Object
show all
Defined in:
app/serializers/supplejack_api/concept_serializer.rb

Instance Method Summary collapse

Instance Method Details

#field_value(field) ⇒ Object


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/serializers/supplejack_api/concept_serializer.rb', line 84

def field_value(field)
  value = if ConceptSchema.fields[field].try(:search_value) && ConceptSchema.fields[field].try(:store) == false
            ConceptSchema.fields[field].search_value.call(object)
          else
            object.public_send(field)
          end

  value = ConceptSchema.fields[field].try(:default_value) if value.nil? rescue nil

  if ConceptSchema.fields[field].try(:date_format)
    value = format_date(value, ConceptSchema.fields[field].try(:date_format))
  end

  value
end

#include_context_fields!(hash) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/serializers/supplejack_api/concept_serializer.rb', line 49

def include_context_fields!(hash)
  fields = hash.dup
  fields.keep_if { |field| ConceptSchema.model_fields.include?(field) }
  field_keys = fields.keys

  # Include unstored fields from the Schema
  record_fields = ConceptSchema.model_fields.select { |_key, value| value.try(:store) == false }
  field_keys += record_fields.keys

  hash['@context'] = if options[:inline_context]
                       Concept.build_context(field_keys)
                     else
                       object.context
                     end
  hash
end

#include_individual_fields!(hash) ⇒ Object


66
67
68
69
70
71
72
73
74
75
# File 'app/serializers/supplejack_api/concept_serializer.rb', line 66

def include_individual_fields!(hash)
  if options[:fields].present?
    options[:fields].push(:concept_id)
    options[:fields].sort!
    options[:fields].each do |field|
      hash[field] = object.send(field)
    end
  end
  hash
end

#include_reverse_fields!(hash) ⇒ Object


77
78
79
80
81
82
# File 'app/serializers/supplejack_api/concept_serializer.rb', line 77

def include_reverse_fields!(hash)
  hash['@reverse'] = {}
  key = concept.edm_type
  include!(:records, node: hash['@reverse'], key: key)
  hash
end

#serializable_hashObject


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/serializers/supplejack_api/concept_serializer.rb', line 21

def serializable_hash
  hash = attributes
  # Create empty context block to add to once we know what fields we have.
  hash['@context'] = {}
  hash['@type'] = object.concept_type
  hash['@id'] = object.site_id

  if default? || verbose?
    groups = (options[:groups] & ConceptSchema.groups.keys) || []
    fields = Set.new

    groups.each do |group|
      fields.merge(ConceptSchema.groups[group].try(:fields)) rescue {}
    end

    fields.each do |field|
      # Doesn't allow the real record id to be overwritten by mongo id
      hash[field] = field == :id ? hash[field] : field_value(field, options)
    end
  end

  include_individual_fields!(hash)
  include_context_fields!(hash)
  include_reverse_fields!(hash) if reverse?
  include!(:source_authorities, node: hash) if source_authorities?
  hash
end