Skip to content

Commit 3a02a4d

Browse files
authored
Merge pull request #1132 from scottgonzalez/action-renaming
Rename controller actions that begin with `get_`
2 parents 19f4d7b + 71ad630 commit 3a02a4d

6 files changed

Lines changed: 47 additions & 35 deletions

File tree

lib/jsonapi/acts_as_resource_controller.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,26 @@ def destroy_relationship
5151
process_request
5252
end
5353

54-
def get_related_resource
54+
def show_related_resource
5555
process_request
5656
end
5757

58-
def get_related_resources
58+
def index_related_resources
5959
process_request
6060
end
6161

62+
def get_related_resource
63+
ActiveSupport::Deprecation.warn "In #{self.class.name} you exposed a `get_related_resource`"\
64+
" action. Please use `show_related_resource` instead."
65+
show_related_resource
66+
end
67+
68+
def get_related_resources
69+
ActiveSupport::Deprecation.warn "In #{self.class.name} you exposed a `get_related_resources`"\
70+
" action. Please use `index_related_resource` instead."
71+
index_related_resources
72+
end
73+
6274
def process_request
6375
@response_document = create_response_document
6476

lib/jsonapi/request_parser.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def each(_response_document)
3737

3838
def transactional?
3939
case params[:action]
40-
when 'index', 'get_related_resource', 'get_related_resources', 'show', 'show_relationship'
40+
when 'index', 'show_related_resource', 'index_related_resources', 'show', 'show_relationship'
4141
return false
4242
else
4343
return true
@@ -80,7 +80,7 @@ def setup_index_action(params, resource_klass)
8080
)
8181
end
8282

83-
def setup_get_related_resource_action(params, resource_klass)
83+
def setup_show_related_resource_action(params, resource_klass)
8484
source_klass = Resource.resource_klass_for(params.require(:source))
8585
source_id = source_klass.verify_key(params.require(source_klass._as_parent_key), @context)
8686

@@ -101,7 +101,7 @@ def setup_get_related_resource_action(params, resource_klass)
101101
)
102102
end
103103

104-
def setup_get_related_resources_action(params, resource_klass)
104+
def setup_index_related_resources_action(params, resource_klass)
105105
source_klass = Resource.resource_klass_for(params.require(:source))
106106
source_id = source_klass.verify_key(params.require(source_klass._as_parent_key), @context)
107107

lib/jsonapi/routing_ext.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def jsonapi_related_resource(*relationship)
221221

222222
match formatted_relationship_name, controller: options[:controller],
223223
relationship: relationship.name, source: resource_type_with_module_prefix(source._type),
224-
action: 'get_related_resource', via: [:get]
224+
action: 'show_related_resource', via: [:get]
225225
end
226226

227227
def jsonapi_related_resources(*relationship)
@@ -238,7 +238,7 @@ def jsonapi_related_resources(*relationship)
238238
match formatted_relationship_name,
239239
controller: options[:controller],
240240
relationship: relationship.name, source: resource_type_with_module_prefix(source._type),
241-
action: 'get_related_resources', via: [:get]
241+
action: 'index_related_resources', via: [:get]
242242
end
243243

244244
protected

test/controllers/controller_test.rb

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,26 +1951,26 @@ def test_show_to_one_relationship_nil
19511951
}
19521952
end
19531953

1954-
def test_get_related_resources_sorted
1955-
assert_cacheable_get :get_related_resources, params: {person_id: '1001', relationship: 'posts', source:'people', sort: 'title' }
1954+
def test_index_related_resources_sorted
1955+
assert_cacheable_get :index_related_resources, params: {person_id: '1001', relationship: 'posts', source:'people', sort: 'title' }
19561956
assert_response :success
19571957
assert_equal 'JR How To', json_response['data'][0]['attributes']['title']
19581958
assert_equal 'New post', json_response['data'][2]['attributes']['title']
1959-
assert_cacheable_get :get_related_resources, params: {person_id: '1001', relationship: 'posts', source:'people', sort: '-title' }
1959+
assert_cacheable_get :index_related_resources, params: {person_id: '1001', relationship: 'posts', source:'people', sort: '-title' }
19601960
assert_response :success
19611961
assert_equal 'New post', json_response['data'][0]['attributes']['title']
19621962
assert_equal 'JR How To', json_response['data'][2]['attributes']['title']
19631963
end
19641964

1965-
def test_get_related_resources_default_sorted
1966-
assert_cacheable_get :get_related_resources, params: {person_id: '1001', relationship: 'posts', source:'people'}
1965+
def test_index_related_resources_default_sorted
1966+
assert_cacheable_get :index_related_resources, params: {person_id: '1001', relationship: 'posts', source:'people'}
19671967
assert_response :success
19681968
assert_equal 'New post', json_response['data'][0]['attributes']['title']
19691969
assert_equal 'JR How To', json_response['data'][2]['attributes']['title']
19701970
end
19711971

1972-
def test_get_related_resources_has_many_filtered
1973-
assert_cacheable_get :get_related_resources, params: {person_id: '1001', relationship: 'posts', source:'people', filter: { title: 'JR How To' } }
1972+
def test_index_related_resources_has_many_filtered
1973+
assert_cacheable_get :index_related_resources, params: {person_id: '1001', relationship: 'posts', source:'people', filter: { title: 'JR How To' } }
19741974
assert_response :success
19751975
assert_equal 'JR How To', json_response['data'][0]['attributes']['title']
19761976
assert_equal 1, json_response['data'].size
@@ -2494,8 +2494,8 @@ def test_invalid_filter_value
24942494
assert_response :bad_request
24952495
end
24962496

2497-
def test_invalid_filter_value_for_get_related_resources
2498-
assert_cacheable_get :get_related_resources, params: {
2497+
def test_invalid_filter_value_for_index_related_resources
2498+
assert_cacheable_get :index_related_resources, params: {
24992499
hair_cut_id: 1,
25002500
relationship: 'people',
25012501
source: 'hair_cuts',
@@ -2513,11 +2513,11 @@ def test_valid_filter_value
25132513
assert_equal 'Joe Author', json_response['data'][0]['attributes']['name']
25142514
end
25152515

2516-
def test_get_related_resource_no_namespace
2516+
def test_show_related_resource_no_namespace
25172517
original_config = JSONAPI.configuration.dup
25182518
JSONAPI.configuration.json_key_format = :dasherized_key
25192519
JSONAPI.configuration.route_format = :underscored_key
2520-
assert_cacheable_get :get_related_resource, params: {post_id: '2', relationship: 'author', source:'posts'}
2520+
assert_cacheable_get :show_related_resource, params: {post_id: '2', relationship: 'author', source:'posts'}
25212521
assert_response :success
25222522

25232523
assert_hash_equals(
@@ -2579,19 +2579,19 @@ def test_get_related_resource_no_namespace
25792579
JSONAPI.configuration = original_config
25802580
end
25812581

2582-
def test_get_related_resource_includes
2582+
def test_show_related_resource_includes
25832583
original_config = JSONAPI.configuration.dup
25842584
JSONAPI.configuration.json_key_format = :dasherized_key
25852585
JSONAPI.configuration.route_format = :underscored_key
2586-
assert_cacheable_get :get_related_resource, params: {post_id: '2', relationship: 'author', source:'posts', include: 'posts'}
2586+
assert_cacheable_get :show_related_resource, params: {post_id: '2', relationship: 'author', source:'posts', include: 'posts'}
25872587
assert_response :success
25882588
assert_equal 'posts', json_response['included'][0]['type']
25892589
ensure
25902590
JSONAPI.configuration = original_config
25912591
end
25922592

2593-
def test_get_related_resource_nil
2594-
get :get_related_resource, params: {post_id: '17', relationship: 'author', source:'posts'}
2593+
def test_show_related_resource_nil
2594+
get :show_related_resource, params: {post_id: '17', relationship: 'author', source:'posts'}
25952595
assert_response :success
25962596
assert_hash_equals json_response,
25972597
{
@@ -3390,10 +3390,10 @@ def test_books_delete_approved_comment_limited_user_using_relation_name_reflecte
33903390
book_comment.delete
33913391
end
33923392

3393-
def test_get_related_resources_pagination
3393+
def test_index_related_resources_pagination
33943394
Api::V2::BookResource.paginator :offset
33953395

3396-
assert_cacheable_get :get_related_resources, params: {author_id: '1003', relationship: 'books', source:'api/v2/authors'}
3396+
assert_cacheable_get :index_related_resources, params: {author_id: '1003', relationship: 'books', source:'api/v2/authors'}
33973397
assert_response :success
33983398
assert_equal 10, json_response['data'].size
33993399
assert_equal 3, json_response['links'].size
@@ -3525,8 +3525,8 @@ def test_save_model_callbacks_fail
35253525
end
35263526

35273527
class Api::V1::MoonsControllerTest < ActionController::TestCase
3528-
def test_get_related_resource
3529-
assert_cacheable_get :get_related_resource, params: {crater_id: 'S56D', relationship: 'moon', source: "api/v1/craters"}
3528+
def test_show_related_resource
3529+
assert_cacheable_get :show_related_resource, params: {crater_id: 'S56D', relationship: 'moon', source: "api/v1/craters"}
35303530
assert_response :success
35313531
assert_hash_equals({
35323532
data: {
@@ -3541,12 +3541,12 @@ def test_get_related_resource
35413541
}, json_response)
35423542
end
35433543

3544-
def test_get_related_resources_with_select_some_db_columns
3544+
def test_index_related_resources_with_select_some_db_columns
35453545
Api::V1::MoonResource.paginator :paged
35463546
original_config = JSONAPI.configuration.dup
35473547
JSONAPI.configuration.top_level_meta_include_record_count = true
35483548
JSONAPI.configuration.json_key_format = :dasherized_key
3549-
assert_cacheable_get :get_related_resources, params: {planet_id: '1', relationship: 'moons', source: 'api/v1/planets'}
3549+
assert_cacheable_get :index_related_resources, params: {planet_id: '1', relationship: 'moons', source: 'api/v1/planets'}
35503550
assert_response :success
35513551
assert_equal 1, json_response['meta']['record-count']
35523552
ensure
@@ -3564,8 +3564,8 @@ def test_show_single
35643564
assert_nil json_response['included']
35653565
end
35663566

3567-
def test_get_related_resources
3568-
assert_cacheable_get :get_related_resources, params: {moon_id: '1', relationship: 'craters', source: "api/v1/moons"}
3567+
def test_index_related_resources
3568+
assert_cacheable_get :index_related_resources, params: {moon_id: '1', relationship: 'craters', source: "api/v1/moons"}
35693569
assert_response :success
35703570
assert_hash_equals({
35713571
data: [
@@ -3587,9 +3587,9 @@ def test_get_related_resources
35873587
}, json_response)
35883588
end
35893589

3590-
def test_get_related_resources_filtered
3590+
def test_index_related_resources_filtered
35913591
$test_user = Person.find(1001)
3592-
assert_cacheable_get :get_related_resources,
3592+
assert_cacheable_get :index_related_resources,
35933593
params: {
35943594
moon_id: '1',
35953595
relationship: 'craters',

test/integration/routes/routes_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_routing_uuid
7878
# Polymorphic
7979
# ToDo: refute this routing. Polymorphic relationships can't support a shared set of filters or includes so
8080
# this this route is no longer supported
81-
# def test_routing_polymorphic_get_related_resource
81+
# def test_routing_polymorphic_show_related_resource
8282
# assert_routing(
8383
# {
8484
# path: '/pictures/1/imageable',
@@ -88,7 +88,7 @@ def test_routing_uuid
8888
# relationship: 'imageable',
8989
# source: 'pictures',
9090
# controller: 'imageables',
91-
# action: 'get_related_resource',
91+
# action: 'show_related_resource',
9292
# picture_id: '1'
9393
# }
9494
# )

test/unit/serializer/polymorphic_serializer_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@
346346
# )
347347
# end
348348
#
349-
# def test_polymorphic_get_related_resource
349+
# def test_polymorphic_show_related_resource
350350
# get '/pictures/1/imageable', headers: { 'Accept' => JSONAPI::MEDIA_TYPE }
351351
# serialized_data = JSON.parse(response.body)
352352
# assert_hash_equals(

0 commit comments

Comments
 (0)