fields-serializer 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fields/serializer/fields_tree.rb +20 -1
- data/lib/fields/serializer/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4eb7b537453470e5785c408759a7cefd465931b
|
4
|
+
data.tar.gz: 6aeab9610fdc44c06d6555f7a2a2f32d0ad3f7ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 11d02a736631a8580b55a550718a70315354f48a68a3afa742db813145da57e179c62a376d6ea53095e8b8829437cd580d03e93f7386e42c96cebd48852d4b53
|
7
|
+
data.tar.gz: 04c6ea5ce89247bb960c760f1efff1eb4713f719ef2dd53f629571bc4a1e26663cd640afe07a769659a8b285017e81ce4b26fed0ae4231496311456f82a5cff1
|
@@ -4,6 +4,7 @@ require "active_model_serializers"
|
|
4
4
|
module Fields
|
5
5
|
module Serializer
|
6
6
|
|
7
|
+
# A class to store a tree structure of a model klass, its attributes and associations.
|
7
8
|
class FieldsTree
|
8
9
|
attr_reader :klass, :fields, :associations
|
9
10
|
|
@@ -17,6 +18,14 @@ module Fields
|
|
17
18
|
self if fields.present? || associations.present?
|
18
19
|
end
|
19
20
|
|
21
|
+
# Adds a new field (json api notation) to the tree structure:
|
22
|
+
#
|
23
|
+
# user_tree.notation
|
24
|
+
# #=> [:name, :surname, { subjects: [:title, { posts: { comments: :count } }], followers: :nickname }]
|
25
|
+
#
|
26
|
+
# user_tree.merge!("subjects.posts.date").notation
|
27
|
+
# #=> [:name, :surname, { subjects: [:title, { posts: [{ comments: :count }, :date] }], followers: :nickname }]
|
28
|
+
#
|
20
29
|
def merge!(join_field)
|
21
30
|
return self unless join_field.present?
|
22
31
|
parent, rest = join_field.to_s.split(".", 2)
|
@@ -28,6 +37,11 @@ module Fields
|
|
28
37
|
self
|
29
38
|
end
|
30
39
|
|
40
|
+
# Return the tree structure in Rails includes notation including both associations and fields
|
41
|
+
#
|
42
|
+
# user_tree.notation
|
43
|
+
# #=> [:name, :surname, { subjects: [:title, { posts: :comments }], followers: :nickname }]
|
44
|
+
#
|
31
45
|
def notation
|
32
46
|
if fields.present?
|
33
47
|
if associations.present?
|
@@ -39,7 +53,12 @@ module Fields
|
|
39
53
|
associations_to_notation.presence
|
40
54
|
end
|
41
55
|
end
|
42
|
-
|
56
|
+
|
57
|
+
# Return the tree structure in Rails includes notation including only associations
|
58
|
+
#
|
59
|
+
# user_tree.notation
|
60
|
+
# #=> [{ subjects: { posts: :comments }}, :followers]
|
61
|
+
#
|
43
62
|
def to_includes
|
44
63
|
to_includes = associations.inject([]) do |result, (k, v)|
|
45
64
|
v_includes = v.to_includes
|