ams_hal 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 77898f307f89b2c0e0817b60c5837161cbdb387d
4
- data.tar.gz: 29abc151fe345ab914679e56e5dda53b3d77352d
3
+ metadata.gz: 7ae5d537e7726b7aa36721d90fe6e39156b9bf82
4
+ data.tar.gz: 64a517fb6e9d71e1ef16270a2f8660cb7e113fcb
5
5
  SHA512:
6
- metadata.gz: 4ebacd405a3634d4a3a9a1379e7efb20f446a6b313788f3735758675b659d2e8824cc45b02c9c4131faf2a7004f03000e4468b078a5d0ae0692e27742b57e00f
7
- data.tar.gz: 768689cfd85cc23962255f5a00cd959d8f5df3a7212158c11c89064ffa098f53bd1303251510150271ee9708696092b35962be1b1f805985f3b4ddb15cfbad1a
6
+ metadata.gz: 17a14cc150b84c8a9aa63a7c5be4cdefa286daee74ea6ff66223f160d0ed9ca751cd0d19b8a7785947a8abc27e1326ba7e4914bad698861cbbadd5a1631cd05a
7
+ data.tar.gz: cf7033fb8efdc620b498023d16eb1fe1005029404363b0cfad3e9c4d8ae4dd5ca7d588e79a258e692461b877fddaa4905ac028fb46da9732197529635bee606c
@@ -1,38 +1,57 @@
1
1
  module AmsHal
2
2
  class Adapter < ActiveModelSerializers::Adapter::Base
3
- def serializable_hash(options = nil)
3
+ def serializable_hash(options = {})
4
4
  options = serialization_options(options)
5
5
  options[:fields] ||= instance_options[:fields]
6
- serialized = if serializer.respond_to?(:each)
7
- serializer.each_with_object([]) do |_serializer, array|
8
- array << serialize_resource(_serializer, instance_options, options)
9
- end
6
+ hash = if serializer.respond_to?(:each)
7
+ serialize_collection(serializer, instance_options, options)
10
8
  else
11
9
  serialize_resource(serializer, instance_options, options)
12
10
  end
13
11
 
14
- self.class.transform_key_casing!(serialized, instance_options)
12
+ self.class.transform_key_casing!(hash, instance_options)
15
13
  end
16
14
 
17
15
  protected
18
16
 
19
17
  def serialize_resource(serializer, adapter_options, options)
18
+ skip_embedded = options.delete(:skip_embedded) || false
19
+
20
20
  options[:include_directive] = {} # Don't include associations as attributes
21
- serialized = serializer.serializable_hash(adapter_options, options, self)
21
+ hash = serializer.serializable_hash(adapter_options, options, self)
22
22
 
23
23
  if links = serialize_links(serializer)
24
- serialized[:_links] = links if links.any?
24
+ hash[:_links] = links if links.any?
25
25
  end
26
26
 
27
+ return hash if skip_embedded
28
+
27
29
  if embedded = serialize_embedded(serializer)
28
- serialized[:_embedded] = embedded if embedded.any?
30
+ hash[:_embedded] = embedded if embedded.any?
31
+ end
32
+
33
+ if associations = serialize_associations(serializer, adapter_options)
34
+ hash[:_embedded] = associations if associations.any?
35
+ end
36
+
37
+ hash
38
+ end
39
+
40
+ def serialize_collection(serializer, adapter_options, options)
41
+ options[:include_directive] = {} # Don't include associations as attributes
42
+ hash = {}
43
+
44
+ if links = serialize_links(serializer)
45
+ hash[:_links] = links if links.any?
29
46
  end
30
47
 
31
- if associations = serialize_associations(serializer)
32
- serialized[:_embedded] = associations if associations.any?
48
+ embedded = serializer.map do |_serializer|
49
+ options[:skip_embedded] = true
50
+ serialize_resource(_serializer, instance_options, options)
33
51
  end
52
+ hash[:_embedded] = embedded if embedded.any?
34
53
 
35
- serialized
54
+ hash
36
55
  end
37
56
 
38
57
  def serialize_links(serializer)
@@ -85,34 +104,24 @@ module AmsHal
85
104
  end
86
105
  end
87
106
 
88
- def serialize_associations(serializer)
107
+ def serialize_associations(serializer, adapter_options)
89
108
  serializer.associations.each_with_object({}) do |association, embedded|
90
- object = serializer.object
91
- if object&.respond_to? association.name
92
- resource = object.public_send(association.name)
93
- else
94
- puts "WARN: Failed to get '#{association.name}' association from resource (#{object})"
95
- end
96
-
97
- next unless resource
98
-
99
- if association.serializer.is_a? ActiveModel::Serializer::CollectionSerializer
100
- # Not sure if this could happen and perhaps we should fail instead of create an array??
101
- resource = [resource] unless resource.respond_to? :each
102
-
103
- embedded[association.name] = resource.map do |resrc|
104
- # FIXME: How to improve this?
105
- serialize_embedded_resource(
106
- resrc,
107
- serializer: association.serializer.send(:options)[:serializer]
108
- )
109
+ embedded[association.name] =
110
+ if association.serializer.nil? || association.serializer.object.nil?
111
+ # active_model_serializers <= 0.10.4
112
+ object = serializer.object
113
+ resource = object.public_send(association.name)
114
+ serialized = [resource].flatten.map do |resrc|
115
+ serialize_embedded_resource(resrc, serializer: association.serializer&.class)
116
+ end
117
+ serialized.size == 1 ? serialized.first : serialized
118
+ elsif association.serializer.respond_to? :each
119
+ association.serializer.map do |_serializer|
120
+ serialize_resource(_serializer, instance_options, {skip_embedded: true})
121
+ end
122
+ else
123
+ serialize_resource(association.serializer, instance_options, {skip_embedded: true})
109
124
  end
110
- else
111
- embedded[association.name] = serialize_embedded_resource(
112
- resource,
113
- serializer: association.serializer&.class
114
- )
115
- end
116
125
  end
117
126
  end
118
127
 
@@ -4,9 +4,10 @@ module AmsHal
4
4
  class Embed
5
5
  include ActiveModelSerializers::SerializationContext::UrlHelpers
6
6
 
7
- attr_reader :name, :options
7
+ attr_reader :serializer, :name, :options
8
8
 
9
9
  def initialize(serializer, name, options = {}, &block)
10
+ @serializer = serializer
10
11
  @name = name
11
12
  @options = options
12
13
  @block = block
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ams_hal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sammy Henningsson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-10 00:00:00.000000000 Z
11
+ date: 2017-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers