Module: JSONAPI::Consumer::Resource::SerializerConcern

Extended by:
ActiveSupport::Concern
Included in:
JSONAPI::Consumer::Resource
Defined in:
lib/jsonapi/consumer/resource/serializer_concern.rb

Instance Method Summary collapse

Instance Method Details



34
35
36
37
38
39
40
41
42
43
# File 'lib/jsonapi/consumer/resource/serializer_concern.rb', line 34

def add_link(name, association, options)
  return if association.nil?

  @hash[:links][name] = case association.class
                        when String, Integer
                          association
                        else
                          association.to_param
                        end
end


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jsonapi/consumer/resource/serializer_concern.rb', line 22

def add_links(name, association, options)
  @hash[:links][name] ||= []
  @hash[:links][name] += (association || []).map do |obj|
    case obj.class
    when String, Integer
      obj
    else
      obj.to_param
    end
  end
end

#serializable_hash(options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/jsonapi/consumer/resource/serializer_concern.rb', line 5

def serializable_hash(options={})
  @hash = persisted? ? attributes : attributes.except(self.class.primary_key)

  self.each_association do |name, association, options|
    @hash[:links] ||= {}

    if association.respond_to?(:each) or _association_type(name) == :has_many
      add_links(name, association, options)
    else
      add_link(name, association, options)
    end
    @hash.delete(:links) if remove_links?
  end

  @hash
end

#to_json(options = {}) ⇒ Object



45
46
47
# File 'lib/jsonapi/consumer/resource/serializer_concern.rb', line 45

def to_json(options={})
  serializable_hash(options).to_json
end