dry_serialization 0.4.3.2 → 0.4.4.3

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
  SHA256:
3
- metadata.gz: 5d7c4bec9452452abc391dabd00b7deccee9b2d8ea982cc4cfcdc094f61814cd
4
- data.tar.gz: 3aa07db6818832c84bd3321e95f5f3dba4e2302faa5523808becc52e86d4fe9b
3
+ metadata.gz: 32515881ad6106083773deb7252ed369d179aa499c8955760459c29e740da85c
4
+ data.tar.gz: b3bfffa4bed91ee6abc05edc66f45d07cee9a56b0865a143c7e5e24880ae9756
5
5
  SHA512:
6
- metadata.gz: 743725f638b0c868057b26100b7b59affb0cfd61cb6846728ceed417703a338a0ddee470fb72fa9b9a3548621ef43a11df7113514e01228cbc58d9c6ebdcb600
7
- data.tar.gz: b9aea7143976de05280cc31effa33a41b91a5c1d135296f2352c08dc63127ab83b4dd05ab9a7d9f3fd6e76d10dca0b5e85f5f329770f9d176025c1721d1c0d82
6
+ metadata.gz: e23aa1a8f4a0b4d27b4c8d2f664c8e149421afa0e4cc2dacd7198cb38f2c43863affe03bb75fe13dfab715b212920facaa42ae4c8fcfe71ef51dd7f8bc8a6db2
7
+ data.tar.gz: daa8252dcdd1f0aec8e2acc0e8ed61beed8b4f6999cfe20c5de643c52360111793a0946335359e59411f81fcf667f05086556174c334af3939726c466f072cb7
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # DrySerialization
2
2
 
3
- ![AppVeyor](https://img.shields.io/appveyor/build/mikeyduece/dry_serialization?style=plastic)
4
- ![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/mikeyduece/dry_serialization?style=plastic)
3
+ [![Gem Version](https://badge.fury.io/rb/dry_serialization.svg)](https://badge.fury.io/rb/dry_serialization)
5
4
  ![GitHub issues](https://img.shields.io/github/issues-raw/mikeyduece/dry_serialization?style=plastic)
6
5
 
7
6
  Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dry_serialization`. To experiment with that code, run `bin/console` for an interactive prompt.
@@ -27,13 +26,33 @@ Or install it yourself as:
27
26
  ## Usage
28
27
 
29
28
  - Install with rails generator
30
- * currently supported serializers are `:blueprinter`
29
+ * currently supported serializers are [jsonapi_serializer](https://github.com/jsonapi-serializer/jsonapi-serializer) and [blueprinter](https://github.com/procore/blueprinter)
31
30
 
32
31
 
33
32
  `rails g dry_serialization:<serializer_name>:install`
34
33
 
35
34
 
35
+ - This gem will assume you have an api controller, and create one if you do not. This is to further keep the api separate from the web controllers.
36
+ - The inclusion of the module corresponding to your chosen serializer gem will give you access to `#serialized_resource`.
37
+ - The `#serialized_resource` method also takes an optional 'options' hash. For `jsonapi-serializer`, that can come in the form of the `include` hash and/or `meta` or any of the other options available through that gem. Please see their documentation for all options.
38
+ - Similarly for `blueprinter` it will also take an optional options hash. Again, please see the official documentation in their repo.
39
+ ```ruby
40
+ user = User.find(params[:id])
41
+ render json: serialized_resource(user, UserSerializer) # UserSerializer can be substituted with UserBlueprint if that is the gem you've chosen.
42
+ ```
43
+
44
+ - SerializationHelper
45
+ - This module provides success and error response helper methods.
46
+ - In your controller you can use them like so:
47
+ ```ruby
48
+ def create
49
+ user = User.create(create_user_params)
50
+ return error_response(user.errors) unless user.errors.empty?
36
51
 
52
+ success_response(user, UserSerializer, :created)
53
+ end
54
+ ```
55
+ - Both methods take an optional argument for the status. You can either use the symbol representation or the number code.
37
56
 
38
57
 
39
58
  ## Development
@@ -30,5 +30,5 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency 'activerecord'
31
31
  spec.add_development_dependency 'sqlite3', '~> 1.4'
32
32
  spec.add_development_dependency 'yard', '~> 0.9.11'
33
- spec.add_development_dependency 'fast_jsonapi'
33
+ spec.add_development_dependency 'jsonapi_serializer'
34
34
  end
@@ -1,7 +1,8 @@
1
1
  require "dry_serialization/version"
2
2
  require "dry_serialization/blueprinter"
3
- require "dry_serialization/fast_jsonapi"
3
+ require "dry_serialization/jsonapi_serializer"
4
4
  require "dry_serialization/concerns/serialization_helper"
5
+ require "dry_serialization/concerns/deserializable"
5
6
 
6
7
  module DrySerialization
7
8
 
@@ -0,0 +1,71 @@
1
+ module DrySerialization
2
+ module Concerns
3
+ module Deserializable
4
+
5
+ def deserialized_payload(base_params, object = nil)
6
+ attrs = attributes(base_params)
7
+ id = dig_id(base_params, object)
8
+ attrs.merge!(id: id) if id
9
+
10
+ build_nested_relationships(attrs, base_params)
11
+ end
12
+
13
+ private
14
+
15
+ def build_nested_relationships(attrs, base_params)
16
+ relations = relationships(base_params)
17
+ attrs.merge!(transform_object_values(relations))
18
+ end
19
+
20
+ def transform_object_values(relations)
21
+ relations.to_h.each_with_object({}) do |(key, data), acc|
22
+ key = "#{key}_attributes" unless key.end_with?('_attributes')
23
+ acc[key] ||= transform_relationships_to_get_attributes(data)
24
+ transform_nested_relationships_to_get_attributes(acc[key], data)
25
+ end
26
+ end
27
+
28
+ def transform_nested_relationships_to_get_attributes(parent_relation_hash, data)
29
+ nested_relationships = data[:data].respond_to?(:key?) ? relationships(data) : nil
30
+ return unless nested_relationships
31
+
32
+ nested_relationships.each do |(relation, rel_data)|
33
+ hash = { "#{relation}_attributes": transform_relationships_to_get_attributes(rel_data) }
34
+ parent_relation_hash.merge!(hash)
35
+ end
36
+ end
37
+
38
+ def transform_relationships_to_get_attributes(object)
39
+ case object[:data]
40
+ when Hash
41
+ object[:attributes] || object.dig(:data, :attributes)
42
+ when Array
43
+ object[:data].map do |e|
44
+ attrs = transform_relationships_to_get_attributes({ data: e })
45
+ attrs.merge!(transform_object_values(e[:relationships]))
46
+ end
47
+ else
48
+ return
49
+ end
50
+ end
51
+
52
+ def dig_id(base_params, object)
53
+ return unless object
54
+
55
+ base_params.dig(:data, :id)
56
+ send("#{object}_id")
57
+ rescue
58
+ nil
59
+ end
60
+
61
+ def attributes(base_params)
62
+ base_params.dig(:data, :attributes)
63
+ end
64
+
65
+ def relationships(base_params)
66
+ base_params.dig(:data, :relationships)
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -1,4 +1,4 @@
1
- module DrySerialization::FastJsonapi
1
+ module DrySerialization::JsonapiSerializer
2
2
 
3
3
  def serialized_resource(resource, serializer, options = {})
4
4
  if resource.is_a?(ActiveRecord::Relation) || resource.is_a?(Array)
@@ -1,3 +1,3 @@
1
1
  module DrySerialization
2
- VERSION = "0.4.3.2"
2
+ VERSION = "0.4.4.3"
3
3
  end
@@ -5,7 +5,7 @@ class BaseGenerator < Rails::Generators::Base
5
5
  SERIALIZERS = {
6
6
  ams: 'ActiveModelSerializers',
7
7
  blueprinter: 'Blueprinter',
8
- fast_jsonapi: 'FastJsonapi'
8
+ jsonapi_serializer: 'JsonapiSerializer'
9
9
  }
10
10
 
11
11
  def self.exit_on_failure?
@@ -14,7 +14,7 @@ class BaseGenerator < Rails::Generators::Base
14
14
 
15
15
  private
16
16
 
17
- # Removes other serialization gems, currently just AMS and FastJsonapi
17
+ # Removes other serialization gems, currently just AMS and JsonapiSerializer
18
18
  def remove_other_supported_gems(*gems)
19
19
  gems = [gems] unless gems.is_a?(Array)
20
20
  return if gems.empty?
@@ -7,7 +7,7 @@ module DrySerialization
7
7
 
8
8
  # Add blueprinter gem to gemfile after dry_serialization declaration and bundles the newly declared gem
9
9
  def install_blueprinter
10
- remove_other_supported_gems(SERIALIZERS[:ams], SERIALIZERS[:fast_jsonapi])
10
+ remove_other_supported_gems(SERIALIZERS[:ams], SERIALIZERS[:jsonapi_serializer])
11
11
 
12
12
  puts "Installing #{SERIALIZERS[:blueprinter]}..."
13
13
  insert_into_file('Gemfile',
@@ -1,21 +1,21 @@
1
1
  require 'generators/base_generator'
2
2
 
3
3
  module DrySerialization
4
- module FastJsonapi
4
+ module JsonapiSerializer
5
5
  class InstallGenerator < BaseGenerator
6
6
  source_root File.expand_path("../../../templates", __FILE__)
7
7
 
8
8
  # Add blueprinter gem to gemfile after dry_serialization declaration and bundles the newly declared gem
9
- def install_blueprinter
9
+ def install_jsonapi_serializer
10
10
  remove_other_supported_gems(SERIALIZERS[:blueprinter], SERIALIZERS[:ams])
11
11
 
12
- puts "Installing #{SERIALIZERS[:blueprinter]}..."
12
+ puts "Installing #{SERIALIZERS[:jsonapi_serializer]}..."
13
13
  insert_into_file('Gemfile',
14
- "\ngem 'fast_jsonapi'",
15
- after: "gem 'dry_serialization', source: 'https://gem.fury.io/mikeyduece-gems/'")
14
+ "\ngem 'jsonapi_serializer'",
15
+ after: "gem 'dry_serialization'")
16
16
  run 'bundle install'
17
17
 
18
- helper_include(SERIALIZERS[:fast_jsonapi])
18
+ helper_include(SERIALIZERS[:jsonapi_serializer])
19
19
  end
20
20
 
21
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry_serialization
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3.2
4
+ version: 0.4.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Heft
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-10 00:00:00.000000000 Z
11
+ date: 2020-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -137,7 +137,7 @@ dependencies:
137
137
  - !ruby/object:Gem::Version
138
138
  version: 0.9.11
139
139
  - !ruby/object:Gem::Dependency
140
- name: fast_jsonapi
140
+ name: jsonapi_serializer
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - ">="
@@ -162,12 +162,13 @@ files:
162
162
  - dry_serialization.gemspec
163
163
  - lib/dry_serialization.rb
164
164
  - lib/dry_serialization/blueprinter.rb
165
+ - lib/dry_serialization/concerns/deserializable.rb
165
166
  - lib/dry_serialization/concerns/serialization_helper.rb
166
- - lib/dry_serialization/fast_jsonapi.rb
167
+ - lib/dry_serialization/jsonapi_serializer.rb
167
168
  - lib/dry_serialization/version.rb
168
169
  - lib/generators/base_generator.rb
169
170
  - lib/generators/dry_serialization/blueprinter/install_generator.rb
170
- - lib/generators/dry_serialization/fast_jsonapi/install_generator.rb
171
+ - lib/generators/dry_serialization/jsonapi_serializer/install_generator.rb
171
172
  - lib/generators/templates/api_controller.rb
172
173
  homepage:
173
174
  licenses: