Merge pull request #1406 from beauby/dynamic-jsonapi-string-links

Add support for custom dynamic valued links in JsonApi adapter.
This commit is contained in:
Lucas Hosseini 2016-01-15 14:57:12 +01:00
commit b9f4720cbd
4 changed files with 25 additions and 17 deletions

View File

@ -16,6 +16,7 @@ Breaking changes:
Features: Features:
- [#1406](https://github.com/rails-api/active_model_serializers/pull/1406) Allow for custom dynamic values in JSON API links (@beauby)
- [#1270](https://github.com/rails-api/active_model_serializers/pull/1270) Adds `assert_response_schema` test helper (@maurogeorge) - [#1270](https://github.com/rails-api/active_model_serializers/pull/1270) Adds `assert_response_schema` test helper (@maurogeorge)
- [#1099](https://github.com/rails-api/active_model_serializers/pull/1099) Adds `assert_serializer` test helper (@maurogeorge) - [#1099](https://github.com/rails-api/active_model_serializers/pull/1099) Adds `assert_serializer` test helper (@maurogeorge)
- [#1403](https://github.com/rails-api/active_model_serializers/pull/1403) Add support for if/unless on attributes/associations (@beauby) - [#1403](https://github.com/rails-api/active_model_serializers/pull/1403) Add support for if/unless on attributes/associations (@beauby)

View File

@ -210,15 +210,7 @@ module ActiveModel
def links_for(serializer) def links_for(serializer)
serializer._links.each_with_object({}) do |(name, value), hash| serializer._links.each_with_object({}) do |(name, value), hash|
hash[name] = hash[name] = Link.new(serializer, value).as_json
if value.respond_to?(:call)
link = Link.new(serializer)
link.instance_eval(&value)
link.to_hash
else
value
end
end end
end end

View File

@ -3,29 +3,39 @@ module ActiveModel
module Adapter module Adapter
class JsonApi class JsonApi
class Link class Link
def initialize(serializer) def initialize(serializer, value)
@object = serializer.object @object = serializer.object
@scope = serializer.scope @scope = serializer.scope
# Use the return value of the block unless it is nil.
if value.respond_to?(:call)
@value = instance_eval(&value)
else
@value = value
end
end end
def href(value) def href(value)
self._href = value @href = value
nil
end end
def meta(value) def meta(value)
self._meta = value @meta = value
nil
end end
def to_hash def as_json
hash = { href: _href } return @value if @value
hash.merge!(meta: _meta) if _meta
hash = { href: @href }
hash.merge!(meta: @meta) if @meta
hash hash
end end
protected protected
attr_accessor :_href, :_meta
attr_reader :object, :scope attr_reader :object, :scope
end end
end end

View File

@ -13,6 +13,10 @@ module ActiveModel
end end
link :other, '//example.com/resource' link :other, '//example.com/resource'
link :yet_another do
"//example.com/resource/#{object.id}"
end
end end
def setup def setup
@ -52,7 +56,8 @@ module ActiveModel
stuff: 'value' stuff: 'value'
} }
}, },
other: '//example.com/resource' other: '//example.com/resource',
yet_another: '//example.com/resource/1337'
} }
assert_equal(expected, hash[:data][:links]) assert_equal(expected, hash[:data][:links])
end end