Merge branch 'master' into 0-10-stable

This commit is contained in:
Benjamin Fleischer 2017-03-07 15:52:27 -06:00
commit 01d4f0464b
14 changed files with 99 additions and 49 deletions

View File

@ -7,12 +7,16 @@ rvm:
- 2.2.6
- 2.3.3
- ruby-head
- jruby-9.0.4.0
- jruby-9.1.5.0 # is precompiled per http://rubies.travis-ci.org/
- jruby-head
jdk:
- 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
cache:
directories:
@ -35,13 +39,13 @@ matrix:
exclude:
- rvm: 2.1
env: RAILS_VERSION=master
- rvm: jruby-9.0.4.0
- rvm: jruby-9.1.5.0
env: RAILS_VERSION=master
- rvm: jruby-head
env: RAILS_VERSION=master
- rvm: 2.1
env: RAILS_VERSION=5.0
- rvm: jruby-9.0.4.0
- rvm: jruby-9.1.5.0
env: RAILS_VERSION=5.0
- rvm: jruby-head
env: RAILS_VERSION=5.0

View File

@ -1,6 +1,6 @@
## 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:
@ -14,8 +14,29 @@ Fixes:
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)
- [#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)

View File

@ -42,7 +42,7 @@ Gem::Specification.new do |spec|
# 'minitest'
# '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_development_dependency 'activerecord', rails_versions

View File

@ -1,4 +1,4 @@
version: '{build}'
version: 1.0.{build}-{branch}
skip_tags: true
@ -7,17 +7,23 @@ environment:
matrix:
- ruby_version: "Ruby21"
- ruby_version: "Ruby21-x64"
- ruby_version: "jruby-9.0.0.0"
cache:
- vendor/bundle
install:
- 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 install --path=vendor/bundle --retry=3 --jobs=3
before_test:
- ruby -v
- gem -v
- bundle -v
test_script:
- bundle exec rake ci

View File

@ -12,3 +12,10 @@ You may customize the logger in an initializer, for example:
```ruby
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)
```

View File

@ -225,10 +225,10 @@ With the `:json` adapter, the previous serializer would be rendered as:
link :self do
href "https://example.com/link_author/#{object.id}"
end
link :author { link_author_url(object) }
link :link_authors { link_authors_url }
link(:author) { link_author_url(object) }
link(:link_authors) { link_authors_url }
link :other, 'https://example.com/resource'
link :posts { link_author_posts_url(object) }
link(:posts) { link_author_posts_url(object) }
```
#### #object

View File

@ -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.
```ruby
def pagination_dict(object)
def pagination_dict(collection)
{
current_page: object.current_page,
next_page: object.next_page,
prev_page: object.prev_page, # use object.previous_page when using will_paginate
total_pages: object.total_pages,
total_count: object.total_count
current_page: collection.current_page,
next_page: collection.next_page,
prev_page: collection.prev_page, # use collection.previous_page when using will_paginate
total_pages: collection.total_pages,
total_count: collection.total_count
}
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.
```ruby
render json: @posts, each_serializer: PostPreviewSerializer, meta: meta_attributes(@post)
render json: @posts, each_serializer: PostPreviewSerializer, meta: meta_attributes(@posts)
```
```ruby
#expects pagination!
def meta_attributes(resource, extra_meta = {})
def meta_attributes(collection, extra_meta = {})
{
current_page: resource.current_page,
next_page: resource.next_page,
prev_page: resource.prev_page, # use resource.previous_page when using will_paginate
total_pages: resource.total_pages,
total_count: resource.total_count
current_page: collection.current_page,
next_page: collection.next_page,
prev_page: collection.prev_page, # use collection.previous_page when using will_paginate
total_pages: collection.total_pages,
total_count: collection.total_count
}.merge(extra_meta)
end
```

View File

@ -10,8 +10,8 @@ In ActiveModelSerializers versions 0.10 or later, serializing resources outside
# Create our resource
post = Post.create(title: "Sample post", body: "I love Active Model Serializers!")
# Optional options parameters
options = {}
# Optional options parameters for both the serializer and instance
options = {serializer: PostDetailedSerializer, username: 'sample user'}
# Create a serializable resource instance
serializable_resource = ActiveModelSerializers::SerializableResource.new(post, options)
@ -20,6 +20,7 @@ serializable_resource = ActiveModelSerializers::SerializableResource.new(post, o
model_json = serializable_resource.as_json
```
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

View File

@ -42,4 +42,32 @@ end
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).

View File

@ -107,7 +107,7 @@ end
```
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`
```ruby

View File

@ -1,5 +1,5 @@
module ActiveModel
class Serializer
VERSION = '0.10.4'.freeze
VERSION = '0.10.5'.freeze
end
end

View File

@ -56,14 +56,6 @@ module ActiveModelSerializers
base.attributes :id
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+.
#
# 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')}"
].compact)
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

View File

@ -60,11 +60,11 @@ module ActiveModelSerializers
attr_reader :document_store
def controller_path
request.filtered_parameters[:controller]
request.filtered_parameters.with_indifferent_access[:controller]
end
def action
request.filtered_parameters[:action]
request.filtered_parameters.with_indifferent_access[:action]
end
def schema_directory

View File

@ -6,6 +6,8 @@ module ActiveModelSerializers
config.active_support.test_order = :random
config.action_controller.perform_caching = true
config.action_controller.cache_store = :memory_store
config.filter_parameters += [:password]
end
app.routes.default_url_options = { host: 'example.com' }