shreddies 0.3.0 → 0.4.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
  SHA256:
3
- metadata.gz: 994ca7a8e1be900a520c23fb51d1d3105ef7ed5354204840b3e4531f1b02bc35
4
- data.tar.gz: 2bb7c93e959057b0d2f3af612bb53ce0bb0a93ca20c2338131971455b8deaa31
3
+ metadata.gz: 362313f00cf053782a4cce7d2ce46b83e8ae078e6812c7c8bb485440f967c3a5
4
+ data.tar.gz: e1f3f78c27fc4def477bfc1f3adb13fb101a57fbc6870add99dbac9f8efb0db4
5
5
  SHA512:
6
- metadata.gz: bfd06349411570a2794796d9c4dc9cb6aa5d578123f9b5013aa3882a0547684f27714832901bfc891b8d0a65ed75775156c17222317163dc3621a6191f95d5c3
7
- data.tar.gz: 8c3e4fd74a2fca6c93c28db3ba0394fce74047b12f555d315061e949601bb3d69ae1a266dce3a617986da9f3b4de8592521e5b3783b79c6d48b84fc042acc3b9
6
+ metadata.gz: 67d80f7708e475b54d8e99a5b24d4198091d0ccfef45a84c131539985bdc682bad234e3df12eb4e8351b3648296a2adffc71e796cd8b149a8336a85495d3acb3
7
+ data.tar.gz: 62e767958dd0b0cd314204a2a7cd573ae7d4df0dd27bdf23a528e64eb4574c2b3e45a5aa9879674a9264c7b57c462f784741a8926e0a82108043396a6caded9e
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shreddies (0.2.0)
4
+ shreddies (0.3.0)
5
5
  activerecord (>= 5)
6
6
  railties (>= 5)
7
7
 
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Shreddies is a JSON serialization library for Rails that focuses on simplicity and speed. No more "magic" DSL's - just plain old Ruby objects! It's primarily intended to serialize Rails models as JSON, but will also work with pretty much anything at all.
4
4
 
5
+ Shreddies primary principle is to be explicit. So a serializer will return nothing until you define some methods. This gives you complete control and everything is a known quantity - no surprises.
6
+
5
7
  ## Installation
6
8
 
7
9
  Add this line to your application's Gemfile:
@@ -91,6 +93,36 @@ ArticleSerializer < Shreddies::Json
91
93
  end
92
94
  ```
93
95
 
96
+ ### ActiveRecord Associations
97
+
98
+ ActiveRecord associations are supported with no additional work on your part. Shreddies will simply call `#as_json` on any method that returns an ActiveRecord model or relation.
99
+
100
+ ```ruby
101
+ # app/serializers/user_serializer.rb
102
+ class UserSerializer < Shreddies::Json
103
+ delegate :articles
104
+
105
+ def latest_article
106
+ articles.latest
107
+ end
108
+ end
109
+ ```
110
+
111
+ And if you need to be specific about what you render, just call the serializer or `#as_json` directly:
112
+
113
+ ```ruby
114
+ # app/serializers/user_serializer.rb
115
+ class UserSerializer < Shreddies::Json
116
+ def articles
117
+ subject.articles.as_json index_by: :slug
118
+ end
119
+
120
+ def latest_article
121
+ LatestArticleSerializer.render articles.latest
122
+ end
123
+ end
124
+ ```
125
+
94
126
  ### `before_render` callback
95
127
 
96
128
  You can define a `#before_render` private method in your serializers, which will act as a callback. It receives the object to be output, and expects you to return the object, which allows you to modify it before rendering.
@@ -66,7 +66,12 @@ module Shreddies
66
66
  end
67
67
 
68
68
  methods.map do |attr|
69
- output[attr] = public_send(attr)
69
+ res = public_send(attr)
70
+ if res.is_a?(ActiveRecord::Relation) || res.is_a?(ActiveRecord::Base)
71
+ res = res.as_json(transform_keys: options[:transform_keys])
72
+ end
73
+
74
+ output[attr] = res
70
75
  end
71
76
 
72
77
  output = before_render(output)
@@ -98,9 +103,7 @@ module Shreddies
98
103
  # Extend with the :module option if given.
99
104
  if options[:module]
100
105
  Array(options[:module]).each do |m|
101
- mod = m.is_a?(Module) ? m : "#{self.class}::#{m}".constantize
102
-
103
- extend mod
106
+ extend m.is_a?(Module) ? m : "#{self.class}::#{m}".constantize
104
107
  end
105
108
  end
106
109
  end
@@ -1,3 +1,3 @@
1
1
  module Shreddies
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shreddies
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Moss