Jsonapi-resources: Support for models with namespaces

Created on 19 Feb 2015  路  6Comments  路  Source: cerebris/jsonapi-resources

Am I missing something or is it not possible to specify a model with a namespace for a given Resource?

For example, how would I create a resource for Test::User?

Most helpful comment

Still having issue with relating resource with namespace. Anyone have a solution regarding this one?

All 6 comments

I see it now... sorry.

For anyone wondering, it would be like this:

class UserResource < JSONAPI::Resource
  model_name 'Test::User'
  ...
end

if you have namespaced models how can the type be prefixed? (eg. test-user)

I'm not sure I'm following. I hope this helps. You can specify the model for the resource as shown above. You could create a resource named TestUserResource and if you set the formatting rules to use dasherized keys it will come out as test-user.

Ah ok thanks I think I completely missed a point. I thought that the type gets extracted from the model and not the resource name.

I'm having trouble getting this to work properly. Over half of our models are namespaced and I'm experiencing inconsistencies. See this example:

/api/project_resource.rb

class API::ProjectResource
  has_many :people
end

/api/project/people_resource.rb

class API::Project::PeopleResource
  has_one :main_product, class_name: 'Product'
  has_one :secondary_product, class_name: 'Product'
end

/api/product_resource.rb

class API::ProductResource
end

1) In order for it to find the resource for one of our models it must be in a sub directory (which I prefer) as you can see from the /api/project/ directory

2) Sometimes it's looking for the product_resource under /api/project and sometimes it's not. The easiest way for me to replicate it is to include the main_product and then right afterward change it to include the secondary product. It usually fails right then with this error

JSONAPI: Could not find resource 'api/project/Product'. (Class API::Project::ProductResource not found)

Still having issue with relating resource with namespace. Anyone have a solution regarding this one?

Was this page helpful?
0 / 5 - 0 ratings