fields-serializer 0.5.7 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 70ce934abbc675e29da522a1ddbc1555131a770f
4
- data.tar.gz: 3903d8c033022f782590cadbb97ed54ceb060125
3
+ metadata.gz: 18a49cde603d26bd1720c5efc9a626d89460debd
4
+ data.tar.gz: ee1496bfa7c568d657e280678c505705d3ecae1e
5
5
  SHA512:
6
- metadata.gz: 73aa0314cb12c01f25a818d9213aedb71f118e570b4408a9e83b86cbe4d7538c4cd92364f95ede8a3444fc64ceb72088e1d6a6c5f66e7ebdaf5aab63f9372325
7
- data.tar.gz: 0a182f99c417db1c5c56ff2d5640bff2307c53a343d360a23ae97826102484c8d4a084e4310ae371c96baac1075e30ffbc173ab845d3a63ba53c3ad78c2ff4c9
6
+ metadata.gz: 0f437546ad70fe765da4d1e8e4cdef5be4cbd1d42d91f3b7621428927b656852b90f2e322e607ced987190a04004d14334ef15fa4ba4a2daa8896d3259be71e9
7
+ data.tar.gz: 47fc5594fe431a1855f6a388041bcc33c3e832eb2cfcc9b3de054501fcc0acaa9a583f428eea3ab10d51c8af7daba0c2db33eed12776d67f4ce8fa8d239db3d6
@@ -8,8 +8,9 @@ module Fields
8
8
 
9
9
  class_methods do
10
10
 
11
+ # If key is an association of a given model class
11
12
  def association?(key)
12
- reflections.keys.include?(key)
13
+ reflections.keys.include?(key.to_s)
13
14
  end
14
15
 
15
16
  # Convert a list of fields (json_api notation) in a list of associations to be
@@ -23,14 +24,32 @@ module Fields
23
24
  fields_to_tree(fields).to_includes
24
25
  end
25
26
 
27
+ # Creates new anonymous ActiveModel::Serializer subclass from fields in json api notation:
28
+ #
29
+ # Class.new do
30
+ # attribute :id
31
+ # attribute :non_association_field_1
32
+ # attribute :non_association_field_2
33
+ # attribute :non_association_field_3
34
+ # attribute :non_association_field_4
35
+ # ...
36
+ #
37
+ # has_one :association_field_1, serializer: new_serializer_class_for_association_field_1_nested_fields
38
+ # or
39
+ # belongs_to :association_field_2, serializer: new_serializer_class_for_association_field_2_nested_fields
40
+ # or
41
+ # has_many :association_field_3, serializer: new_serializer_class_for_association_field_3_nested_fields
42
+ # end ...
26
43
  def fields_serializer(*fields)
27
44
  create_serializer_class(fields_to_tree(fields).notation)
28
45
  end
29
46
 
47
+ # Creates new anonymous ActiveModel::Serializer subclass from fields in Rails includes notation
30
48
  def create_serializer_class(fields)
31
49
  klass = self
32
50
  Class.new(ActiveModel::Serializer).tap do |new_class|
33
51
  new_class.class_eval do
52
+ attribute :id
34
53
  Array.wrap(fields).each do |field|
35
54
  if field.kind_of?(Hash)
36
55
  field.each do |association_name, nested_fields|
@@ -51,20 +70,6 @@ module Fields
51
70
  array_fields(fields.flatten).inject(FieldsTree.new(self), &:merge!)
52
71
  end
53
72
 
54
- # Calls:
55
- # has_one :user, serializer: new_serializer_class_for_user_fields
56
- # or
57
- # belongs_to :user, serializer: new_serializer_class_for_user_fields
58
- # or
59
- # has_many :users, serializer: new_serializer_class_for_user_fields
60
- #
61
- def nested_association(fields)
62
- fields.each do |association_name, nested_fields|
63
- reflection = reflections[association_name]
64
- send(reflection.macro, association_name.to_sym, serializer: reflection.klass.create_serializer_class(nested_fields))
65
- end
66
- end
67
-
68
73
  def array_fields(fields)
69
74
  Array.wrap(fields).map { |str| str.to_s.split(",").map(&:strip) }.flatten.sort
70
75
  end
@@ -1,5 +1,5 @@
1
1
  module Fields
2
2
  module Serializer
3
- VERSION = "0.5.7"
3
+ VERSION = "0.6.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fields-serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.7
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stuart Chinery