2017-05-23 5 views
0

JasonApi 자원 보석을 사용하여 링크 :제거 나는 다음과 같은 클래스와 간단한 모델이

class Country < ApplicationRecord 
    has_many :country_categories 
    has_many :categories, through: :country_categories 
end 

class CountryCategory < ApplicationRecord 
    belongs_to :country 
    belongs_to :category 
end 

class Category < ApplicationRecord 
    has_many :country_categories 
    has_many :countries, through: :country_categories 
end 

및 자원 : 나는 /countries/1?include=country_categories.categoryGET 요청을 수행 할 때

class CountryResource < JSONAPI::Resource 
    has_many :country_categories 
end 

는 응답이 항상을 제공합니다 많은 링크.

{ 
 
    "data": { 
 
    "id": "31", 
 
    "type": "countries", 
 
    "attributes": { 
 
     "description": null 
 
    }, 
 
    "relationships": { 
 
     "country-categories": { 
 
     "links": { 
 
      "self": "http://api.localhost.local:3000/api/v1/countries/31/relationships/country-categories", 
 
      "related": "http://api.localhost.local:3000/api/v1/countries/31/country-categories" 
 
     }, 
 
     "data": [ 
 
      { 
 
      "type": "country-categories", 
 
      "id": "129" 
 
      }, 
 
      { 
 
      "type": "country-categories", 
 
      "id": "130" 
 
      } 
 
     ] 
 
     }, 
 
    } 
 
    }, 
 
    "included": [ 
 
    { 
 
     "id": "129", 
 
     "type": "country-categories", 
 
     "attributes": { 
 
     "stuff": "aaa" 
 
     }, 
 
     "relationships": { 
 
     "country-subcategories": { 
 
      "links": { 
 
      "self": "http://api.localhost.local:3000/api/v1/country-categories/129/relationships/country-subcategories", 
 
      "related": "http://api.localhost.local:3000/api/v1/country-categories/129/country-subcategories" 
 
      } 
 
     }, 
 
     "category": { 
 
      "links": { 
 
      "self": "http://api.localhost.local:3000/api/v1/country-categories/129/relationships/category", 
 
      "related": "http://api.localhost.local:3000/api/v1/country-categories/129/category" 
 
      }, 
 
      "data": { 
 
      "type": "categories", 
 
      "id": "1" 
 
      } 
 
     } 
 
     } 
 
    }, 
 
    { 
 
     "id": "130", 
 
     "type": "country-categories", 
 
     "attributes": { 
 
     "stuff": "sfasf" 
 
     }, 
 
     "relationships": { 
 
     "country-subcategories": { 
 
      "links": { 
 
      "self": "http://api.localhost.local:3000/api/v1/country-categories/130/relationships/country-subcategories", 
 
      "related": "http://api.localhost.local:3000/api/v1/country-categories/130/country-subcategories" 
 
      } 
 
     }, 
 
     "category": { 
 
      "links": { 
 
      "self": "http://api.localhost.local:3000/api/v1/country-categories/130/relationships/category", 
 
      "related": "http://api.localhost.local:3000/api/v1/country-categories/130/category" 
 
      }, 
 
      "data": { 
 
      "type": "categories", 
 
      "id": "3" 
 
      } 
 
     } 
 
     } 
 
    }, 
 
    { 
 
     "id": "1", 
 
     "type": "categories", 
 
     "links": { 
 
     "self": "http://api.localhost.local:3000/api/v1/categories/1" 
 
     }, 
 
     "attributes": { 
 
     "name": "Value extraction" 
 
     }, 
 
     "relationships": { 
 
     "subcategories": { 
 
      "links": { 
 
      "self": "http://api.localhost.local:3000/api/v1/categories/1/relationships/subcategories", 
 
      "related": "http://api.localhost.local:3000/api/v1/categories/1/subcategories" 
 
      } 
 
     } 
 
     } 
 
    }, 
 
    { 
 
     "id": "3", 
 
     "type": "categories", 
 
     "links": { 
 
     "self": "http://api.localhost.local:3000/api/v1/categories/3" 
 
     }, 
 
     "attributes": { 
 
     "name": "Enabling environment" 
 
     }, 
 
     "relationships": { 
 
     "subcategories": { 
 
      "links": { 
 
      "self": "http://api.localhost.local:3000/api/v1/categories/3/relationships/subcategories", 
 
      "related": "http://api.localhost.local:3000/api/v1/categories/3/subcategories" 
 
      } 
 
     } 
 
     } 
 
    } 
 
    ] 
 
}

예를

를 들어 이러한 모든 links는 사용하지 않을 수 있습니다. 내가 그들을 제거 할 수있는 방법이 있습니까? 고맙습니다.

+0

http://jsonapi-resources.com/v0.9/guide/serializer.html – max

+0

감사, 최대,하지만 많은 나에게 도움이되지 않습니다 :) 나는 이미 재정 의하여 자기의 링크를 교체했습니다 custom_links.self. 그러나 나는 관계 연결을 위해 동일한 것을하는 방법을 찾는 것처럼 보이지 않는다. – Tiago

답변

0

내가 지금까지 발견 한 해결책은 Resource Serializer를 원숭이 패치 (이 솔루션이 완벽하지는 않지만)로 해결했습니다.

module JSONAPI 
    class ResourceSerializer 

    def link_object_to_many(source, relationship, include_linkage) 
    include_linkage = include_linkage | relationship.always_include_linkage_data 
     link_object_hash = {} 
     link_object_hash[:links] = {} if relationship.always_include_linkage_data 
     link_object_hash[:links][:self] = self_link(source, relationship) if relationship.always_include_linkage_data 
     link_object_hash[:links][:related] = related_link(source, relationship) if relationship.always_include_linkage_data 
     link_object_hash[:data] = to_many_linkage(source, relationship) if include_linkage 
     link_object_hash 
    end 

    def link_object_to_one(source, relationship, include_linkage) 
     include_linkage = include_linkage | @always_include_to_one_linkage_data | relationship.always_include_linkage_data 
     link_object_hash = {} 
     link_object_hash[:links] = {} if relationship.always_include_linkage_data 
     link_object_hash[:links][:self] = self_link(source, relationship) if relationship.always_include_linkage_data 
     link_object_hash[:links][:related] = related_link(source, relationship) if relationship.always_include_linkage_data 
     link_object_hash[:data] = to_one_linkage(source, relationship) if include_linkage 
     link_object_hash 
    end 
    end 
end 
관련 문제