Skip to content

Commit fac1d83

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 1f0e98e + 7d4f0c5 commit fac1d83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+238
-105
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ matrix:
3333
include:
3434
- rvm: 2.2
3535
env: CAPTURE_STDERR=true
36-
- rvm: jruby-9000
37-
env: JRUBY_OPTS='--server -Xcompile.invokedynamic=false -Xcli.debug=true --debug'
36+
- rvm: jruby-9.0.4.0
37+
env: JRUBY_OPTS='-Xcompat.version=2.0 --server -Xcompile.invokedynamic=false -Xcli.debug=true --debug'
3838
allow_failures:
3939
- rvm: ruby-head
4040
- rvm: rbx-2

CHANGELOG.md

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,22 @@ Breaking changes:
1616

1717
Features:
1818

19-
- [#1336](https://github.com/rails-api/active_model_serializers/pull/1336) Added support for Grape >= 0.13, < 1.0
19+
- [#1378](https://github.com/rails-api/active_model_serializers/pull/1378) Change association blocks
20+
to be evaluated in *serializer* scope, rather than *association* scope. (@bf4)
21+
* Syntax changes from e.g.
22+
`has_many :titles do customers.pluck(:title) end` (in #1356) to
23+
`has_many :titles do object.customers.pluck(:title) end`
24+
- [#1356](https://github.com/rails-api/active_model_serializers/pull/1356) Add inline syntax for
25+
attributes and associations (@bf4 @beauby @noahsilas)
26+
* Allows defining attributes so that they don't conflict with existing methods. e.g. `attribute
27+
:title do 'Mr. Topum Hat' end`
28+
* Allows defining associations so that they don't conflict with existing methods. e.g. `has_many
29+
:titles do customers.pluck(:title) end`
30+
* Allows dynamic associations, as compared to compare to using
31+
[`virtual_value`](https://github.com/rails-api/active_model_serializers/pull/1356#discussion_r47146466).
32+
e.g. `has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }]`
33+
* Removes dynamically defined methods on the serializer
34+
- [#1336](https://github.com/rails-api/active_model_serializers/pull/1336) Added support for Grape >= 0.13, < 1.0 (@johnhamelink)
2035
- [#1291](https://github.com/rails-api/active_model_serializers/pull/1291) Add logging (@maurogeorge)
2136
- [#1225](https://github.com/rails-api/active_model_serializers/pull/1225) Better serializer lookup, use nested serializer when it exists (@beauby)
2237
- [#1172](https://github.com/rails-api/active_model_serializers/pull/1172) Better serializer registration, get more than just the first module (@bf4)
@@ -35,6 +50,7 @@ Fixes:
3550
- [#1358](https://github.com/rails-api/active_model_serializers/pull/1358) Handle serializer file paths with spaces (@rwstauner, @bf4)
3651

3752
Misc:
53+
- [#1370](https://github.com/rails-api/active_model_serializers/pull/1370) Simplify attributes handling via a mixin (@beauby)
3854
- [#1233](https://github.com/rails-api/active_model_serializers/pull/1233) Top-level meta and meta_key options no longer handled at serializer level (@beauby)
3955
- [#1232](https://github.com/rails-api/active_model_serializers/pull/1232) fields option no longer handled at serializer level (@beauby)
4056
- [#1178](https://github.com/rails-api/active_model_serializers/pull/1178) env CAPTURE_STDERR=false lets devs see hard failures (@bf4)
@@ -183,6 +199,13 @@ Misc:
183199
* adds method to override association
184200
- [#794](https://github.com/rails-api/active_model_serializers/pull/794) add to_param for correct URL generation (@carlesjove)
185201

202+
### v0.10.0-pre
203+
204+
- [Introduce Adapter](https://github.com/rails-api/active_model_serializers/commit/f00fe5595ddf741dc26127ed8fe81adad833ead5)
205+
- Prefer `ActiveModel::Serializer` to `ActiveModelSerializers`:
206+
- [Namespace](https://github.com/rails-api/active_model_serializers/commit/729a823868e8c7ac86c653fcc7100ee511e08cb6#diff-fe7aa2941c19a41ccea6e52940d84016).
207+
- [README](https://github.com/rails-api/active_model_serializers/commit/4a2d9853ba7486acc1747752982aa5650e7fd6e9).
208+
186209
## 0.09.x
187210

188211
### v0.9.3 (2015/01/21 20:29 +00:00)
@@ -226,6 +249,36 @@ Features:
226249
- [#653](https://github.com/rails-api/active_model_serializers/pull/653) Add "_test" suffix to JsonApi::HasManyTest filename. (@alexgenco)
227250
- [#631](https://github.com/rails-api/active_model_serializers/pull/631) Update build badge URL (@craiglittle)
228251

252+
### 0.9.0.alpha1 - January 7, 2014
253+
254+
### 0.9.0.pre
255+
256+
* The following methods were removed
257+
- Model#active\_model\_serializer
258+
- Serializer#include!
259+
- Serializer#include?
260+
- Serializer#attr\_disabled=
261+
- Serializer#cache
262+
- Serializer#perform\_caching
263+
- Serializer#schema (needs more discussion)
264+
- Serializer#attribute
265+
- Serializer#include\_#{name}? (filter method added)
266+
- Serializer#attributes (took a hash)
267+
268+
* The following things were added
269+
- Serializer#filter method
270+
- CONFIG object
271+
272+
* Remove support for ruby 1.8 versions.
273+
274+
* Require rails >= 3.2.
275+
276+
* Serializers for associations are being looked up in a parent serializer's namespace first. Same with controllers' namespaces.
277+
278+
* Added a "prefix" option in case you want to use a different version of serializer.
279+
280+
* Serializers default namespace can be set in `default_serializer_options` and inherited by associations.
281+
229282
## 0.08.x
230283

231284
### v0.8.3 (2014/12/10 14:45 +00:00)
@@ -241,3 +294,94 @@ Features:
241294
- [#610](https://github.com/rails-api/active_model_serializers/pull/610) ArraySerializer (@bolshakov)
242295
- [#607](https://github.com/rails-api/active_model_serializers/pull/607) ruby syntax highlights (@zigomir)
243296
- [#602](https://github.com/rails-api/active_model_serializers/pull/602) Add DSL for associations (@JordanFaust)
297+
298+
### 0.8.1 (May 6, 2013)
299+
300+
* Fix bug whereby a serializer using 'options' would blow up.
301+
302+
### 0.8.0 (May 5, 2013)
303+
304+
* Attributes can now have optional types.
305+
306+
* A new DefaultSerializer ensures that POROs behave the same way as ActiveModels.
307+
308+
* If you wish to override ActiveRecord::Base#to_Json, you can now require
309+
'active_record/serializer_override'. We don't recommend you do this, but
310+
many users do, so we've left it optional.
311+
312+
* Fixed a bug where ActionController wouldn't always have MimeResponds.
313+
314+
* An optinal caching feature allows you to cache JSON & hashes that AMS uses.
315+
Adding 'cached true' to your Serializers will turn on this cache.
316+
317+
* URL helpers used inside of Engines now work properly.
318+
319+
* Serializers now can filter attributes with `only` and `except`:
320+
321+
```
322+
UserSerializer.new(user, only: [:first_name, :last_name])
323+
UserSerializer.new(user, except: :first_name)
324+
```
325+
326+
* Basic Mongoid support. We now include our mixins in the right place.
327+
328+
* On Ruby 1.8, we now generate an `id` method that properly serializes `id`
329+
columns. See issue #127 for more.
330+
331+
* Add an alias for `scope` method to be the name of the context. By default
332+
this is `current_user`. The name is automatically set when using
333+
`serialization_scope` in the controller.
334+
335+
* Pass through serialization options (such as `:include`) when a model
336+
has no serializer defined.
337+
338+
## [0.7.0 (March 6, 2013)](https://github.com/rails-api/active_model_serializers/commit/fabdc621ff97fbeca317f6301973dd4564b9e695)
339+
340+
* ```embed_key``` option to allow embedding by attributes other than IDs
341+
* Fix rendering nil with custom serializer
342+
* Fix global ```self.root = false```
343+
* Add support for specifying the serializer for an association as a String
344+
* Able to specify keys on the attributes method
345+
* Serializer Reloading via ActiveSupport::DescendantsTracker
346+
* Reduce double map to once; Fixes datamapper eager loading.
347+
348+
## 0.6.0 (October 22, 2012)
349+
350+
* Serialize sets properly
351+
* Add root option to ArraySerializer
352+
* Support polymorphic associations
353+
* Support :each_serializer in ArraySerializer
354+
* Add `scope` method to easily access the scope in the serializer
355+
* Fix regression with Rails 3.2.6; add Rails 4 support
356+
* Allow serialization_scope to be disabled with serialization_scope nil
357+
* Array serializer should support pure ruby objects besides serializers
358+
359+
## 0.05.x
360+
361+
### [0.5.2 (June 5, 2012)](https://github.com/rails-api/active_model_serializers/commit/615afd125c260432d456dc8be845867cf87ea118#diff-0c5c12f311d3b54734fff06069efd2ac)
362+
363+
### [0.5.1 (May 23, 2012)](https://github.com/rails-api/active_model_serializers/commit/00194ec0e41831802fcbf893a34c0bb0853ebe14#diff-0c5c12f311d3b54734fff06069efd2ac)
364+
365+
### [0.5.0 (May 16, 2012)](https://github.com/rails-api/active_model_serializers/commit/33d4842dcd35c7167b0b33fc0abcf00fb2c92286)
366+
367+
* First tagged version
368+
* Changes generators to always generate an ApplicationSerializer
369+
370+
## [0.1.0 (December 21, 2011)](https://github.com/rails-api/active_model_serializers/commit/1e0c9ef93b96c640381575dcd30be07ac946818b)
371+
372+
## First Commit as [Rails Serializers 0.0.1](https://github.com/rails-api/active_model_serializers/commit/d72b66d4c5355b0ff0a75a04895fcc4ea5b0c65e)
373+
(December 1, 2011).
374+
375+
## Prehistory
376+
377+
- [Changing Serialization/Serializers namespace to `Serializable` (November 30, 2011)](https://github.com/rails/rails/commit/8896b4fdc8a543157cdf4dfc378607ebf6c10ab0)
378+
- [Merge branch 'serializers'. This implements the ActiveModel::Serializer object. Includes code, tests, generators and guides. From José and Yehuda with love.](https://github.com/rails/rails/commit/fcacc6986ab60f1fb2e423a73bf47c7abd7b191d)
379+
- But [was reverted](https://github.com/rails/rails/commit/5b2eb64ceb08cd005dc06b721935de5853971473).
380+
'[Revert the serializers API as other alternatives are now also under discussion](https://github.com/rails/rails/commit/0a4035b12a6c59253cb60f9e3456513c6a6a9d33)'.
381+
- [Proposed Implementation to Rails 3.2 by @wycats and @josevalim (November 25, 2011)](https://github.com/rails/rails/pull/3753)
382+
- [Docs and CHANGELOG](https://github.com/rails/rails/commit/696d01f7f4a8ed787924a41cce6df836cd73c46f)
383+
- [Deprecation of ActiveModel::Serialization to ActiveModel::Serializable](https://github.com/rails/rails/blob/696d01f7f4a8ed787924a41cce6df836cd73c46f/activemodel/lib/active_model/serialization.rb)
384+
- [Creation of `ActiveModel::Serialization` from `ActiveModel::Serializer` in Rails (2009)](https://github.com/rails/rails/commit/c6bc8e662614be711f45a8d4b231d5f993b024a7#diff-d029b9768d8df0407a35804a468e3ae5)
385+
- [Integration of `ActiveModel::Serializer` into `ActiveRecord::Serialization`](https://github.com/rails/rails/commit/783db25e0c640c1588732967a87d65c10fddc08e)
386+
- [Creation of `ActiveModel::Serializer` in Rails (2009)](https://github.com/rails/rails/commit/d2b78b3594b9cc9870e6a6ebfeb2e56d00e6ddb8#diff-80d5beeced9bdc24ca2b04a201543bdd)
387+
- [Creation of `ActiveModel::Serializers::JSON` in Rails (2009)](https://github.com/rails/rails/commit/fbdf706fffbfb17731a1f459203d242414ef5086)

CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
First of all, **thank you**!
2+
13
![Commit Strip
24
http://www.commitstrip.com/en/2014/05/07/the-truth-behind-open-source-apps/](docs/how-open-source-maintained.jpg)
35

@@ -214,3 +216,5 @@ To run a single test suite
214216
Which can be further narrowed down to one test:
215217

216218
`$ rake test TEST=path/to/test.rb TESTOPTS="--name=test_something"`
219+
220+
:heart: :sparkling_heart: :heart:

docs/ARCHITECTURE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ and, if there is no serializer, primitives.
1515
The **`ActiveModel::Adapter`** describes the structure of the JSON document generated from a
1616
serializer. For example, the `Attributes` example represents each serializer as its
1717
unmodified attributes. The `JsonApi` adapter represents the serializer as a [JSON
18-
API](jsonapi.org/) document.
18+
API](http://jsonapi.org/) document.
1919

2020
The **`ActiveModel::SerializableResource`** acts to coordinate the serializer(s) and adapter
2121
to an object that responds to `to_json`, and `as_json`. It is used in the controller to

docs/DESIGN.textile

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module ActiveModel
2+
class Serializer
3+
Attribute = Struct.new(:name, :block) do
4+
def value(serializer)
5+
if block
6+
serializer.instance_eval(&block)
7+
else
8+
serializer.read_attribute_for_serialization(name)
9+
end
10+
end
11+
end
12+
end
13+
end

lib/active_model/serializer/attributes.rb

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,32 @@
11
module ActiveModel
22
class Serializer
33
module Attributes
4-
# @api private
5-
class Attribute
6-
delegate :call, to: :reader
7-
8-
attr_reader :name, :reader
9-
10-
def initialize(name)
11-
@name = name
12-
@reader = :no_reader
13-
end
14-
15-
def self.build(name, block)
16-
if block
17-
AttributeBlock.new(name, block)
18-
else
19-
AttributeReader.new(name)
20-
end
21-
end
22-
end
23-
# @api private
24-
class AttributeReader < Attribute
25-
def initialize(name)
26-
super(name)
27-
@reader = ->(instance) { instance.read_attribute_for_serialization(name) }
28-
end
29-
end
30-
# @api private
31-
class AttributeBlock < Attribute
32-
def initialize(name, block)
33-
super(name)
34-
@reader = ->(instance) { instance.instance_eval(&block) }
35-
end
36-
end
37-
384
extend ActiveSupport::Concern
395

406
included do
417
with_options instance_writer: false, instance_reader: false do |serializer|
42-
serializer.class_attribute :_attribute_mappings # @api private : maps attribute key names to names to names of implementing methods, @see #attribute
43-
self._attribute_mappings ||= {}
8+
serializer.class_attribute :_attributes_data # @api private
9+
self._attributes_data ||= {}
4410
end
4511

12+
extend ActiveSupport::Autoload
13+
autoload :Attribute
14+
4615
# Return the +attributes+ of +object+ as presented
4716
# by the serializer.
4817
def attributes(requested_attrs = nil, reload = false)
4918
@attributes = nil if reload
50-
@attributes ||= self.class._attribute_mappings.each_with_object({}) do |(key, attribute_mapping), hash|
19+
@attributes ||= self.class._attributes_data.each_with_object({}) do |(key, attr), hash|
5120
next unless requested_attrs.nil? || requested_attrs.include?(key)
52-
hash[key] = attribute_mapping.call(self)
21+
hash[key] = attr.value(self)
5322
end
5423
end
5524
end
5625

5726
module ClassMethods
5827
def inherited(base)
5928
super
60-
base._attribute_mappings = _attribute_mappings.dup
29+
base._attributes_data = _attributes_data.dup
6130
end
6231

6332
# @example
@@ -85,25 +54,25 @@ def attributes(*attrs)
8554
# end
8655
def attribute(attr, options = {}, &block)
8756
key = options.fetch(:key, attr)
88-
_attribute_mappings[key] = Attribute.build(attr, block)
57+
_attributes_data[key] = Attribute.new(attr, block)
8958
end
9059

9160
# @api private
92-
# names of attribute methods
61+
# keys of attributes
9362
# @see Serializer::attribute
9463
def _attributes
95-
_attribute_mappings.keys
64+
_attributes_data.keys
9665
end
9766

9867
# @api private
9968
# maps attribute value to explict key name
10069
# @see Serializer::attribute
10170
# @see Adapter::FragmentCache#fragment_serializer
10271
def _attributes_keys
103-
_attribute_mappings
104-
.each_with_object({}) do |(key, attribute_mapping), hash|
105-
next if key == attribute_mapping.name
106-
hash[attribute_mapping.name] = { key: key }
72+
_attributes_data
73+
.each_with_object({}) do |(key, attr), hash|
74+
next if key == attr.name
75+
hash[attr.name] = { key: key }
10776
end
10877
end
10978
end

lib/active_model/serializer/reflection.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ class Serializer
88
# has_one :author, serializer: AuthorSerializer
99
# has_many :comments
1010
# has_many :comments, key: :last_comments do
11-
# last(1)
11+
# object.comments.last(1)
1212
# end
1313
# end
1414
#
15-
# Notice that the association block is evaluated in the context of the association.
15+
# Notice that the association block is evaluated in the context of the serializer.
1616
# Specifically, the association 'comments' is evaluated two different ways:
1717
# 1) as 'comments' and named 'comments'.
18-
# 2) as 'comments.last(1)' and named 'last_comments'.
18+
# 2) as 'object.comments.last(1)' and named 'last_comments'.
1919
#
2020
# PostSerializer._reflections #=>
2121
# # [
@@ -29,7 +29,7 @@ class Serializer
2929
# @api private
3030
def value(instance)
3131
if block
32-
instance.read_attribute_for_serialization(name).instance_eval(&block)
32+
instance.instance_eval(&block)
3333
else
3434
instance.read_attribute_for_serialization(name)
3535
end

test/active_model_serializers/logging_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module ActiveModel
44
class Serializer
5-
class LoggingTest < Minitest::Test
5+
class LoggingTest < ActiveSupport::TestCase
66
class TestLogger < ActiveSupport::Logger
77
def initialize
88
@file = StringIO.new

test/active_model_serializers/model_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'test_helper'
22

3-
class ActiveModelSerializers::ModelTest < Minitest::Test
3+
class ActiveModelSerializers::ModelTest < ActiveSupport::TestCase
44
include ActiveModel::Serializer::Lint::Tests
55

66
def setup

0 commit comments

Comments
 (0)