Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add url option to assocation for jsonapi #840

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/action_controller/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def use_adapter?

@_serializer_opts[:scope] ||= serialization_scope
@_serializer_opts[:scope_name] = _serialization_scope

@_adapter_opts[:url_helper] = self
# omg hax
object = serializer.new(resource, @_serializer_opts)
adapter = ActiveModel::Serializer::Adapter.create(object, @_adapter_opts)
Expand Down
21 changes: 14 additions & 7 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def initialize(serializer, options = {})
super
serializer.root = true
@hash = {}
@url_helper = options[:url_helper]
@top = @options.fetch(:top) { @hash }

if fields = options.delete(:fields)
Expand Down Expand Up @@ -138,21 +139,27 @@ def serialized_object_type(serializer)
end
end

attr_reader :url_helper

def add_resource_links(attrs, serializer, options = {})
options[:add_linked] = options.fetch(:add_linked, true)

serializer.each_association do |name, association, opts|
attrs[:links] ||= {}

if association.respond_to?(:each)
add_links(attrs, name, association)
if opts[:url]
attrs[:links][name] = url_helper.url_for([serializer.object, name])
else
add_link(attrs, name, association)
end
if association.respond_to?(:each)
add_links(attrs, name, association)
else
add_link(attrs, name, association)
end

if @options[:embed] != :ids && options[:add_linked]
Array(association).each do |association|
add_linked(name, association)
if @options[:embed] != :ids && options[:add_linked]
Array(association).each do |association|
add_linked(name, association)
end
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions test/adapter/json_api/has_many_url_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'test_helper'

module ActionController
module Serialization
class JsonApiHasManyUrlTest < ActionController::TestCase
class MyController < ActionController::Base

def render_resource_with_url_association
@tag = Tag.new(id: 1)
@tag.posts = []
render json: @tag, adapter: :json_api
end
end

tests MyController

def test_render_resource_with_url_association
get :render_resource_with_url_association
response = JSON.parse(@response.body)
assert response.key? 'tags'
assert response['tags'].key? 'links'
assert response['tags']['links'].key? 'posts'
assert_equal 'http://test.host/tags/1/posts', response['tags']['links']['posts']
end
end
end
end
9 changes: 8 additions & 1 deletion test/fixtures/poro.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Model
extend ActiveModel::Naming
def initialize(hash={})
@attributes = hash
end
Expand All @@ -24,7 +25,7 @@ def id
end

def to_param
id
id.to_s
end

def method_missing(meth, *args)
Expand Down Expand Up @@ -63,6 +64,8 @@ class ProfilePreviewSerializer < ActiveModel::Serializer
Bio = Class.new(Model)
Blog = Class.new(Model)
Role = Class.new(Model)
Tag = Class.new(Model)

module Spam; end
Spam::UnrelatedLink = Class.new(Model)

Expand Down Expand Up @@ -167,6 +170,10 @@ def self.root_name
belongs_to :author, serializer: AuthorPreviewSerializer
end

TagSerializer = Class.new(ActiveModel::Serializer) do
has_many :posts, url: true
end

Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
attributes :id
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Foo < Rails::Application
module TestHelper
Routes = ActionDispatch::Routing::RouteSet.new
Routes.draw do
get 'tags/:tag_id/posts', to: 'application#index', as: 'tag_posts'
get ':controller(/:action(/:id))'
get ':controller(/:action)'
end
Expand Down