panko_serializer 0.8.2 → 0.8.3
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 +4 -4
- data/.github/dependabot.yml +6 -0
- data/.github/workflows/docs.yml +4 -4
- data/.github/workflows/lint.yml +8 -14
- data/.github/workflows/ruby.yml +11 -7
- data/.gitignore +2 -0
- data/.rubocop.yml +29 -21
- data/Appraisals +20 -8
- data/Gemfile +11 -3
- data/README.md +3 -3
- data/Rakefile +3 -1
- data/benchmarks/allocs.rb +2 -2
- data/benchmarks/benchmarking_support.rb +2 -1
- data/benchmarks/bm_ams_0_10.rb +3 -7
- data/benchmarks/bm_panko_json.rb +2 -6
- data/benchmarks/bm_panko_object.rb +2 -6
- data/benchmarks/bm_plain_object.rb +1 -4
- data/benchmarks/bm_serialization_descriptor.rb +1 -1
- data/benchmarks/bm_to_object.rb +2 -6
- data/benchmarks/profile.rb +2 -2
- data/benchmarks/sanity.rb +2 -6
- data/benchmarks/setup.rb +4 -3
- data/benchmarks/type_casts/support.rb +0 -1
- data/docs/docs/associations.md +19 -6
- data/docs/docs/attributes.md +29 -23
- data/docs/docs/design-choices.md +28 -27
- data/docs/docs/getting-started.md +9 -3
- data/docs/docs/introduction.md +5 -5
- data/docs/docs/performance.md +0 -1
- data/docs/docs/response-bag.md +8 -1
- data/docs/docusaurus.config.js +86 -0
- data/docs/package-lock.json +12607 -18674
- data/docs/package.json +14 -7
- data/docs/src/css/customTheme.css +9 -0
- data/docs/static/CNAME +1 -0
- data/ext/panko_serializer/attributes_writer/active_record.c +103 -72
- data/ext/panko_serializer/attributes_writer/active_record.h +2 -0
- data/gemfiles/7.0.0.gemfile +12 -3
- data/gemfiles/7.0.0.gemfile.lock +62 -50
- data/gemfiles/7.1.0.gemfile +15 -6
- data/gemfiles/7.1.0.gemfile.lock +76 -59
- data/gemfiles/{6.1.0.gemfile → 7.2.0.gemfile} +15 -6
- data/gemfiles/{6.1.0.gemfile.lock → 7.2.0.gemfile.lock} +97 -65
- data/gemfiles/8.0.0.gemfile +39 -0
- data/gemfiles/8.0.0.gemfile.lock +219 -0
- data/lib/panko/version.rb +1 -1
- data/panko_serializer.gemspec +1 -1
- metadata +12 -12
- data/.standard.yml +0 -5
- data/docs/.DS_Store +0 -0
- data/docs/README.md +0 -198
- data/docs/core/Footer.js +0 -80
- data/docs/i18n/en.json +0 -50
- data/docs/siteConfig.js +0 -80
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e04b05a5a9a72ce68ad0768dd84b403e4adb6cefa8226f89c30a09d480993481
|
4
|
+
data.tar.gz: 1d7ff5f7b035a497e4b7d48e9749bfc13c3861cfb067b53b167b54c2ce5c8822
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9bcb10f0e30eb48277dcdc9eea463dd475e892ed6eca37818d0d21eb9978cabeb1d0c7b1b8989ff69c1dc5c9b958fe8d0d073f0861d4b7207054e33677d615e7
|
7
|
+
data.tar.gz: 98846622471bed88140fe737f12144e9221e8a439c181c45d3e4ae1cd95aef265e640dd5bb4fda1d6beb8f4fc1f6eecc016620d31b9ec2a51fec6cc214683830
|
data/.github/workflows/docs.yml
CHANGED
@@ -5,7 +5,7 @@ name: Docs Publishing
|
|
5
5
|
|
6
6
|
on:
|
7
7
|
push:
|
8
|
-
branches: [ master ]
|
8
|
+
branches: [ master, docup ]
|
9
9
|
|
10
10
|
jobs:
|
11
11
|
build:
|
@@ -17,9 +17,9 @@ jobs:
|
|
17
17
|
steps:
|
18
18
|
- uses: actions/checkout@v4
|
19
19
|
- name: Use Node.js ${{ matrix.node-version }}
|
20
|
-
uses: actions/setup-node@
|
20
|
+
uses: actions/setup-node@v4
|
21
21
|
with:
|
22
|
-
node-version: '
|
22
|
+
node-version: '20.x'
|
23
23
|
|
24
24
|
- name: Install Dependencies
|
25
25
|
working-directory: ${{ env.working_directory }}
|
@@ -35,5 +35,5 @@ jobs:
|
|
35
35
|
run: |
|
36
36
|
git config --global user.email "action@github.com"
|
37
37
|
git config --global user.name "GitHub Action"
|
38
|
-
GIT_USER=yosiat CURRENT_BRANCH=master USE_SSH=true npm run
|
38
|
+
GIT_USER=yosiat CURRENT_BRANCH=master USE_SSH=true npm run deploy
|
39
39
|
|
data/.github/workflows/lint.yml
CHANGED
@@ -8,28 +8,22 @@ jobs:
|
|
8
8
|
|
9
9
|
steps:
|
10
10
|
- uses: actions/checkout@v4
|
11
|
-
- name: Set up Ruby
|
12
|
-
uses: ruby/setup-ruby@v1
|
13
|
-
with:
|
14
|
-
ruby-version: 3.1.0
|
15
11
|
|
16
12
|
- name: Install deps
|
17
13
|
run: |
|
18
14
|
sudo apt update -y
|
19
15
|
sudo apt install -y libsqlite3-dev
|
20
16
|
|
21
|
-
- name:
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
run: |
|
29
|
-
bundle exec rake standard
|
17
|
+
- name: Lint Ruby code
|
18
|
+
uses: ruby/setup-ruby@v1
|
19
|
+
with:
|
20
|
+
ruby-version: 3
|
21
|
+
bundler-cache: true
|
22
|
+
- run: |
|
23
|
+
bundle exec rake rubocop
|
30
24
|
|
31
25
|
- name: Lint C
|
32
|
-
uses: jidicula/clang-format-action@v4.
|
26
|
+
uses: jidicula/clang-format-action@v4.13.0
|
33
27
|
with:
|
34
28
|
clang-format-version: "16"
|
35
29
|
check-path: "ext/panko_serializer"
|
data/.github/workflows/ruby.yml
CHANGED
@@ -8,24 +8,28 @@ jobs:
|
|
8
8
|
strategy:
|
9
9
|
fail-fast: false
|
10
10
|
matrix:
|
11
|
-
ruby: ["3.
|
12
|
-
rails: ["
|
11
|
+
ruby: ["3.1", "3.2", "3.3"]
|
12
|
+
rails: ["7.0.0", "7.1.0", "7.2.0", "8.0.0"]
|
13
|
+
exclude:
|
14
|
+
- ruby: 3.1
|
15
|
+
rails: 8.0.0
|
13
16
|
|
14
17
|
steps:
|
18
|
+
- name: Install deps
|
19
|
+
run: |
|
20
|
+
sudo apt update -y
|
21
|
+
sudo apt install -y libsqlite3-dev
|
22
|
+
|
15
23
|
- uses: actions/checkout@v4
|
16
24
|
- name: Set up Ruby ${{ matrix.ruby }}
|
17
25
|
uses: ruby/setup-ruby@v1
|
18
26
|
with:
|
19
27
|
ruby-version: ${{ matrix.ruby }}
|
20
28
|
|
21
|
-
- name: Install deps
|
22
|
-
run: |
|
23
|
-
sudo apt update -y
|
24
|
-
sudo apt install -y libsqlite3-dev
|
25
29
|
|
26
30
|
- name: Gems Cache
|
27
31
|
id: gem-cache
|
28
|
-
uses: actions/cache@
|
32
|
+
uses: actions/cache@v4
|
29
33
|
with:
|
30
34
|
path: vendor/bundle
|
31
35
|
key: ${{ runner.os }}-${{ matrix.ruby }}-${{ matrix.rails }}-gem
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
@@ -1,29 +1,37 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
# We want Exclude directives from different
|
2
|
+
# config files to get merged, not overwritten
|
3
|
+
inherit_mode:
|
4
|
+
merge:
|
5
|
+
- Exclude
|
6
|
+
|
7
|
+
require:
|
8
|
+
- standard
|
9
|
+
- rubocop-performance
|
10
|
+
- standard-performance
|
11
|
+
- rubocop-rspec
|
12
|
+
|
13
|
+
inherit_gem:
|
14
|
+
standard: config/base.yml
|
15
|
+
standard-performance: config/base.yml
|
5
16
|
|
6
|
-
|
7
|
-
|
17
|
+
AllCops:
|
18
|
+
TargetRubyVersion: 3.1
|
19
|
+
SuggestExtensions: false
|
20
|
+
NewCops: disable
|
21
|
+
Exclude:
|
22
|
+
- ext/**/*
|
23
|
+
- gemfiles/**/*
|
8
24
|
|
9
|
-
Style/StringLiterals:
|
10
|
-
EnforcedStyle: double_quotes
|
11
25
|
|
12
26
|
Style/FrozenStringLiteralComment:
|
13
27
|
Enabled: true
|
14
28
|
EnforcedStyle: always
|
29
|
+
SafeAutoCorrect: true
|
15
30
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
Layout/TrailingWhitespace:
|
20
|
-
Enabled: true
|
31
|
+
# TODO: need to work on specs.
|
32
|
+
RSpec:
|
33
|
+
Enabled: false
|
21
34
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
Style/RedundantFreeze:
|
26
|
-
Enabled: true
|
27
|
-
|
28
|
-
Performance/UnfreezeString:
|
29
|
-
Enabled: true
|
35
|
+
Lint/ConstantDefinitionInBlock:
|
36
|
+
Exclude:
|
37
|
+
- spec/**/*
|
data/Appraisals
CHANGED
@@ -1,17 +1,29 @@
|
|
1
|
-
|
2
|
-
gem "activesupport", "~> 6.1.0"
|
3
|
-
gem "activemodel", "~> 6.1.0"
|
4
|
-
gem "activerecord", "~> 6.1.0", group: :test
|
5
|
-
end
|
1
|
+
# frozen_string_literal: true
|
6
2
|
|
7
3
|
appraise "7.0.0" do
|
4
|
+
gem "sqlite3", "~> 1.4"
|
8
5
|
gem "activesupport", "~> 7.0.0"
|
9
6
|
gem "activemodel", "~> 7.0.0"
|
10
7
|
gem "activerecord", "~> 7.0.0", group: :test
|
11
8
|
end
|
12
9
|
|
13
10
|
appraise "7.1.0" do
|
14
|
-
gem "
|
15
|
-
gem "
|
16
|
-
gem "
|
11
|
+
gem "sqlite3", "~> 1.4"
|
12
|
+
gem "activesupport", "~> 7.1.5"
|
13
|
+
gem "activemodel", "~> 7.1.5"
|
14
|
+
gem "activerecord", "~> 7.1.5", group: :test
|
15
|
+
end
|
16
|
+
|
17
|
+
appraise "7.2.0" do
|
18
|
+
gem "sqlite3", "~> 1.4"
|
19
|
+
gem "activesupport", "~> 7.2.0"
|
20
|
+
gem "activemodel", "~> 7.2.0"
|
21
|
+
gem "activerecord", "~> 7.2.0", group: :test
|
22
|
+
end
|
23
|
+
|
24
|
+
appraise "8.0.0" do
|
25
|
+
gem "sqlite3", ">= 2.1"
|
26
|
+
gem "activesupport", "~> 8.0.0"
|
27
|
+
gem "activemodel", "~> 8.0.0"
|
28
|
+
gem "activerecord", "~> 8.0.0", group: :test
|
17
29
|
end
|
data/Gemfile
CHANGED
@@ -5,8 +5,9 @@ source "https://rubygems.org"
|
|
5
5
|
gemspec
|
6
6
|
|
7
7
|
group :benchmarks do
|
8
|
-
gem "
|
9
|
-
gem "
|
8
|
+
gem "vernier"
|
9
|
+
gem "stackprof"
|
10
|
+
gem "pg"
|
10
11
|
|
11
12
|
gem "benchmark-ips"
|
12
13
|
gem "active_model_serializers", "~> 0.10"
|
@@ -25,4 +26,11 @@ group :development do
|
|
25
26
|
gem "rake-compiler"
|
26
27
|
end
|
27
28
|
|
28
|
-
|
29
|
+
group :development, :test do
|
30
|
+
gem "rubocop"
|
31
|
+
|
32
|
+
gem "standard"
|
33
|
+
gem "standard-performance"
|
34
|
+
gem "rubocop-performance"
|
35
|
+
gem "rubocop-rspec"
|
36
|
+
end
|
data/README.md
CHANGED
@@ -4,20 +4,20 @@
|
|
4
4
|
|
5
5
|
Panko is a library which is inspired by ActiveModelSerializers 0.9 for serializing ActiveRecord/Ruby objects to JSON strings, fast.
|
6
6
|
|
7
|
-
To achieve its [performance](https://panko.dev/docs/performance
|
7
|
+
To achieve its [performance](https://panko.dev/docs/performance/):
|
8
8
|
|
9
9
|
* Oj - Panko relies on Oj since it's fast and allows for incremental serialization using `Oj::StringWriter`
|
10
10
|
* Serialization Descriptor - Panko computes most of the metadata ahead of time, to save time later in serialization.
|
11
11
|
* Type casting — Panko does type casting by itself, instead of relying on ActiveRecord.
|
12
12
|
|
13
|
-
To dig deeper about the performance choices, read [Design Choices](https://panko.dev/docs/design-choices
|
13
|
+
To dig deeper about the performance choices, read [Design Choices](https://panko.dev/docs/design-choices/).
|
14
14
|
|
15
15
|
|
16
16
|
Support
|
17
17
|
-------
|
18
18
|
|
19
19
|
- [Documentation](https://panko.dev/docs)
|
20
|
-
- [Getting Started](https://panko.dev/docs/getting-started
|
20
|
+
- [Getting Started](https://panko.dev/docs/getting-started/)
|
21
21
|
|
22
22
|
License
|
23
23
|
-------
|
data/Rakefile
CHANGED
@@ -6,7 +6,7 @@ require "json"
|
|
6
6
|
require "terminal-table"
|
7
7
|
require "rake/extensiontask"
|
8
8
|
require "pty"
|
9
|
-
require "
|
9
|
+
require "rubocop/rake_task"
|
10
10
|
|
11
11
|
gem = Gem::Specification.load(File.dirname(__FILE__) + "/panko_serializer.gemspec")
|
12
12
|
|
@@ -24,6 +24,8 @@ Rake::Task[:compile].prerequisites << :clean
|
|
24
24
|
|
25
25
|
task default: :spec
|
26
26
|
|
27
|
+
RuboCop::RakeTask.new
|
28
|
+
|
27
29
|
def print_and_flush(str)
|
28
30
|
print str
|
29
31
|
$stdout.flush
|
data/benchmarks/allocs.rb
CHANGED
@@ -10,8 +10,8 @@ class PostFastSerializer < Panko::Serializer
|
|
10
10
|
attributes :id, :body, :title, :author_id
|
11
11
|
end
|
12
12
|
|
13
|
-
def count_allocs(&
|
14
|
-
memory_report = MemoryProfiler.report(&
|
13
|
+
def count_allocs(&)
|
14
|
+
memory_report = MemoryProfiler.report(&)
|
15
15
|
puts memory_report.pretty_print
|
16
16
|
end
|
17
17
|
|
@@ -9,7 +9,8 @@ module Benchmark
|
|
9
9
|
def data
|
10
10
|
posts = Post.all.includes(:author).to_a
|
11
11
|
posts_50 = posts.first(50).to_a
|
12
|
-
|
12
|
+
posts_single = posts.first(1).to_a
|
13
|
+
{all: posts, small: posts_50, single: posts_single}
|
13
14
|
end
|
14
15
|
|
15
16
|
def run(label = nil, time: 10, disable_gc: true, warmup: 3, &block)
|
data/benchmarks/bm_ams_0_10.rb
CHANGED
@@ -5,7 +5,7 @@ require_relative "app"
|
|
5
5
|
require_relative "setup"
|
6
6
|
|
7
7
|
# disable logging for benchmarks
|
8
|
-
ActiveModelSerializers.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(
|
8
|
+
ActiveModelSerializers.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(File::NULL))
|
9
9
|
|
10
10
|
class AmsAuthorFastSerializer < ActiveModel::Serializer
|
11
11
|
attributes :id, :name
|
@@ -24,17 +24,13 @@ end
|
|
24
24
|
def benchmark_ams(prefix, serializer, options = {})
|
25
25
|
merged_options = options.merge(each_serializer: serializer)
|
26
26
|
|
27
|
-
|
28
|
-
posts = data[:all]
|
29
|
-
posts_50 = data[:small]
|
27
|
+
posts = Benchmark.data[:all]
|
30
28
|
|
31
29
|
Benchmark.run("AMS_#{prefix}_Posts_#{posts.count}") do
|
32
30
|
ActiveModelSerializers::SerializableResource.new(posts, merged_options).to_json
|
33
31
|
end
|
34
32
|
|
35
|
-
|
36
|
-
posts = data[:all]
|
37
|
-
posts_50 = data[:small]
|
33
|
+
posts_50 = Benchmark.data[:small]
|
38
34
|
|
39
35
|
Benchmark.run("AMS_#{prefix}_Posts_50") do
|
40
36
|
ActiveModelSerializers::SerializableResource.new(posts_50, merged_options).to_json
|
data/benchmarks/bm_panko_json.rb
CHANGED
@@ -37,9 +37,7 @@ class AuthorWithHasManyFastSerializer < Panko::Serializer
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def benchmark(prefix, serializer, options = {})
|
40
|
-
|
41
|
-
posts = data[:all]
|
42
|
-
posts_50 = data[:small]
|
40
|
+
posts = Benchmark.data[:all]
|
43
41
|
|
44
42
|
merged_options = options.merge(each_serializer: serializer)
|
45
43
|
|
@@ -47,9 +45,7 @@ def benchmark(prefix, serializer, options = {})
|
|
47
45
|
Panko::ArraySerializer.new(posts, merged_options).to_json
|
48
46
|
end
|
49
47
|
|
50
|
-
|
51
|
-
posts = data[:all]
|
52
|
-
posts_50 = data[:small]
|
48
|
+
posts_50 = Benchmark.data[:small]
|
53
49
|
|
54
50
|
Benchmark.run("Panko_ActiveRecord_#{prefix}_Posts_50") do
|
55
51
|
Panko::ArraySerializer.new(posts_50, merged_options).to_json
|
@@ -25,9 +25,7 @@ class AuthorWithHasManyFastSerializer < Panko::Serializer
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def benchmark(prefix, serializer, options = {})
|
28
|
-
|
29
|
-
posts = data[:all]
|
30
|
-
posts_50 = data[:small]
|
28
|
+
posts = Benchmark.data[:all]
|
31
29
|
|
32
30
|
merged_options = options.merge(each_serializer: serializer)
|
33
31
|
|
@@ -35,9 +33,7 @@ def benchmark(prefix, serializer, options = {})
|
|
35
33
|
Panko::ArraySerializer.new(posts, merged_options).to_a
|
36
34
|
end
|
37
35
|
|
38
|
-
|
39
|
-
posts = data[:all]
|
40
|
-
posts_50 = data[:small]
|
36
|
+
posts_50 = Benchmark.data[:small]
|
41
37
|
|
42
38
|
Benchmark.run("Panko_ActiveRecord_#{prefix}_Posts_50") do
|
43
39
|
Panko::ArraySerializer.new(posts_50, merged_options).to_a
|
@@ -78,7 +78,6 @@ end
|
|
78
78
|
def benchmark(prefix, serializer, options = {})
|
79
79
|
data = benchmark_data
|
80
80
|
posts = data[:all]
|
81
|
-
posts_50 = data[:small]
|
82
81
|
|
83
82
|
merged_options = options.merge(each_serializer: serializer)
|
84
83
|
|
@@ -86,9 +85,7 @@ def benchmark(prefix, serializer, options = {})
|
|
86
85
|
Panko::ArraySerializer.new(posts, merged_options).to_json
|
87
86
|
end
|
88
87
|
|
89
|
-
|
90
|
-
posts = data[:all]
|
91
|
-
posts_50 = data[:small]
|
88
|
+
posts_50 = benchmark_data[:small]
|
92
89
|
|
93
90
|
Benchmark.run("Panko_Plain_#{prefix}_Posts_50") do
|
94
91
|
Panko::ArraySerializer.new(posts_50, merged_options).to_json
|
data/benchmarks/bm_to_object.rb
CHANGED
@@ -62,9 +62,7 @@ class AuthorWithHasManyFastSerializer < Panko::Serializer
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def benchmark(prefix, serializer, options = {})
|
65
|
-
|
66
|
-
posts = data[:all]
|
67
|
-
posts_50 = data[:small]
|
65
|
+
posts = Benchmark.data[:all]
|
68
66
|
|
69
67
|
merged_options = options.merge(each_serializer: serializer)
|
70
68
|
|
@@ -72,9 +70,7 @@ def benchmark(prefix, serializer, options = {})
|
|
72
70
|
Panko::ArraySerializer.new(posts, merged_options).to_a
|
73
71
|
end
|
74
72
|
|
75
|
-
|
76
|
-
posts = data[:all]
|
77
|
-
posts_50 = data[:small]
|
73
|
+
posts_50 = Benchmark.data[:small]
|
78
74
|
|
79
75
|
Benchmark.run("Panko_#{prefix}_Posts_50") do
|
80
76
|
Panko::ArraySerializer.new(posts_50, merged_options).to_a
|
data/benchmarks/profile.rb
CHANGED
data/benchmarks/sanity.rb
CHANGED
@@ -62,9 +62,7 @@ class AuthorWithHasManyFastSerializer < Panko::Serializer
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def benchmark(prefix, serializer, options = {})
|
65
|
-
|
66
|
-
posts = data[:all]
|
67
|
-
posts_50 = data[:small]
|
65
|
+
posts = Benchmark.data[:all]
|
68
66
|
|
69
67
|
merged_options = options.merge(each_serializer: serializer)
|
70
68
|
|
@@ -72,9 +70,7 @@ def benchmark(prefix, serializer, options = {})
|
|
72
70
|
Panko::ArraySerializer.new(posts, merged_options).to_json
|
73
71
|
end
|
74
72
|
|
75
|
-
|
76
|
-
posts = data[:all]
|
77
|
-
posts_50 = data[:small]
|
73
|
+
posts_50 = Benchmark.data[:small]
|
78
74
|
|
79
75
|
Benchmark.run("Panko_#{prefix}_Posts_50") do
|
80
76
|
Panko::ArraySerializer.new(posts_50, merged_options).to_json
|
data/benchmarks/setup.rb
CHANGED
@@ -5,6 +5,7 @@
|
|
5
5
|
##########################################
|
6
6
|
require "active_record"
|
7
7
|
require "sqlite3"
|
8
|
+
require "securerandom"
|
8
9
|
|
9
10
|
# Change the following to reflect your database settings
|
10
11
|
ActiveRecord::Base.establish_connection(
|
@@ -46,9 +47,9 @@ Author.destroy_all
|
|
46
47
|
Post.transaction do
|
47
48
|
ENV.fetch("ITEMS_COUNT", "2300").to_i.times do
|
48
49
|
Post.create(
|
49
|
-
body:
|
50
|
-
title:
|
51
|
-
author: Author.create(name:
|
50
|
+
body: SecureRandom.hex(30),
|
51
|
+
title: SecureRandom.hex(20),
|
52
|
+
author: Author.create(name: SecureRandom.alphanumeric),
|
52
53
|
data: {a: 1, b: 2, c: 3}
|
53
54
|
)
|
54
55
|
end
|
data/docs/docs/associations.md
CHANGED
@@ -3,18 +3,19 @@ id: associations
|
|
3
3
|
title: Associations
|
4
4
|
sidebar_label: Associations
|
5
5
|
---
|
6
|
-
|
7
6
|
A serializer can define it's own associations - both `has_many` and `has_one` to serializer under the context of the object.
|
8
7
|
|
9
8
|
For example:
|
10
9
|
|
11
10
|
```ruby
|
11
|
+
|
12
12
|
class PostSerializer < Panko::Serializer
|
13
13
|
attributes :title, :body
|
14
14
|
|
15
15
|
has_one :author, serializer: AuthorSerializer
|
16
16
|
has_many :comments, each_serializer: CommentSerializer
|
17
17
|
end
|
18
|
+
|
18
19
|
```
|
19
20
|
|
20
21
|
### Associations with aliases
|
@@ -23,33 +24,40 @@ An association key name can be aliased with the `name` option.
|
|
23
24
|
|
24
25
|
For example:
|
25
26
|
the `actual_author` property will be converted to `alias_author`.
|
27
|
+
|
26
28
|
```ruby
|
29
|
+
|
27
30
|
class PostSerializer < Panko::Serializer
|
28
31
|
attributes :title, :body
|
29
32
|
|
30
33
|
has_one :actual_author, serializer: AuthorSerializer, name: :alias_author
|
31
34
|
has_many :comments, each_serializer: CommentSerializer
|
32
35
|
end
|
36
|
+
|
33
37
|
```
|
38
|
+
|
34
39
|
### Inference
|
35
40
|
|
36
41
|
Panko can find the type of the serializer by looking at the realtionship name, so instead specifying
|
37
42
|
the serializer at the above example, we can -
|
38
43
|
|
39
44
|
```ruby
|
45
|
+
|
40
46
|
class PostSerializer < Panko::Serializer
|
41
47
|
attributes :title, :body
|
42
48
|
|
43
49
|
has_one :author
|
44
50
|
has_many :comments
|
45
51
|
end
|
52
|
+
|
46
53
|
```
|
47
54
|
|
48
55
|
The logic of inferencing is -
|
49
|
-
- Take the name of the relationship (for example - `:author` / `:comments`) singularize and camelize it
|
50
|
-
- Look for const defined with the name aboe and "Serializer" suffix (by using `Object.const_get`)
|
51
56
|
|
52
|
-
|
57
|
+
- Take the name of the relationship (for example - `:author` / `:comments`) singularize and camelize it
|
58
|
+
- Look for const defined with the name aboe and "Serializer" suffix (by using `Object.const_get`)
|
59
|
+
|
60
|
+
> If Panko can't find the serializer it will throw an error on startup time, for example: `Can't find serializer for PostSerializer.author has_one relationship`
|
53
61
|
|
54
62
|
## Nested Filters
|
55
63
|
|
@@ -61,6 +69,7 @@ For example, let's say one portion of the application needs to serializer list o
|
|
61
69
|
We can declare tailored serializer for this, or we can re-use the above defined serializer - `PostSerializer` and use nested filters.
|
62
70
|
|
63
71
|
```ruby
|
72
|
+
|
64
73
|
posts = Post.all
|
65
74
|
|
66
75
|
Panko::ArraySerializer.new(posts, each_serializer: PostSerializer, only: {
|
@@ -68,17 +77,20 @@ Panko::ArraySerializer.new(posts, each_serializer: PostSerializer, only: {
|
|
68
77
|
author: [:id],
|
69
78
|
comments: [:id],
|
70
79
|
})
|
80
|
+
|
71
81
|
```
|
72
82
|
|
73
83
|
Let's dissect `only` option we passed -
|
74
|
-
|
75
|
-
|
84
|
+
|
85
|
+
- `instance` - list of attributes (and associations) we want to serializer for current instance of the serializer, in this case - `PostSerializer`.
|
86
|
+
- `author`, `comments` - here we specify the list of attributes we want to serialize for each association.
|
76
87
|
|
77
88
|
It's important to note that Nested Filters, are recursive, in other words, we can filter the association's associations.
|
78
89
|
|
79
90
|
For example, `CommentSerializer` have has_one association `Author`, and for each `comments.author` we only it's name.
|
80
91
|
|
81
92
|
```ruby
|
93
|
+
|
82
94
|
posts = Post.all
|
83
95
|
|
84
96
|
Panko::ArraySerializer.new(posts, only: {
|
@@ -89,6 +101,7 @@ Panko::ArraySerializer.new(posts, only: {
|
|
89
101
|
author: [:name]
|
90
102
|
}
|
91
103
|
})
|
104
|
+
|
92
105
|
```
|
93
106
|
|
94
107
|
As you see now in `comments` the `instance` have different meaning, the `CommentSerializer`.
|