Skip to content

Commit 47ed229

Browse files
committed
Change syntax.
1 parent 26874cc commit 47ed229

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

lib/active_model/serializer.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class Serializer
3333
self._attributes ||= []
3434
class_attribute :_attributes_keys
3535
self._attributes_keys ||= {}
36+
class_attribute :_links_blocks
37+
self._links_blocks ||= {}
3638
serializer.class_attribute :_cache
3739
serializer.class_attribute :_fragmented
3840
serializer.class_attribute :_cache_key
@@ -45,6 +47,7 @@ class Serializer
4547
def self.inherited(base)
4648
base._attributes = _attributes.dup
4749
base._attributes_keys = _attributes_keys.dup
50+
base._links_blocks = _links_blocks.dup
4851
base._cache_digest = digest_caller_file(caller.first)
4952
super
5053
end
@@ -53,8 +56,8 @@ def self.type(type)
5356
self._type = type
5457
end
5558

56-
def self.links(&block)
57-
self._links_block = block
59+
def self.link(name, &block)
60+
_links_blocks[name] = block
5861
end
5962

6063
def self.attributes(*attrs)
@@ -143,9 +146,7 @@ def self.get_serializer_for(klass)
143146
end
144147
end
145148

146-
attr_accessor :object, :root, :scope, :links
147-
class_attribute :_type, instance_writer: false
148-
class_attribute :_links_block, instance_writer: false
149+
attr_accessor :object, :root, :scope
149150

150151
def initialize(object, options = {})
151152
self.object = object
@@ -159,19 +160,18 @@ def initialize(object, options = {})
159160
define_method scope_name, lambda { scope }
160161
end
161162
end
162-
163-
instance_eval(&_links_block) if _links_block
164-
end
165-
166-
def link(name, value)
167-
self.links ||= {}
168-
self.links[name] = value
169163
end
170164

171165
def json_key
172166
root || object.class.model_name.to_s.underscore
173167
end
174168

169+
def links
170+
self.class._links_blocks.each_with_object({}) do |(name, block), hash|
171+
hash[name] = instance_eval(&block)
172+
end
173+
end
174+
175175
def attributes
176176
attributes = self.class._attributes.dup
177177

test/adapter/json_api/links_test.rb

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
11
require 'test_helper'
22

3-
LinkPost = Class.new(Model)
4-
class LinkPostSerializer < ActiveModel::Serializer
5-
belongs_to :link_author
6-
end
7-
8-
LinkAuthor = Class.new(Model)
9-
class LinkAuthorSerializer < ActiveModel::Serializer
10-
links do
11-
link :self, href: "//example.com/link_author/#{object.id}"
12-
end
13-
end
14-
153
module ActiveModel
164
class Serializer
175
module Adapter
186
class JsonApi
197
class LinksTest < Minitest::Test
8+
LinkAuthor = Class.new(::Model)
9+
class LinkAuthorSerializer < ActiveModel::Serializer
10+
link :self do
11+
{ href: "//example.com/link_author/#{object.id}" }
12+
end
13+
end
14+
2015
def setup
16+
@post = Post.new(id: 1337, comments: [], author: nil)
2117
@author = LinkAuthor.new(id: 1337)
2218
end
2319

20+
def test_toplevel_links
21+
hash = ActiveModel::SerializableResource.new(
22+
@post,
23+
adapter: :json_api,
24+
links: {
25+
self: {
26+
href: '//example.com/posts'
27+
}
28+
}).serializable_hash
29+
expected = {
30+
self: {
31+
href: '//example.com/posts'
32+
}
33+
}
34+
assert_equal(expected, hash[:links])
35+
end
36+
2437
def test_resource_links
2538
hash = serializable(@author, adapter: :json_api).serializable_hash
2639
expected = {

0 commit comments

Comments
 (0)