Skip to content

Commit 6ebf986

Browse files
committed
Merge pull request #167 from togglepro/jsonapi_relationships
Extract jsonapi_relationships method for nested routes in block.
2 parents b4d4faa + 586ca61 commit 6ebf986

5 files changed

Lines changed: 41 additions & 32 deletions

File tree

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,15 @@ edit_contact GET /contacts/:id/edit(.:format) contacts#edit
781781
```
782782

783783
To manually add in the nested routes you can use the `jsonapi_links`, `jsonapi_related_resources` and
784-
`jsonapi_related_resource` inside the block.
784+
`jsonapi_related_resource` inside the block. Or, you can add the default set of nested routes using the `jsonapi_relationships` method. For example:
785+
786+
```ruby
787+
Rails.application.routes.draw do
788+
jsonapi_resources :contacts do
789+
jsonapi_relationships
790+
end
791+
end
792+
```
785793

786794
###### `jsonapi_links`
787795

lib/jsonapi/request.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def parse_single_replace_operation(data, keys)
400400
end
401401

402402
type = data[:type]
403-
if type.nil? || type != format_key(@resource_klass._type).to_s
403+
if type.nil? || type != @resource_klass._type.to_s
404404
raise JSONAPI::Exceptions::ParameterMissing.new(:type)
405405
end
406406

lib/jsonapi/routing_ext.rb

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,59 +18,58 @@ def format_route(route)
1818
end
1919

2020
def jsonapi_resource(*resources, &block)
21-
resource_type = resources.first
22-
res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(resources.first))
21+
@resource_type = resources.first
22+
res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(@resource_type))
2323

2424
options = resources.extract_options!.dup
25-
options[:controller] ||= resource_type
25+
options[:controller] ||= @resource_type
2626
options.merge!(res.routing_resource_options)
27-
options[:path] = format_route(resource_type)
27+
options[:path] = format_route(@resource_type)
2828

29-
resource resource_type, options do
30-
@scope[:jsonapi_resource] = resource_type
29+
resource @resource_type, options do
30+
@scope[:jsonapi_resource] = @resource_type
3131

3232
if block_given?
3333
yield
3434
else
35-
res._associations.each do |association_name, association|
36-
if association.is_a?(JSONAPI::Association::HasMany)
37-
jsonapi_links(association_name)
38-
else
39-
jsonapi_link(association_name)
40-
end
41-
end
35+
jsonapi_relationships
36+
end
37+
end
38+
end
39+
40+
def jsonapi_relationships
41+
res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(@resource_type))
42+
res._associations.each do |association_name, association|
43+
if association.is_a?(JSONAPI::Association::HasMany)
44+
jsonapi_links(association_name)
45+
jsonapi_related_resources(association_name)
46+
else
47+
jsonapi_link(association_name)
48+
jsonapi_related_resource(association_name)
4249
end
4350
end
4451
end
4552

4653
def jsonapi_resources(*resources, &block)
47-
resource_type = resources.first
48-
res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(resources.first))
54+
@resource_type = resources.first
55+
res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(@resource_type))
4956

5057
options = resources.extract_options!.dup
51-
options[:controller] ||= resource_type
58+
options[:controller] ||= @resource_type
5259
options.merge!(res.routing_resource_options)
5360

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

57-
options[:path] = format_route(resource_type)
64+
options[:path] = format_route(@resource_type)
5865

59-
resources resource_type, options do
60-
@scope[:jsonapi_resource] = resource_type
66+
resources @resource_type, options do
67+
@scope[:jsonapi_resource] = @resource_type
6168

6269
if block_given?
6370
yield
6471
else
65-
res._associations.each do |association_name, association|
66-
if association.is_a?(JSONAPI::Association::HasMany)
67-
jsonapi_links(association_name)
68-
jsonapi_related_resources(association_name)
69-
else
70-
jsonapi_link(association_name)
71-
jsonapi_related_resource(association_name)
72-
end
73-
end
72+
jsonapi_relationships
7473
end
7574
end
7675
end

test/fixtures/active_record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,4 +755,4 @@ class BadlyNamedAttributesResource < JSONAPI::Resource
755755
betax = Planet.create(name: 'Beta X', description: 'Newly discovered Planet X', planet_type_id: unknown.id)
756756
betay = Planet.create(name: 'Beta X', description: 'Newly discovered Planet Y', planet_type_id: unknown.id)
757757
betaz = Planet.create(name: 'Beta X', description: 'Newly discovered Planet Z', planet_type_id: unknown.id)
758-
betaw = Planet.create(name: 'Beta W', description: 'Newly discovered Planet W')
758+
betaw = Planet.create(name: 'Beta W', description: 'Newly discovered Planet W')

test/test_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ def as_json(options = nil)
6666
jsonapi_resources :people
6767
jsonapi_resources :comments
6868
jsonapi_resources :tags
69-
jsonapi_resources :posts
69+
jsonapi_resources :posts do
70+
jsonapi_relationships
71+
end
7072
jsonapi_resources :sections
7173
jsonapi_resources :iso_currencies
7274
jsonapi_resources :expense_entries

0 commit comments

Comments
 (0)