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: c7f7c4357c186555345ddae7a13f73059ec571a1
4
- data.tar.gz: d0328fbeb7133eff87fc348e4f6ccb74061c773b
3
+ metadata.gz: a605f2f9368ab70ff141b73c43fa45e61c8ce25a
4
+ data.tar.gz: de5eb5b39c3cf88e0a72fcb08afc85f97990cf67
5
5
  SHA512:
6
- metadata.gz: 10e3e27e1f3e4dfef757cd951227451132d9e857f5a1803b083ea82e7fa8f22c57e41704e1ec37f806dd3f541217e405eae281de0df1ec4facd7d61a2aac6a77
7
- data.tar.gz: a375388498a96f33cb998ecefc6100112327f244a626c1f0daec1cbc47638eb04a457476684fd08e08d9087566dc58a0b338fa0d2636a512d2568216e0397cce
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
- # `render json: @region, serializer: FieldSerializer, fields: [:id, :title]`
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
- args.first.map { |field| create_attribute_structure(field.split("."), object) }
16
+ fields.map { |field| create_attribute_structure(field.split("."), object) }
16
17
  end
17
18
  end
18
19
  end
@@ -1,5 +1,5 @@
1
1
  module Fields
2
2
  module Serializer
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.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.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stuart Chinery