fields-serializer 0.1.1 → 0.2.0
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a605f2f9368ab70ff141b73c43fa45e61c8ce25a
|
4
|
+
data.tar.gz: de5eb5b39c3cf88e0a72fcb08afc85f97990cf67
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3888cff7c99bd8429ffa87c97020672d705c1c67125d77c87e1a0ac7f5b0f43185ebbd68f7b685ddec655136fba0e577a2d9b514ed1c63ecdf20655831a11c57
|
7
|
+
data.tar.gz: 4ef58173416352bb6a8f060d3662c54667043f3e9ac08213b441be9970531f562813ca57252f4e4c82b34476e5ffe268af59ae1044369677afb0826ff4aa5617
|
@@ -6,6 +6,13 @@ module Fields
|
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
8
|
class_methods do
|
9
|
+
# Convert a list of fields (json_api notation) in a list of associations to be
|
10
|
+
# added to a ActiveRecord Model.includes call
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
#
|
14
|
+
# BoilerPack.fields_to_includes("id,boiler.gas_safe_code") #=> ["boiler"]
|
15
|
+
#
|
9
16
|
def fields_to_includes(fields)
|
10
17
|
flatten_fields = Array(fields).map { |str| str.to_s.split(",").map(&:strip) }.flatten
|
11
18
|
nested_fields = flatten_fields.map { |field| nested_field(field.split(".")) }.compact
|
@@ -4,15 +4,16 @@ require "active_model_serializers"
|
|
4
4
|
# It can be used with any model but does not currently support associations.
|
5
5
|
#
|
6
6
|
# Example usage:
|
7
|
-
#
|
7
|
+
# render json: @region, serializer: FieldSerializer, fields: [:id, :title]
|
8
8
|
#
|
9
9
|
# > { "id": "5f19582d-ee28-4e89-9e3a-edc42a8b59e5", "title": "London" }
|
10
|
-
|
10
|
+
#
|
11
11
|
class FieldSerializer < ActiveModel::Serializer
|
12
12
|
def attributes(*args)
|
13
|
+
fields = Array(args.first).map { |str| str.to_s.split(",").map(&:strip) }.flatten
|
13
14
|
adding_id do
|
14
15
|
merging_attributes do
|
15
|
-
|
16
|
+
fields.map { |field| create_attribute_structure(field.split("."), object) }
|
16
17
|
end
|
17
18
|
end
|
18
19
|
end
|