mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
commit
f4b17166eb
14
README.md
14
README.md
@ -36,8 +36,6 @@ class PostSerializer < ActiveModel::Serializer
|
|||||||
attributes :title, :body
|
attributes :title, :body
|
||||||
|
|
||||||
has_many :comments
|
has_many :comments
|
||||||
|
|
||||||
url :post
|
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -48,8 +46,6 @@ class CommentSerializer < ActiveModel::Serializer
|
|||||||
attributes :name, :body
|
attributes :name, :body
|
||||||
|
|
||||||
belongs_to :post
|
belongs_to :post
|
||||||
|
|
||||||
url [:post, :comment]
|
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -239,8 +235,6 @@ class PostSerializer < ActiveModel::Serializer
|
|||||||
|
|
||||||
has_many :comments
|
has_many :comments
|
||||||
has_one :author
|
has_one :author
|
||||||
|
|
||||||
url :post
|
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -251,8 +245,6 @@ class CommentSerializer < ActiveModel::Serializer
|
|||||||
attributes :name, :body
|
attributes :name, :body
|
||||||
|
|
||||||
belongs_to :post_id
|
belongs_to :post_id
|
||||||
|
|
||||||
url [:post, :comment]
|
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -274,8 +266,6 @@ And you can change the JSON key that the serializer should use for a particular
|
|||||||
has_many :comments, key: :reviews
|
has_many :comments, key: :reviews
|
||||||
```
|
```
|
||||||
|
|
||||||
The `url` declaration describes which named routes to use while generating URLs
|
|
||||||
for your JSON. Not every adapter will require URLs.
|
|
||||||
## Pagination
|
## Pagination
|
||||||
|
|
||||||
Pagination links will be included in your response automatically as long as the resource is paginated using [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate) and if you are using a ```JSON-API``` adapter.
|
Pagination links will be included in your response automatically as long as the resource is paginated using [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate) and if you are using a ```JSON-API``` adapter.
|
||||||
@ -307,8 +297,6 @@ class PostSerializer < ActiveModel::Serializer
|
|||||||
attributes :title, :body
|
attributes :title, :body
|
||||||
|
|
||||||
has_many :comments
|
has_many :comments
|
||||||
|
|
||||||
url :post
|
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -332,8 +320,6 @@ class PostSerializer < ActiveModel::Serializer
|
|||||||
attributes :title, :body
|
attributes :title, :body
|
||||||
|
|
||||||
has_many :comments
|
has_many :comments
|
||||||
|
|
||||||
url :post
|
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@ -31,7 +31,6 @@ module ActiveModel
|
|||||||
class << self
|
class << self
|
||||||
attr_accessor :_attributes
|
attr_accessor :_attributes
|
||||||
attr_accessor :_attributes_keys
|
attr_accessor :_attributes_keys
|
||||||
attr_accessor :_urls
|
|
||||||
attr_accessor :_cache
|
attr_accessor :_cache
|
||||||
attr_accessor :_fragmented
|
attr_accessor :_fragmented
|
||||||
attr_accessor :_cache_key
|
attr_accessor :_cache_key
|
||||||
@ -44,7 +43,6 @@ module ActiveModel
|
|||||||
def self.inherited(base)
|
def self.inherited(base)
|
||||||
base._attributes = self._attributes.try(:dup) || []
|
base._attributes = self._attributes.try(:dup) || []
|
||||||
base._attributes_keys = self._attributes_keys.try(:dup) || {}
|
base._attributes_keys = self._attributes_keys.try(:dup) || {}
|
||||||
base._urls = []
|
|
||||||
base._cache_digest = digest_caller_file(caller.first)
|
base._cache_digest = digest_caller_file(caller.first)
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
@ -86,14 +84,6 @@ module ActiveModel
|
|||||||
@_cache_options = (options.empty?) ? nil : options
|
@_cache_options = (options.empty?) ? nil : options
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.url(attr)
|
|
||||||
@_urls.push attr
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.urls(*attrs)
|
|
||||||
@_urls.concat attrs
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.serializer_for(resource, options = {})
|
def self.serializer_for(resource, options = {})
|
||||||
if resource.respond_to?(:serializer_class)
|
if resource.respond_to?(:serializer_class)
|
||||||
resource.serializer_class
|
resource.serializer_class
|
||||||
|
|||||||
1
test/fixtures/active_record.rb
vendored
1
test/fixtures/active_record.rb
vendored
@ -40,7 +40,6 @@ module ARModels
|
|||||||
|
|
||||||
has_many :comments
|
has_many :comments
|
||||||
belongs_to :author
|
belongs_to :author
|
||||||
url :comments
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class CommentSerializer < ActiveModel::Serializer
|
class CommentSerializer < ActiveModel::Serializer
|
||||||
|
|||||||
5
test/fixtures/poro.rb
vendored
5
test/fixtures/poro.rb
vendored
@ -59,8 +59,6 @@ end
|
|||||||
class ProfileSerializer < ActiveModel::Serializer
|
class ProfileSerializer < ActiveModel::Serializer
|
||||||
attributes :name, :description
|
attributes :name, :description
|
||||||
|
|
||||||
urls :posts, :comments
|
|
||||||
|
|
||||||
def arguments_passed_in?
|
def arguments_passed_in?
|
||||||
options[:my_options] == :accessible
|
options[:my_options] == :accessible
|
||||||
end
|
end
|
||||||
@ -68,8 +66,6 @@ end
|
|||||||
|
|
||||||
class ProfilePreviewSerializer < ActiveModel::Serializer
|
class ProfilePreviewSerializer < ActiveModel::Serializer
|
||||||
attributes :name
|
attributes :name
|
||||||
|
|
||||||
urls :posts, :comments
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Post = Class.new(Model)
|
Post = Class.new(Model)
|
||||||
@ -100,7 +96,6 @@ PostSerializer = Class.new(ActiveModel::Serializer) do
|
|||||||
has_many :comments
|
has_many :comments
|
||||||
belongs_to :blog
|
belongs_to :blog
|
||||||
belongs_to :author
|
belongs_to :author
|
||||||
url :comments
|
|
||||||
|
|
||||||
def blog
|
def blog
|
||||||
Blog.new(id: 999, name: 'Custom blog')
|
Blog.new(id: 999, name: 'Custom blog')
|
||||||
|
|||||||
@ -1,25 +0,0 @@
|
|||||||
require 'test_helper'
|
|
||||||
|
|
||||||
module ActiveModel
|
|
||||||
class Serializer
|
|
||||||
class UrlsTest < Minitest::Test
|
|
||||||
def setup
|
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
|
||||||
@post = Post.new({ title: 'New Post', body: 'Body' })
|
|
||||||
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
|
||||||
@post.comments = [@comment]
|
|
||||||
|
|
||||||
@profile_serializer = ProfileSerializer.new(@profile)
|
|
||||||
@post_serializer = PostSerializer.new(@post)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_urls_definition
|
|
||||||
assert_equal([:posts, :comments], @profile_serializer.class._urls)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_url_definition
|
|
||||||
assert_equal([:comments], @post_serializer.class._urls)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
Loading…
Reference in New Issue
Block a user