active_model_normalizr 0.1.0 → 0.1.1

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: 8aeaaebc6e366c729ec0ae087777a35849f7130b
4
- data.tar.gz: 44c1ee6d82fb6014c4381938ca7f6998b08df399
3
+ metadata.gz: ad9dde5b9be3598bfcc29d0bc5747b6ce870d343
4
+ data.tar.gz: 741ede58843ada6f28643e0275af91a97db601aa
5
5
  SHA512:
6
- metadata.gz: 70905f2d4164f0cd054900f85cace10c56e0a7753b276b16ce34abf722572122c46b72aa54b3dbd61c6e5b75815b89c9eb72243b91345bbd6d07dd582a4fcc60
7
- data.tar.gz: 74c678ee9383e6fac6ca89a90469f2fed863bf69454d9386aa3d6f82295979d1dfb1ecd0caa8da18f3b6cc90832ca23d1c0f93985a363ed6cba556cbe50740c2
6
+ metadata.gz: 1298af9d90fd17b1c2376a5630dd874cc6c1e60a0e0d191ce1d869755abe0f70ee029522036d74e8640b78a0f6cb4dc237c585e74de93d0e069a77ae7bc3cd60
7
+ data.tar.gz: 41a674bb7beeb36330aa3eb476de2e53897a1cf1d69c325f00b1d0862037e6d6e418390c8997876672ff8e8a6994a87a875f19987b44cbb5fd7e44e455191a67
data/MIT-LICENSE CHANGED
@@ -1 +1,19 @@
1
- TODO
1
+ Copyright (c) 2016 by Worship Resource Media
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,2 +1,26 @@
1
- TODO
2
- ====
1
+ Active Model Normalizr
2
+ ======================
3
+
4
+ [Normalizr](https://github.com/paularmstrong/normalizr)-like serialization for [Active Model Serializers](https://github.com/rails-api/active_model_serializers).
5
+
6
+ Active Model Normalizr provides a custom [adapter](https://github.com/rails-api/active_model_serializers/blob/master/docs/general/adapters.md) for Active Model Serializers. So you can write your serializers like you always have and the output will be in a normalizr-like format that's easy to use with [Redux](http://redux.js.org/) or [Flux](https://facebook.github.io/flux/).
7
+
8
+ Installation
9
+ ------------
10
+
11
+ Add it to your `Gemfile`
12
+
13
+ ```ruby
14
+ gem 'active_model_normalizr'
15
+ ```
16
+
17
+ In a Rails initializer or somewhere similar:
18
+
19
+ ```ruby
20
+ ActiveModelSerializers.config.adapter = ActiveModelSerializers::Adapter::Normalizr
21
+ ```
22
+
23
+ Supported Dependency Versions
24
+ =============================
25
+
26
+ - `active_model_serializers ~> 0.10`
@@ -1,3 +1,3 @@
1
1
  module ActiveModelNormalizr
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -21,20 +21,42 @@ class ActiveModelSerializers::Adapter::Normalizr < ActiveModelSerializers::Adapt
21
21
  def entities(options = nil)
22
22
  h = {}
23
23
 
24
- objects = obj.respond_to?(:each) ? obj : Array(obj)
25
24
  serializers = serializer.respond_to?(:each) ? serializer : Array(serializer)
26
25
 
27
26
  serializers.each do |ser|
28
- key = ser.json_key.pluralize
29
- h[key] ||= {}
30
- h[key][ser.object.id] = ser.attributes.stringify_keys
27
+ # Main object(s) to be serialized
28
+ main_entity_name = ser.json_key.pluralize
29
+ main_entity_id = ser.object.id
31
30
 
31
+ h[main_entity_name] ||= {}
32
+ h[main_entity_name][main_entity_id] = ser.attributes.stringify_keys
33
+
34
+ # Associated objects
32
35
  ser.associations.each do |assoc|
33
- h[assoc.name.to_s.pluralize] ||= {}
36
+ entity_name = assoc.name.to_s
37
+ entities_name = entity_name.pluralize
38
+ entity_object = ser.object.send(assoc.name)
39
+ assoc_ids =
40
+ if entity_object.nil?
41
+ nil
42
+ elsif entity_object.respond_to?(:each)
43
+ entity_object.map(&:id)
44
+ else
45
+ entity_object.id
46
+ end
47
+
48
+ # Ensure that objects contain ids of their associated models
49
+ h[main_entity_name][main_entity_id][entity_name] = assoc_ids
50
+ h[entities_name] ||= {}
34
51
 
35
- ser.object.send(assoc.name).each do |assoc_entity|
36
- # TODO: use serializer here if possible
37
- h[assoc.name.to_s.pluralize][assoc_entity.id] = assoc_entity.attributes
52
+ if assoc.serializer.nil?
53
+ Array(assoc.options[:virtual_value]).each do |v_value|
54
+ h[entities_name][v_value['id']] = v_value.stringify_keys
55
+ end
56
+ else
57
+ Array(assoc.serializer).try(:each) do |assoc_ser|
58
+ h[entities_name][assoc_ser.object.id] = assoc_ser.as_json.stringify_keys
59
+ end
38
60
  end
39
61
  end
40
62
  end
@@ -42,27 +64,6 @@ class ActiveModelSerializers::Adapter::Normalizr < ActiveModelSerializers::Adapt
42
64
  h
43
65
  end
44
66
 
45
- # def entity_array
46
- # array = []
47
- #
48
- # if obj.respond_to? :to_a
49
- # array += obj.to_a
50
- # else
51
- # array << obj
52
- # end
53
- #
54
- # binding.pry
55
- # serializer.associations.each do |association|
56
- #
57
- # end
58
- #
59
- # array
60
- # end
61
- #
62
- # def entity_hash(model)
63
- # { model.id => model.attributes }
64
- # end
65
-
66
67
  def object_key(object)
67
68
  if object.is_a? ActiveRecord::Relation
68
69
  object.model.model_name.plural
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_normalizr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Crownoble
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-15 00:00:00.000000000 Z
11
+ date: 2016-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers