Skip to content
Merged
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,15 @@ edit_contact GET /contacts/:id/edit(.:format) contacts#edit
```

To manually add in the nested routes you can use the `jsonapi_links`, `jsonapi_related_resources` and
`jsonapi_related_resource` inside the block.
`jsonapi_related_resource` inside the block. Or, you can add the default set of nested routes using the `jsonapi_relationships` method. For example:

```ruby
Rails.application.routes.draw do
jsonapi_resources :contacts do
jsonapi_relationships
end
end
```

###### `jsonapi_links`

Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def parse_single_replace_operation(data, keys)
end

type = data[:type]
if type.nil? || type != format_key(@resource_klass._type).to_s
if type.nil? || type != @resource_klass._type.to_s
raise JSONAPI::Exceptions::ParameterMissing.new(:type)
end

Expand Down
55 changes: 27 additions & 28 deletions lib/jsonapi/routing_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,58 @@ def format_route(route)
end

def jsonapi_resource(*resources, &block)
resource_type = resources.first
res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(resources.first))
@resource_type = resources.first
res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(@resource_type))

options = resources.extract_options!.dup
options[:controller] ||= resource_type
options[:controller] ||= @resource_type
options.merge!(res.routing_resource_options)
options[:path] = format_route(resource_type)
options[:path] = format_route(@resource_type)

resource resource_type, options do
@scope[:jsonapi_resource] = resource_type
resource @resource_type, options do
@scope[:jsonapi_resource] = @resource_type

if block_given?
yield
else
res._associations.each do |association_name, association|
if association.is_a?(JSONAPI::Association::HasMany)
jsonapi_links(association_name)
else
jsonapi_link(association_name)
end
end
jsonapi_relationships
end
end
end

def jsonapi_relationships
res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(@resource_type))
res._associations.each do |association_name, association|
if association.is_a?(JSONAPI::Association::HasMany)
jsonapi_links(association_name)
jsonapi_related_resources(association_name)
else
jsonapi_link(association_name)
jsonapi_related_resource(association_name)
end
end
end

def jsonapi_resources(*resources, &block)
resource_type = resources.first
res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(resources.first))
@resource_type = resources.first
res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(@resource_type))

options = resources.extract_options!.dup
options[:controller] ||= resource_type
options[:controller] ||= @resource_type
options.merge!(res.routing_resource_options)

# Route using the primary_key. Can be overridden using routing_resource_options
options[:param] ||= res._primary_key

options[:path] = format_route(resource_type)
options[:path] = format_route(@resource_type)

resources resource_type, options do
@scope[:jsonapi_resource] = resource_type
resources @resource_type, options do
@scope[:jsonapi_resource] = @resource_type

if block_given?
yield
else
res._associations.each do |association_name, association|
if association.is_a?(JSONAPI::Association::HasMany)
jsonapi_links(association_name)
jsonapi_related_resources(association_name)
else
jsonapi_link(association_name)
jsonapi_related_resource(association_name)
end
end
jsonapi_relationships
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -755,4 +755,4 @@ class BadlyNamedAttributesResource < JSONAPI::Resource
betax = Planet.create(name: 'Beta X', description: 'Newly discovered Planet X', planet_type_id: unknown.id)
betay = Planet.create(name: 'Beta X', description: 'Newly discovered Planet Y', planet_type_id: unknown.id)
betaz = Planet.create(name: 'Beta X', description: 'Newly discovered Planet Z', planet_type_id: unknown.id)
betaw = Planet.create(name: 'Beta W', description: 'Newly discovered Planet W')
betaw = Planet.create(name: 'Beta W', description: 'Newly discovered Planet W')
4 changes: 3 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def as_json(options = nil)
jsonapi_resources :people
jsonapi_resources :comments
jsonapi_resources :tags
jsonapi_resources :posts
jsonapi_resources :posts do
jsonapi_relationships
end
jsonapi_resources :sections
jsonapi_resources :iso_currencies
jsonapi_resources :expense_entries
Expand Down