2017-05-23 18 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

私が今までに見つけた解決策は、リソースシリアライザのサプリメントです(このソリューションは完璧ではありませんが)。

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 
関連する問題