mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge branch 'master' into 0-10-stable
This commit is contained in:
commit
01d4f0464b
10
.travis.yml
10
.travis.yml
@ -7,12 +7,16 @@ rvm:
|
|||||||
- 2.2.6
|
- 2.2.6
|
||||||
- 2.3.3
|
- 2.3.3
|
||||||
- ruby-head
|
- ruby-head
|
||||||
- jruby-9.0.4.0
|
- jruby-9.1.5.0 # is precompiled per http://rubies.travis-ci.org/
|
||||||
- jruby-head
|
- jruby-head
|
||||||
|
|
||||||
jdk:
|
jdk:
|
||||||
- oraclejdk8
|
- oraclejdk8
|
||||||
|
|
||||||
|
before_install:
|
||||||
|
- gem update --system
|
||||||
|
- rvm @global do gem uninstall bundler -a -x
|
||||||
|
- rvm @global do gem install bundler -v 1.13.7
|
||||||
install: bundle install --path=vendor/bundle --retry=3 --jobs=3
|
install: bundle install --path=vendor/bundle --retry=3 --jobs=3
|
||||||
cache:
|
cache:
|
||||||
directories:
|
directories:
|
||||||
@ -35,13 +39,13 @@ matrix:
|
|||||||
exclude:
|
exclude:
|
||||||
- rvm: 2.1
|
- rvm: 2.1
|
||||||
env: RAILS_VERSION=master
|
env: RAILS_VERSION=master
|
||||||
- rvm: jruby-9.0.4.0
|
- rvm: jruby-9.1.5.0
|
||||||
env: RAILS_VERSION=master
|
env: RAILS_VERSION=master
|
||||||
- rvm: jruby-head
|
- rvm: jruby-head
|
||||||
env: RAILS_VERSION=master
|
env: RAILS_VERSION=master
|
||||||
- rvm: 2.1
|
- rvm: 2.1
|
||||||
env: RAILS_VERSION=5.0
|
env: RAILS_VERSION=5.0
|
||||||
- rvm: jruby-9.0.4.0
|
- rvm: jruby-9.1.5.0
|
||||||
env: RAILS_VERSION=5.0
|
env: RAILS_VERSION=5.0
|
||||||
- rvm: jruby-head
|
- rvm: jruby-head
|
||||||
env: RAILS_VERSION=5.0
|
env: RAILS_VERSION=5.0
|
||||||
|
|||||||
23
CHANGELOG.md
23
CHANGELOG.md
@ -1,6 +1,6 @@
|
|||||||
## 0.10.x
|
## 0.10.x
|
||||||
|
|
||||||
### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.4...master)
|
### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.5...master)
|
||||||
|
|
||||||
Breaking changes:
|
Breaking changes:
|
||||||
|
|
||||||
@ -14,8 +14,29 @@ Fixes:
|
|||||||
|
|
||||||
Misc:
|
Misc:
|
||||||
|
|
||||||
|
### [v0.10.5 (2017-03-07)](https://github.com/rails-api/active_model_serializers/compare/v0.10.4...v0.10.5)
|
||||||
|
|
||||||
|
Breaking changes:
|
||||||
|
|
||||||
|
Features:
|
||||||
|
|
||||||
|
- [#2021](https://github.com/rails-api/active_model_serializers/pull/2021) ActiveModelSerializers::Model#attributes. Originally in [#1982](https://github.com/rails-api/active_model_serializers/pull/1982). (@bf4)
|
||||||
|
- [#2057](https://github.com/rails-api/active_model_serializers/pull/2057)
|
||||||
|
Update version constraint for jsonapi-renderer to `['>= 0.1.1.beta1', '< 0.2']`
|
||||||
|
(@jaredbeck)
|
||||||
|
|
||||||
|
Fixes:
|
||||||
|
|
||||||
|
- [#2022](https://github.com/rails-api/active_model_serializers/pull/2022) Mutation of ActiveModelSerializers::Model now changes the attributes. Originally in [#1984](https://github.com/rails-api/active_model_serializers/pull/1984). (@bf4)
|
||||||
|
|
||||||
|
Misc:
|
||||||
|
|
||||||
|
- [#2055](https://github.com/rails-api/active_model_serializers/pull/2055)
|
||||||
|
Replace deprecated dependency jsonapi with jsonapi-renderer. (@jaredbeck)
|
||||||
- [#2021](https://github.com/rails-api/active_model_serializers/pull/2021) Make test attributes explicit. Tests have Model#associations. (@bf4)
|
- [#2021](https://github.com/rails-api/active_model_serializers/pull/2021) Make test attributes explicit. Tests have Model#associations. (@bf4)
|
||||||
- [#1981](https://github.com/rails-api/active_model_serializers/pull/1981) Fix relationship link documentation. (@groyoh)
|
- [#1981](https://github.com/rails-api/active_model_serializers/pull/1981) Fix relationship link documentation. (@groyoh)
|
||||||
|
- [#2035](https://github.com/rails-api/active_model_serializers/pull/2035) Document how to disable the logger. (@MSathieu)
|
||||||
|
- [#2039](https://github.com/rails-api/active_model_serializers/pull/2039) Documentation fixes. (@biow0lf)
|
||||||
|
|
||||||
### [v0.10.4 (2017-01-06)](https://github.com/rails-api/active_model_serializers/compare/v0.10.3...v0.10.4)
|
### [v0.10.4 (2017-01-06)](https://github.com/rails-api/active_model_serializers/compare/v0.10.3...v0.10.4)
|
||||||
|
|
||||||
|
|||||||
@ -42,7 +42,7 @@ Gem::Specification.new do |spec|
|
|||||||
# 'minitest'
|
# 'minitest'
|
||||||
# 'thread_safe'
|
# 'thread_safe'
|
||||||
|
|
||||||
spec.add_runtime_dependency 'jsonapi', '0.1.1.beta6'
|
spec.add_runtime_dependency 'jsonapi-renderer', ['>= 0.1.1.beta1', '< 0.2']
|
||||||
spec.add_runtime_dependency 'case_transform', '>= 0.2'
|
spec.add_runtime_dependency 'case_transform', '>= 0.2'
|
||||||
|
|
||||||
spec.add_development_dependency 'activerecord', rails_versions
|
spec.add_development_dependency 'activerecord', rails_versions
|
||||||
|
|||||||
12
appveyor.yml
12
appveyor.yml
@ -1,4 +1,4 @@
|
|||||||
version: '{build}'
|
version: 1.0.{build}-{branch}
|
||||||
|
|
||||||
skip_tags: true
|
skip_tags: true
|
||||||
|
|
||||||
@ -7,17 +7,23 @@ environment:
|
|||||||
matrix:
|
matrix:
|
||||||
- ruby_version: "Ruby21"
|
- ruby_version: "Ruby21"
|
||||||
- ruby_version: "Ruby21-x64"
|
- ruby_version: "Ruby21-x64"
|
||||||
- ruby_version: "jruby-9.0.0.0"
|
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
- vendor/bundle
|
- vendor/bundle
|
||||||
|
|
||||||
install:
|
install:
|
||||||
- SET PATH=C:\%ruby_version%\bin;%PATH%
|
- SET PATH=C:\%ruby_version%\bin;%PATH%
|
||||||
- gem install bundler
|
- gem update --system
|
||||||
|
- gem uninstall bundler -a -x
|
||||||
|
- gem install bundler -v 1.13.7
|
||||||
- bundle env
|
- bundle env
|
||||||
- bundle install --path=vendor/bundle --retry=3 --jobs=3
|
- bundle install --path=vendor/bundle --retry=3 --jobs=3
|
||||||
|
|
||||||
|
before_test:
|
||||||
|
- ruby -v
|
||||||
|
- gem -v
|
||||||
|
- bundle -v
|
||||||
|
|
||||||
test_script:
|
test_script:
|
||||||
- bundle exec rake ci
|
- bundle exec rake ci
|
||||||
|
|
||||||
|
|||||||
@ -12,3 +12,10 @@ You may customize the logger in an initializer, for example:
|
|||||||
```ruby
|
```ruby
|
||||||
ActiveModelSerializers.logger = Logger.new(STDOUT)
|
ActiveModelSerializers.logger = Logger.new(STDOUT)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
You can also disable the logger, just put this in `config/initializers/active_model_serializers.rb`:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
require 'active_model_serializers'
|
||||||
|
ActiveSupport::Notifications.unsubscribe(ActiveModelSerializers::Logging::RENDER_EVENT)
|
||||||
|
```
|
||||||
|
|||||||
@ -225,10 +225,10 @@ With the `:json` adapter, the previous serializer would be rendered as:
|
|||||||
link :self do
|
link :self do
|
||||||
href "https://example.com/link_author/#{object.id}"
|
href "https://example.com/link_author/#{object.id}"
|
||||||
end
|
end
|
||||||
link :author { link_author_url(object) }
|
link(:author) { link_author_url(object) }
|
||||||
link :link_authors { link_authors_url }
|
link(:link_authors) { link_authors_url }
|
||||||
link :other, 'https://example.com/resource'
|
link :other, 'https://example.com/resource'
|
||||||
link :posts { link_author_posts_url(object) }
|
link(:posts) { link_author_posts_url(object) }
|
||||||
```
|
```
|
||||||
|
|
||||||
#### #object
|
#### #object
|
||||||
|
|||||||
@ -77,13 +77,13 @@ If you are using `JSON` adapter, pagination links will not be included automatic
|
|||||||
Add this method to your base API controller.
|
Add this method to your base API controller.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
def pagination_dict(object)
|
def pagination_dict(collection)
|
||||||
{
|
{
|
||||||
current_page: object.current_page,
|
current_page: collection.current_page,
|
||||||
next_page: object.next_page,
|
next_page: collection.next_page,
|
||||||
prev_page: object.prev_page, # use object.previous_page when using will_paginate
|
prev_page: collection.prev_page, # use collection.previous_page when using will_paginate
|
||||||
total_pages: object.total_pages,
|
total_pages: collection.total_pages,
|
||||||
total_count: object.total_count
|
total_count: collection.total_count
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
@ -117,18 +117,18 @@ ex.
|
|||||||
You can also achieve the same result if you have a helper method that adds the pagination info in the meta tag. For instance, in your action specify a custom serializer.
|
You can also achieve the same result if you have a helper method that adds the pagination info in the meta tag. For instance, in your action specify a custom serializer.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
render json: @posts, each_serializer: PostPreviewSerializer, meta: meta_attributes(@post)
|
render json: @posts, each_serializer: PostPreviewSerializer, meta: meta_attributes(@posts)
|
||||||
```
|
```
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
#expects pagination!
|
#expects pagination!
|
||||||
def meta_attributes(resource, extra_meta = {})
|
def meta_attributes(collection, extra_meta = {})
|
||||||
{
|
{
|
||||||
current_page: resource.current_page,
|
current_page: collection.current_page,
|
||||||
next_page: resource.next_page,
|
next_page: collection.next_page,
|
||||||
prev_page: resource.prev_page, # use resource.previous_page when using will_paginate
|
prev_page: collection.prev_page, # use collection.previous_page when using will_paginate
|
||||||
total_pages: resource.total_pages,
|
total_pages: collection.total_pages,
|
||||||
total_count: resource.total_count
|
total_count: collection.total_count
|
||||||
}.merge(extra_meta)
|
}.merge(extra_meta)
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|||||||
@ -10,8 +10,8 @@ In ActiveModelSerializers versions 0.10 or later, serializing resources outside
|
|||||||
# Create our resource
|
# Create our resource
|
||||||
post = Post.create(title: "Sample post", body: "I love Active Model Serializers!")
|
post = Post.create(title: "Sample post", body: "I love Active Model Serializers!")
|
||||||
|
|
||||||
# Optional options parameters
|
# Optional options parameters for both the serializer and instance
|
||||||
options = {}
|
options = {serializer: PostDetailedSerializer, username: 'sample user'}
|
||||||
|
|
||||||
# Create a serializable resource instance
|
# Create a serializable resource instance
|
||||||
serializable_resource = ActiveModelSerializers::SerializableResource.new(post, options)
|
serializable_resource = ActiveModelSerializers::SerializableResource.new(post, options)
|
||||||
@ -20,6 +20,7 @@ serializable_resource = ActiveModelSerializers::SerializableResource.new(post, o
|
|||||||
model_json = serializable_resource.as_json
|
model_json = serializable_resource.as_json
|
||||||
```
|
```
|
||||||
The object that is passed to `ActiveModelSerializers::SerializableResource.new` can be a single resource or a collection.
|
The object that is passed to `ActiveModelSerializers::SerializableResource.new` can be a single resource or a collection.
|
||||||
|
The additional options are the same options that are passed [through controllers](../general/rendering.md#explicit-serializer).
|
||||||
|
|
||||||
### Looking up the Serializer for a Resource
|
### Looking up the Serializer for a Resource
|
||||||
|
|
||||||
|
|||||||
@ -42,4 +42,32 @@ end
|
|||||||
|
|
||||||
The default serializer would be `MyModelSerializer`.
|
The default serializer would be `MyModelSerializer`.
|
||||||
|
|
||||||
|
*IMPORTANT*: There is a surprising behavior (bug) in the current implementation of ActiveModelSerializers::Model that
|
||||||
|
prevents an accessor from modifying attributes on the instance. The fix for this bug
|
||||||
|
is a breaking change, so we have made an opt-in configuration.
|
||||||
|
|
||||||
|
New applications should set:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
ActiveModelSerializers::Model.derive_attributes_from_names_and_fix_accessors
|
||||||
|
```
|
||||||
|
|
||||||
|
Existing applications can use the fix *and* avoid breaking changes
|
||||||
|
by making a superclass for new models. For example:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class SerializablePoro < ActiveModelSerializers::Model
|
||||||
|
derive_attributes_from_names_and_fix_accessors
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
So that `MyModel` above would inherit from `SerializablePoro`.
|
||||||
|
|
||||||
|
`derive_attributes_from_names_and_fix_accessors` prepends the `DeriveAttributesFromNamesAndFixAccessors`
|
||||||
|
module and does the following:
|
||||||
|
|
||||||
|
- `id` will *always* be in the attributes. (This is until we separate out the caching requirement for POROs.)
|
||||||
|
- Overwrites the `attributes` method to that it only returns declared attributes.
|
||||||
|
`attributes` will now be a frozen hash with indifferent access.
|
||||||
|
|
||||||
For more information, see [README: What does a 'serializable resource' look like?](../../README.md#what-does-a-serializable-resource-look-like).
|
For more information, see [README: What does a 'serializable resource' look like?](../../README.md#what-does-a-serializable-resource-look-like).
|
||||||
|
|||||||
@ -107,7 +107,7 @@ end
|
|||||||
|
|
||||||
```
|
```
|
||||||
Add this class to your app however you see fit. This is the class that your existing serializers
|
Add this class to your app however you see fit. This is the class that your existing serializers
|
||||||
that inherit from `ActiveMode::Serializer` should inherit from.
|
that inherit from `ActiveModel::Serializer` should inherit from.
|
||||||
|
|
||||||
### 3. Add `ActiveModel::V08::CollectionSerializer`
|
### 3. Add `ActiveModel::V08::CollectionSerializer`
|
||||||
```ruby
|
```ruby
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
VERSION = '0.10.4'.freeze
|
VERSION = '0.10.5'.freeze
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -56,14 +56,6 @@ module ActiveModelSerializers
|
|||||||
base.attributes :id
|
base.attributes :id
|
||||||
end
|
end
|
||||||
|
|
||||||
# Override the initialize method so that attributes aren't processed.
|
|
||||||
#
|
|
||||||
# @param attributes [Hash]
|
|
||||||
def initialize(attributes = {})
|
|
||||||
@errors = ActiveModel::Errors.new(self)
|
|
||||||
super
|
|
||||||
end
|
|
||||||
|
|
||||||
# Override the +attributes+ method so that the hash is derived from +attribute_names+.
|
# Override the +attributes+ method so that the hash is derived from +attribute_names+.
|
||||||
#
|
#
|
||||||
# The the fields in +attribute_names+ determines the returned hash.
|
# The the fields in +attribute_names+ determines the returned hash.
|
||||||
@ -133,16 +125,5 @@ module ActiveModelSerializers
|
|||||||
"#{id}-#{updated_at.strftime('%Y%m%d%H%M%S%9N')}"
|
"#{id}-#{updated_at.strftime('%Y%m%d%H%M%S%9N')}"
|
||||||
].compact)
|
].compact)
|
||||||
end
|
end
|
||||||
|
|
||||||
# The following methods are needed to be minimally implemented for ActiveModel::Errors
|
|
||||||
# :nocov:
|
|
||||||
def self.human_attribute_name(attr, _options = {})
|
|
||||||
attr
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.lookup_ancestors
|
|
||||||
[self]
|
|
||||||
end
|
|
||||||
# :nocov:
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -60,11 +60,11 @@ module ActiveModelSerializers
|
|||||||
attr_reader :document_store
|
attr_reader :document_store
|
||||||
|
|
||||||
def controller_path
|
def controller_path
|
||||||
request.filtered_parameters[:controller]
|
request.filtered_parameters.with_indifferent_access[:controller]
|
||||||
end
|
end
|
||||||
|
|
||||||
def action
|
def action
|
||||||
request.filtered_parameters[:action]
|
request.filtered_parameters.with_indifferent_access[:action]
|
||||||
end
|
end
|
||||||
|
|
||||||
def schema_directory
|
def schema_directory
|
||||||
|
|||||||
@ -6,6 +6,8 @@ module ActiveModelSerializers
|
|||||||
config.active_support.test_order = :random
|
config.active_support.test_order = :random
|
||||||
config.action_controller.perform_caching = true
|
config.action_controller.perform_caching = true
|
||||||
config.action_controller.cache_store = :memory_store
|
config.action_controller.cache_store = :memory_store
|
||||||
|
|
||||||
|
config.filter_parameters += [:password]
|
||||||
end
|
end
|
||||||
|
|
||||||
app.routes.default_url_options = { host: 'example.com' }
|
app.routes.default_url_options = { host: 'example.com' }
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user