wor-simple_crud 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +32 -0
  3. data/Gemfile +1 -0
  4. data/README.md +157 -2
  5. data/lib/simple_crud/authorization/adapter.rb +7 -0
  6. data/lib/simple_crud/authorization/pundit_adapter.rb +6 -0
  7. data/lib/simple_crud/config.rb +6 -1
  8. data/lib/simple_crud/controller_helpers.rb +119 -0
  9. data/lib/simple_crud/pagination/adapter.rb +7 -1
  10. data/lib/simple_crud/pagination/kaminari_adapter.rb +23 -2
  11. data/lib/simple_crud/pagination/pagy_adapter.rb +7 -2
  12. data/lib/simple_crud/pagination/will_paginate_adapter.rb +7 -3
  13. data/lib/simple_crud/pagination/wor_paginate_adapter.rb +7 -2
  14. data/lib/simple_crud/rspec/config.rb +61 -0
  15. data/lib/simple_crud/rspec/helpers.rb +147 -0
  16. data/lib/simple_crud/rspec.rb +34 -0
  17. data/lib/simple_crud/simple_crud_controller.rb +51 -36
  18. data/lib/simple_crud/version.rb +1 -1
  19. data/lib/spec/shared_contexts/authenticate_user.rb +1 -7
  20. data/lib/spec/shared_examples/authorization_adapter_authorize.rb +11 -0
  21. data/lib/spec/shared_examples/simple_crud_for_create.rb +43 -11
  22. data/lib/spec/shared_examples/simple_crud_for_create_with_block.rb +18 -0
  23. data/lib/spec/shared_examples/simple_crud_for_create_with_build.rb +15 -0
  24. data/lib/spec/shared_examples/simple_crud_for_destroy.rb +19 -8
  25. data/lib/spec/shared_examples/simple_crud_for_destroy_with_block.rb +13 -0
  26. data/lib/spec/shared_examples/simple_crud_for_destroy_with_finder.rb +24 -0
  27. data/lib/spec/shared_examples/simple_crud_for_index.rb +8 -23
  28. data/lib/spec/shared_examples/simple_crud_for_index_with_block.rb +14 -0
  29. data/lib/spec/shared_examples/simple_crud_for_index_with_scope.rb +21 -0
  30. data/lib/spec/shared_examples/simple_crud_for_new.rb +29 -0
  31. data/lib/spec/shared_examples/simple_crud_for_new_with_block.rb +9 -0
  32. data/lib/spec/shared_examples/simple_crud_for_new_with_build.rb +13 -0
  33. data/lib/spec/shared_examples/simple_crud_for_show.rb +14 -10
  34. data/lib/spec/shared_examples/simple_crud_for_show_with_block.rb +11 -0
  35. data/lib/spec/shared_examples/simple_crud_for_show_with_finder.rb +24 -0
  36. data/lib/spec/shared_examples/simple_crud_for_update.rb +55 -15
  37. data/lib/spec/shared_examples/simple_crud_for_update_with_finder.rb +29 -0
  38. data/lib/spec/shared_examples/simple_crud_not_found_with_finder.rb +13 -0
  39. data/lib/spec/shared_examples/simple_crud_renders_template.rb +8 -0
  40. data/lib/spec/shared_examples/simple_crud_when_not_authorized.rb +18 -0
  41. data/lib/spec/shared_examples/simple_crud_without_authenticated_user.rb +9 -0
  42. data/lib/spec/shared_examples/unauthorized_when_not_logged_in.rb +2 -2
  43. data/spec/block/dummy_models_controller_spec.rb +8 -0
  44. data/spec/block_create/dummy_models_controller_spec.rb +7 -0
  45. data/spec/block_destroy/dummy_models_controller_spec.rb +7 -0
  46. data/spec/block_new/dummy_models_controller_spec.rb +7 -0
  47. data/spec/block_show/dummy_models_controller_spec.rb +7 -0
  48. data/spec/built/dummy_models_controller_spec.rb +8 -0
  49. data/spec/dummy/app/controllers/block/dummy_models_controller.rb +17 -0
  50. data/spec/dummy/app/controllers/block_create/dummy_models_controller.rb +13 -0
  51. data/spec/dummy/app/controllers/block_destroy/dummy_models_controller.rb +9 -0
  52. data/spec/dummy/app/controllers/block_modes/base_controller.rb +11 -0
  53. data/spec/dummy/app/controllers/block_new/dummy_models_controller.rb +9 -0
  54. data/spec/dummy/app/controllers/block_show/dummy_models_controller.rb +9 -0
  55. data/spec/dummy/app/controllers/built/dummy_models_controller.rb +21 -0
  56. data/spec/dummy/app/controllers/dummy_models_controller.rb +2 -1
  57. data/spec/dummy/app/controllers/finder/dummy_models_controller.rb +22 -0
  58. data/spec/dummy/app/controllers/html/dummy_models_controller.rb +7 -0
  59. data/spec/dummy/app/controllers/html_create/dummy_models_controller.rb +7 -0
  60. data/spec/dummy/app/controllers/html_destroy/dummy_models_controller.rb +7 -0
  61. data/spec/dummy/app/controllers/html_modes/base_controller.rb +17 -0
  62. data/spec/dummy/app/controllers/html_new/dummy_models_controller.rb +7 -0
  63. data/spec/dummy/app/controllers/html_scoped/dummy_models_controller.rb +8 -0
  64. data/spec/dummy/app/controllers/html_show/dummy_models_controller.rb +7 -0
  65. data/spec/dummy/app/controllers/html_update/dummy_models_controller.rb +7 -0
  66. data/spec/dummy/app/controllers/invalid_status/dummy_models_controller.rb +13 -0
  67. data/spec/dummy/app/controllers/nested/dummy_models_controller.rb +21 -0
  68. data/spec/dummy/app/controllers/nested_route/dummy_models_controller.rb +26 -0
  69. data/spec/dummy/app/controllers/redirect/dummy_models_controller.rb +10 -0
  70. data/spec/dummy/app/controllers/redirect_auth/dummy_models_controller.rb +28 -0
  71. data/spec/dummy/app/controllers/scoped/dummy_models_controller.rb +16 -0
  72. data/spec/dummy/app/controllers/scoped_params/dummy_models_controller.rb +16 -0
  73. data/spec/dummy/app/controllers/strict/dummy_models_controller.rb +21 -0
  74. data/spec/dummy/app/models/dummy_model.rb +4 -0
  75. data/spec/dummy/app/models/dummy_model_policy.rb +4 -0
  76. data/spec/dummy/app/views/block/dummy_models/index.html.erb +3 -0
  77. data/spec/dummy/app/views/block_new/dummy_models/new.html.erb +1 -0
  78. data/spec/dummy/app/views/block_show/dummy_models/show.html.erb +1 -0
  79. data/spec/dummy/app/views/html/dummy_models/index.html.erb +3 -0
  80. data/spec/dummy/app/views/html_create/dummy_models/new.html.erb +1 -0
  81. data/spec/dummy/app/views/html_destroy/dummy_models/show.html.erb +1 -0
  82. data/spec/dummy/app/views/html_new/dummy_models/new.html.erb +1 -0
  83. data/spec/dummy/app/views/html_scoped/dummy_models/index.html.erb +3 -0
  84. data/spec/dummy/app/views/html_show/dummy_models/show.html.erb +1 -0
  85. data/spec/dummy/app/views/html_update/dummy_models/edit.html.erb +1 -0
  86. data/spec/dummy/app/views/invalid_status/dummy_models/new.html.erb +1 -0
  87. data/spec/dummy/app/views/layouts/application.html.erb +4 -0
  88. data/spec/dummy/app/views/redirect/dummy_models/new.html.erb +1 -0
  89. data/spec/dummy/config/routes.rb +90 -0
  90. data/spec/dummy/db/migrate/20260806000000_add_slug_to_dummy_models.rb +7 -0
  91. data/spec/dummy/db/migrate/20260807000000_add_classroom_slug_to_dummy_models.rb +7 -0
  92. data/spec/dummy/db/schema.rb +3 -1
  93. data/spec/dummy/spec/factories/dummy_models.rb +1 -0
  94. data/spec/dummy_controller_spec.rb +1 -0
  95. data/spec/dummy_model_policy_spec.rb +8 -16
  96. data/spec/finder/dummy_models_controller_spec.rb +12 -0
  97. data/spec/html/dummy_models_controller_spec.rb +7 -0
  98. data/spec/html_create/dummy_models_controller_spec.rb +7 -0
  99. data/spec/html_destroy/dummy_models_controller_spec.rb +7 -0
  100. data/spec/html_new/dummy_models_controller_spec.rb +7 -0
  101. data/spec/html_scoped/dummy_models_controller_spec.rb +7 -0
  102. data/spec/html_show/dummy_models_controller_spec.rb +7 -0
  103. data/spec/html_update/dummy_models_controller_spec.rb +7 -0
  104. data/spec/invalid_status/dummy_models_controller_spec.rb +10 -0
  105. data/spec/nested/dummy_models_controller_spec.rb +12 -0
  106. data/spec/nested_route/dummy_models_controller_spec.rb +19 -0
  107. data/spec/redirect/dummy_models_controller_spec.rb +23 -0
  108. data/spec/redirect_auth/dummy_models_controller_spec.rb +11 -0
  109. data/spec/scoped/dummy_models_controller_spec.rb +7 -0
  110. data/spec/scoped_params/dummy_models_controller_spec.rb +11 -0
  111. data/spec/simple_crud/authorization/action_policy_adapter_spec.rb +1 -9
  112. data/spec/simple_crud/authorization/can_can_can_adapter_spec.rb +1 -7
  113. data/spec/simple_crud/authorization/pundit_adapter_spec.rb +41 -0
  114. data/spec/simple_crud/controller_helpers_spec.rb +49 -0
  115. data/spec/simple_crud/pagination/adapters_spec.rb +37 -13
  116. data/spec/simple_crud/rspec_config_spec.rb +67 -0
  117. data/spec/simple_crud_configuration_spec.rb +19 -16
  118. data/spec/simple_crud_controller_spec.rb +4 -4
  119. data/spec/spec_helper.rb +1 -0
  120. data/spec/strict/dummy_models_controller_spec.rb +23 -0
  121. metadata +87 -4
  122. data/lib/spec/shared_examples/helpers.rb +0 -50
@@ -2,39 +2,24 @@
2
2
 
3
3
  shared_examples 'simple crud for index' do
4
4
  describe 'GET #index' do
5
- let(:created_models) { create_list(model_class, 10) }
5
+ let(:created_models) { create_records(model_class, 10, model_attributes) }
6
6
 
7
7
  before { created_models }
8
8
 
9
- context 'without authenticated user' do
10
- subject!(:req) { get :index }
11
-
12
- include_examples 'unauthorized when not logged in' if check_authenticate(:index)
13
- end
14
-
15
- if check_authorize(:index)
16
- context 'when not authorized' do
17
- include_context 'with authenticated user' if check_authenticate(:index)
18
-
19
- before do
20
- make_policies_fail(:index)
21
- get :index
22
- end
23
-
24
- it 'fails with forbidden' do
25
- expect(response).to have_http_status(:forbidden)
26
- end
27
- end
28
- end
9
+ include_examples 'simple crud without authenticated user', :index
10
+ include_examples 'simple crud when not authorized', :index, :get
29
11
 
30
12
  context 'with authenticated user' do
31
13
  include_context 'with authenticated user' if check_authenticate(:index)
14
+
32
15
  before do
33
16
  make_policies_succeed(:index)
34
- get :index
17
+ get :index, params: with_route_params({}), format: request_format(:index)
35
18
  end
36
19
 
37
- if check_paginate(:index)
20
+ if check_html(:index)
21
+ include_examples 'simple crud renders template', :index
22
+ elsif check_paginate(:index)
38
23
  it 'renders paginated models correctly serialized' do
39
24
  expect(response_body['page']).to have_been_serialized_with(model_serializer)
40
25
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'simple crud for index with block' do
4
+ describe 'GET #index with a render block' do
5
+ let(:created_models) { create_records(model_class, 2, {}) }
6
+
7
+ before do
8
+ created_models
9
+ get :index, params: with_route_params({}), format: :html
10
+ end
11
+
12
+ include_examples 'simple crud renders template', :index
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'simple crud for index with scope' do
4
+ let(:user) { current_user }
5
+ let(:other_user) { resolve(config.other_user) }
6
+ let(:my_models) { create_records(model_class, 2, owner_association => user) }
7
+ let(:other_models) { create_records(model_class, 3, owner_association => other_user) }
8
+ let(:request_params) { {} }
9
+
10
+ before do
11
+ authenticate_request
12
+ make_policies_succeed(:index)
13
+ my_models
14
+ other_models
15
+ get :index, params: with_route_params(request_params)
16
+ end
17
+
18
+ it 'returns only the scoped records' do
19
+ expect(response_body['page'].map { |m| m['id'] }).to match_array(my_models.map(&:id))
20
+ end
21
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'simple crud for new' do
4
+ describe 'GET #new' do
5
+ include_examples 'simple crud without authenticated user', :new
6
+ include_examples 'simple crud when not authorized', :new, :get
7
+
8
+ context 'when building a new record' do
9
+ include_context 'with authenticated user' if check_authenticate(:new)
10
+
11
+ before do
12
+ make_policies_succeed(:new)
13
+ get :new, params: with_route_params({}), format: request_format(:new)
14
+ end
15
+
16
+ it 'responds with ok status' do
17
+ expect(response).to have_http_status(:ok)
18
+ end
19
+
20
+ if check_html(:new)
21
+ include_examples 'simple crud renders template', :new
22
+ else
23
+ it 'returns a blank record as json' do
24
+ expect(response_body['id']).to be_nil
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'simple crud for new with block' do
4
+ describe 'GET #new with a render block' do
5
+ before { get :new, params: with_route_params({}), format: :html }
6
+
7
+ include_examples 'simple crud renders template', :new
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'simple crud for new with build' do
4
+ describe 'GET #new with a build hook' do
5
+ include_context 'with authenticated user'
6
+
7
+ before { get :new, params: with_route_params({}) }
8
+
9
+ it 'builds the record owned by the current user' do
10
+ expect(response_body[owner_foreign_key.to_s]).to eq(current_user.id)
11
+ end
12
+ end
13
+ end
@@ -2,21 +2,21 @@
2
2
 
3
3
  shared_examples 'simple crud for show' do
4
4
  describe 'GET #show' do
5
- subject(:show_request) { get :show, params: show_params }
5
+ subject(:show_request) { get :show, params: with_route_params(show_params), format: request_format(:show) }
6
6
 
7
7
  before do
8
8
  model
9
9
  end
10
10
 
11
11
  context 'without authenticated user' do
12
- subject!(:req) { get :show, params: { id: model.id } }
12
+ subject!(:req) { get :show, params: with_route_params(record_param(:show, model)), format: request_format(:show) }
13
13
 
14
- include_examples 'unauthorized when not logged in'
14
+ include_examples 'unauthorized when not logged in' if check_authenticate(:show)
15
15
  end
16
16
 
17
17
  describe 'when the model exists' do
18
18
  include_context 'with authenticated user'
19
- let(:show_params) { { id: model.id } }
19
+ let(:show_params) { record_param(:show, model) }
20
20
 
21
21
  before do
22
22
  show_request
@@ -26,18 +26,22 @@ shared_examples 'simple crud for show' do
26
26
  expect(response).to have_http_status(:ok)
27
27
  end
28
28
 
29
- it 'returns the asked model' do
30
- expect(response_body['id']).to eq(model.id)
31
- end
29
+ if check_html(:show)
30
+ include_examples 'simple crud renders template', :show
31
+ else
32
+ it 'returns the asked model' do
33
+ expect(response_body['id']).to eq(model.id)
34
+ end
32
35
 
33
- it 'have been serializer with model Serializer' do
34
- expect(response_body).to have_been_serialized_with(model_serializer)
36
+ it 'have been serializer with model Serializer' do
37
+ expect(response_body).to have_been_serialized_with(model_serializer)
38
+ end
35
39
  end
36
40
  end
37
41
 
38
42
  describe 'when the model does not exist' do
39
43
  include_context 'with authenticated user'
40
- let(:show_params) { { id: -1 } }
44
+ let(:show_params) { record_param(:show, nil, not_found: true) }
41
45
 
42
46
  before do
43
47
  show_request
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'simple crud for show with block' do
4
+ describe 'GET #show with a render block' do
5
+ let!(:record) { model }
6
+
7
+ before { get :show, params: with_route_params({ id: record.id }), format: :html }
8
+
9
+ include_examples 'simple crud renders template', :show
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'simple crud for show with finder' do
4
+ describe 'GET #show with a custom finder' do
5
+ include_context 'with authenticated user'
6
+
7
+ context 'when the model exists' do
8
+ before do
9
+ model
10
+ get :show, params: with_route_params(finder_key => model.public_send(finder_key))
11
+ end
12
+
13
+ it 'responds with ok status' do
14
+ expect(response).to have_http_status(:ok)
15
+ end
16
+
17
+ it 'returns the asked model' do
18
+ expect(response_body['id']).to eq(model.id)
19
+ end
20
+ end
21
+
22
+ include_examples 'simple crud not found with finder', :get, :show
23
+ end
24
+ end
@@ -3,9 +3,12 @@
3
3
  shared_examples 'simple crud for update' do
4
4
  describe 'PUT #update' do
5
5
  context 'without authenticated user' do
6
- subject!(:req) { put :update, params: attributes_for(model_class).merge(id: model.id) }
6
+ subject!(:req) do
7
+ update_params = body_params(params_for(model_class)).merge(record_param(:update, model))
8
+ put :update, params: with_route_params(update_params), format: request_format(:update)
9
+ end
7
10
 
8
- include_examples 'unauthorized when not logged in'
11
+ include_examples 'unauthorized when not logged in' if check_authenticate(:update)
9
12
  end
10
13
 
11
14
  if check_authorize(:update)
@@ -14,7 +17,8 @@ shared_examples 'simple crud for update' do
14
17
 
15
18
  before do
16
19
  make_policies_fail(:update)
17
- put :update, params: model_params.merge(id: model.id)
20
+ put :update, params: with_route_params(body_params(model_params).merge(record_param(:update, model))),
21
+ format: request_format(:update)
18
22
  end
19
23
 
20
24
  it 'fails with forbidden' do
@@ -24,32 +28,43 @@ shared_examples 'simple crud for update' do
24
28
  end
25
29
 
26
30
  context 'when successfully updating an model' do
27
- include_context 'with authenticated user'
31
+ include_context 'with authenticated user' if check_authenticate(:update)
32
+ let(:update_params) { body_params(model_params).merge(record_param(:update, model)) }
33
+ let(:update_attributes) { model_params.merge(record_param(:update, model).except(:id)) }
28
34
 
29
35
  before do
30
36
  model
31
- put :update, params: model_params.merge(id: model.id)
37
+ put :update, params: with_route_params(update_params), format: request_format(:update)
32
38
  end
33
39
 
34
- it 'response with 200 status code' do
35
- expect(response).to have_http_status(:ok)
36
- end
40
+ if check_html(:update)
41
+ it 'redirects to the updated record' do
42
+ expect(response).to have_http_status(:found)
43
+ end
44
+ else
45
+ it 'response with 200 status code' do
46
+ expect(response).to have_http_status(:ok)
47
+ end
37
48
 
38
- it 'updates an model' do
39
- expect(model_class_object.count).to be 1
40
- end
49
+ it 'updates an model' do
50
+ expect(model_class_object.count).to be 1
51
+ end
41
52
 
42
- it 'updates an model with valid attributes' do
43
- expect(model_class.classify.constantize.last).to have_attributes(model_params)
53
+ it 'updates an model with valid attributes' do
54
+ expect(model_class_object.last).to have_attributes(update_attributes)
55
+ end
44
56
  end
45
57
  end
46
58
 
47
59
  context 'when updating a model that doesn\'t exist' do
48
60
  include_context 'with authenticated user'
49
61
 
50
- before { put :update, params: { id: 1 } }
62
+ before do
63
+ put :update, params: with_route_params(record_param(:update, nil, not_found: true)),
64
+ format: request_format(:update)
65
+ end
51
66
 
52
- it 'response with 422 status code' do
67
+ it 'response with 404 status code' do
53
68
  expect(response).to have_http_status(:not_found)
54
69
  end
55
70
 
@@ -57,5 +72,30 @@ shared_examples 'simple crud for update' do
57
72
  expect(model_class_object.count).to be 0
58
73
  end
59
74
  end
75
+
76
+ unless check_raise_on_invalid(:update)
77
+ context 'when updating with invalid attributes' do
78
+ include_context 'with authenticated user' if check_authenticate(:update)
79
+
80
+ before do
81
+ model
82
+ put :update, params: with_route_params(body_params({ required_attribute => nil }.merge(owner_params))
83
+ .merge(record_param(:update, model))),
84
+ format: request_format(:update)
85
+ end
86
+
87
+ if check_html(:update)
88
+ include_examples 'simple crud renders template', :edit, -> { invalid_status }
89
+ else
90
+ it 'responds with unprocessable entity' do
91
+ expect(response).to have_http_status(unprocessable_status)
92
+ end
93
+
94
+ it 'returns the validation errors' do
95
+ expect(response_body['errors']).to include(required_error)
96
+ end
97
+ end
98
+ end
99
+ end
60
100
  end
61
101
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'simple crud for update with finder' do
4
+ describe 'PUT #update with a custom finder' do
5
+ include_context 'with authenticated user'
6
+
7
+ context 'when successfully updating a model' do
8
+ let(:update_params) do
9
+ with_route_params(body_params(required_attribute => 'Updated').merge(owner_params))
10
+ .merge(finder_key => model.public_send(finder_key))
11
+ end
12
+
13
+ before do
14
+ model
15
+ put :update, params: update_params
16
+ end
17
+
18
+ it 'responds with ok status' do
19
+ expect(response).to have_http_status(:ok)
20
+ end
21
+
22
+ it 'updates the model' do
23
+ expect(model.reload.public_send(required_attribute)).to eq('Updated')
24
+ end
25
+ end
26
+
27
+ include_examples 'simple crud not found with finder', :put, :update
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'simple crud not found with finder' do |http_method, action|
4
+ context 'when the model does not exist' do
5
+ before do
6
+ send(http_method, action, params: with_route_params(finder_key => "nonexistent-#{finder_key}"))
7
+ end
8
+
9
+ it 'responds with not found status' do
10
+ expect(response).to have_http_status(:not_found)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'simple crud renders template' do |template, status = :ok|
4
+ it "renders the #{template} template", :aggregate_failures do
5
+ expect(response).to have_http_status(resolve(status))
6
+ expect(response).to render_template(template) if assert_html_template
7
+ end
8
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'simple crud when not authorized' do |action, verb, request_params = {}|
4
+ if check_authorize(action)
5
+ context 'when not authorized' do
6
+ include_context 'with authenticated user' if check_authenticate(action)
7
+
8
+ before do
9
+ make_policies_fail(action)
10
+ send(verb, action, params: with_route_params(request_params))
11
+ end
12
+
13
+ it 'fails with forbidden' do
14
+ expect(response).to have_http_status(:forbidden)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'simple crud without authenticated user' do |action|
4
+ context 'without authenticated user' do
5
+ subject!(:req) { get action, params: with_route_params({}) }
6
+
7
+ include_examples 'unauthorized when not logged in' if check_authenticate(action)
8
+ end
9
+ end
@@ -2,9 +2,9 @@
2
2
 
3
3
  shared_examples 'unauthorized when not logged in' do
4
4
  context 'when the user is not logged in' do
5
- it 'returns status code unauthorized' do
5
+ it 'returns the unauthenticated status' do
6
6
  req
7
- expect(response).to have_http_status(:unauthorized)
7
+ expect(response).to have_http_status(unauthenticated_status)
8
8
  end
9
9
  end
10
10
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Block::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for index with block'
7
+ include_examples 'simple crud for index'
8
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe BlockCreate::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for create with block'
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe BlockDestroy::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for destroy with block'
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe BlockNew::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for new with block'
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe BlockShow::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for show with block'
7
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Built::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for new with build'
7
+ include_examples 'simple crud for create with build'
8
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Block
4
+ class DummyModelsController < ApplicationController
5
+ include Wor::Paginate
6
+ extend SimpleCrudController
7
+
8
+ before_action :set_params
9
+ def set_params
10
+ SimpleCrudController.params = params
11
+ end
12
+
13
+ simple_crud_for :index, paginate: false, authenticate: false, authorize: false do |records|
14
+ render :index, locals: { models: records }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlockCreate
4
+ class DummyModelsController < HtmlModes::BaseController
5
+ simple_crud_for :create, html: false, authenticate: false, authorize: false do |record, saved|
6
+ if saved
7
+ render json: { saved: true, id: record.id }
8
+ else
9
+ render json: { saved: false, errors: record.errors.full_messages }, status: 422
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlockDestroy
4
+ class DummyModelsController < HtmlModes::BaseController
5
+ simple_crud_for :destroy, html: false, authenticate: false, authorize: false do |_record, destroyed|
6
+ render json: { destroyed: destroyed ? true : false }
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlockModes
4
+ class BaseController < HtmlModes::BaseController
5
+ private
6
+
7
+ def render_model(record, template)
8
+ render template, locals: { model: record }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlockNew
4
+ class DummyModelsController < BlockModes::BaseController
5
+ simple_crud_for :new, authenticate: false, authorize: false do |record|
6
+ render_model(record, :new)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlockShow
4
+ class DummyModelsController < BlockModes::BaseController
5
+ simple_crud_for :show, authenticate: false, authorize: false do |record|
6
+ render_model(record, :show)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Built
4
+ class DummyModelsController < ApplicationController
5
+ include Wor::Paginate
6
+ include Pundit::Authorization
7
+ extend SimpleCrudController
8
+
9
+ before_action :set_params
10
+ def set_params
11
+ SimpleCrudController.params = params
12
+ end
13
+
14
+ def dummy_model_params
15
+ params.permit(:name, :something, :user_id, :slug)
16
+ end
17
+
18
+ simple_crud_for :new, build: -> { current_user.dummy_models.build }
19
+ simple_crud_for :create, build: -> { current_user.dummy_models.build }
20
+ end
21
+ end
@@ -12,12 +12,13 @@ class DummyModelsController < ApplicationController
12
12
  end
13
13
 
14
14
  def dummy_model_params
15
- params.permit(:name, :something, :user_id)
15
+ params.permit(:name, :something, :user_id, :slug)
16
16
  end
17
17
 
18
18
  simple_crud_for :create
19
19
  simple_crud_for :destroy
20
20
  simple_crud_for :update
21
21
  simple_crud_for :show
22
+ simple_crud_for :new
22
23
  simple_crud_for :index
23
24
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Finder
4
+ class DummyModelsController < ApplicationController
5
+ include Wor::Paginate
6
+ include Pundit::Authorization
7
+ extend SimpleCrudController
8
+
9
+ before_action :set_params
10
+ def set_params
11
+ SimpleCrudController.params = params
12
+ end
13
+
14
+ def dummy_model_params
15
+ params.permit(:name, :something, :user_id)
16
+ end
17
+
18
+ simple_crud_for :show, finder: ->(params) { DummyModel.find_by!(slug: params[:slug]) }
19
+ simple_crud_for :update, finder: :find_by_slug
20
+ simple_crud_for :destroy, finder: ->(params) { DummyModel.find_by!(slug: params[:slug]) }
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Html
4
+ class DummyModelsController < HtmlModes::BaseController
5
+ simple_crud_for :index, html: true, authenticate: false, authorize: false
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HtmlCreate
4
+ class DummyModelsController < HtmlModes::BaseController
5
+ simple_crud_for :create, html: true, authenticate: false, authorize: false
6
+ end
7
+ end