wor-simple_crud 0.2.1 → 0.3.1

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 (144) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -0
  3. data/CHANGELOG.md +46 -0
  4. data/Gemfile +1 -0
  5. data/README.md +200 -2
  6. data/lib/simple_crud/action_lambdas.rb +81 -0
  7. data/lib/simple_crud/authorization/adapter.rb +7 -0
  8. data/lib/simple_crud/authorization/pundit_adapter.rb +6 -0
  9. data/lib/simple_crud/config.rb +6 -1
  10. data/lib/simple_crud/controller_helpers.rb +128 -0
  11. data/lib/simple_crud/pagination/adapter.rb +7 -1
  12. data/lib/simple_crud/pagination/kaminari_adapter.rb +23 -2
  13. data/lib/simple_crud/pagination/pagy_adapter.rb +7 -2
  14. data/lib/simple_crud/pagination/will_paginate_adapter.rb +7 -3
  15. data/lib/simple_crud/pagination/wor_paginate_adapter.rb +7 -2
  16. data/lib/simple_crud/rspec/config.rb +74 -0
  17. data/lib/simple_crud/rspec/helpers/authentication.rb +32 -0
  18. data/lib/simple_crud/rspec/helpers/controller_options.rb +19 -0
  19. data/lib/simple_crud/rspec/helpers/models.rb +55 -0
  20. data/lib/simple_crud/rspec/helpers/policies.rb +25 -0
  21. data/lib/simple_crud/rspec/helpers/requests.rb +45 -0
  22. data/lib/simple_crud/rspec/helpers/settings.rb +49 -0
  23. data/lib/simple_crud/rspec/helpers.rb +25 -0
  24. data/lib/simple_crud/rspec.rb +35 -0
  25. data/lib/simple_crud/simple_crud_controller.rb +27 -75
  26. data/lib/simple_crud/version.rb +1 -1
  27. data/lib/spec/shared_contexts/authenticate_user.rb +1 -7
  28. data/lib/spec/shared_examples/authorization_adapter_authorize.rb +11 -0
  29. data/lib/spec/shared_examples/simple_crud_for_create.rb +46 -11
  30. data/lib/spec/shared_examples/simple_crud_for_create_with_block.rb +36 -0
  31. data/lib/spec/shared_examples/simple_crud_for_create_with_build.rb +15 -0
  32. data/lib/spec/shared_examples/simple_crud_for_destroy.rb +44 -8
  33. data/lib/spec/shared_examples/simple_crud_for_destroy_with_block.rb +21 -0
  34. data/lib/spec/shared_examples/simple_crud_for_destroy_with_finder.rb +35 -0
  35. data/lib/spec/shared_examples/simple_crud_for_edit.rb +79 -0
  36. data/lib/spec/shared_examples/simple_crud_for_index.rb +9 -24
  37. data/lib/spec/shared_examples/simple_crud_for_index_with_block.rb +22 -0
  38. data/lib/spec/shared_examples/simple_crud_for_index_with_scope.rb +33 -0
  39. data/lib/spec/shared_examples/simple_crud_for_new.rb +29 -0
  40. data/lib/spec/shared_examples/simple_crud_for_new_with_block.rb +20 -0
  41. data/lib/spec/shared_examples/simple_crud_for_new_with_build.rb +19 -0
  42. data/lib/spec/shared_examples/simple_crud_for_show.rb +14 -10
  43. data/lib/spec/shared_examples/simple_crud_for_show_with_block.rb +22 -0
  44. data/lib/spec/shared_examples/simple_crud_for_show_with_finder.rb +33 -0
  45. data/lib/spec/shared_examples/simple_crud_for_update.rb +61 -14
  46. data/lib/spec/shared_examples/simple_crud_for_update_with_finder.rb +39 -0
  47. data/lib/spec/shared_examples/simple_crud_not_found_with_finder.rb +14 -0
  48. data/lib/spec/shared_examples/simple_crud_renders_template.rb +8 -0
  49. data/lib/spec/shared_examples/simple_crud_when_not_authorized.rb +18 -0
  50. data/lib/spec/shared_examples/simple_crud_without_authenticated_user.rb +9 -0
  51. data/lib/spec/shared_examples/unauthorized_when_not_logged_in.rb +2 -2
  52. data/spec/block/dummy_models_controller_spec.rb +8 -0
  53. data/spec/block_create/dummy_models_controller_spec.rb +7 -0
  54. data/spec/block_destroy/dummy_models_controller_spec.rb +7 -0
  55. data/spec/block_finder/dummy_models_controller_spec.rb +10 -0
  56. data/spec/block_json/dummy_models_controller_spec.rb +9 -0
  57. data/spec/block_new/dummy_models_controller_spec.rb +7 -0
  58. data/spec/block_redirect/dummy_models_controller_spec.rb +7 -0
  59. data/spec/block_show/dummy_models_controller_spec.rb +7 -0
  60. data/spec/built/dummy_models_controller_spec.rb +8 -0
  61. data/spec/dummy/app/controllers/block/dummy_models_controller.rb +17 -0
  62. data/spec/dummy/app/controllers/block_create/dummy_models_controller.rb +13 -0
  63. data/spec/dummy/app/controllers/block_destroy/dummy_models_controller.rb +9 -0
  64. data/spec/dummy/app/controllers/block_finder/dummy_models_controller.rb +25 -0
  65. data/spec/dummy/app/controllers/block_json/dummy_models_controller.rb +17 -0
  66. data/spec/dummy/app/controllers/block_modes/base_controller.rb +11 -0
  67. data/spec/dummy/app/controllers/block_new/dummy_models_controller.rb +9 -0
  68. data/spec/dummy/app/controllers/block_redirect/dummy_models_controller.rb +13 -0
  69. data/spec/dummy/app/controllers/block_show/dummy_models_controller.rb +9 -0
  70. data/spec/dummy/app/controllers/built/dummy_models_controller.rb +21 -0
  71. data/spec/dummy/app/controllers/dummy_models_controller.rb +2 -1
  72. data/spec/dummy/app/controllers/finder/dummy_models_controller.rb +23 -0
  73. data/spec/dummy/app/controllers/html/dummy_models_controller.rb +7 -0
  74. data/spec/dummy/app/controllers/html_create/dummy_models_controller.rb +7 -0
  75. data/spec/dummy/app/controllers/html_destroy/dummy_models_controller.rb +7 -0
  76. data/spec/dummy/app/controllers/html_finder/dummy_models_controller.rb +17 -0
  77. data/spec/dummy/app/controllers/html_modes/base_controller.rb +17 -0
  78. data/spec/dummy/app/controllers/html_new/dummy_models_controller.rb +7 -0
  79. data/spec/dummy/app/controllers/html_scoped/dummy_models_controller.rb +8 -0
  80. data/spec/dummy/app/controllers/html_show/dummy_models_controller.rb +7 -0
  81. data/spec/dummy/app/controllers/html_update/dummy_models_controller.rb +7 -0
  82. data/spec/dummy/app/controllers/invalid_status/dummy_models_controller.rb +13 -0
  83. data/spec/dummy/app/controllers/nested/dummy_models_controller.rb +21 -0
  84. data/spec/dummy/app/controllers/nested_route/dummy_models_controller.rb +26 -0
  85. data/spec/dummy/app/controllers/redirect/dummy_models_controller.rb +10 -0
  86. data/spec/dummy/app/controllers/redirect_auth/dummy_models_controller.rb +28 -0
  87. data/spec/dummy/app/controllers/scoped/dummy_models_controller.rb +16 -0
  88. data/spec/dummy/app/controllers/scoped_params/dummy_models_controller.rb +16 -0
  89. data/spec/dummy/app/controllers/strict/dummy_models_controller.rb +21 -0
  90. data/spec/dummy/app/controllers/unpaginated_scoped/dummy_models_controller.rb +16 -0
  91. data/spec/dummy/app/models/dummy_model.rb +4 -0
  92. data/spec/dummy/app/models/dummy_model_policy.rb +9 -1
  93. data/spec/dummy/app/views/block/dummy_models/index.html.erb +3 -0
  94. data/spec/dummy/app/views/block_new/dummy_models/new.html.erb +1 -0
  95. data/spec/dummy/app/views/block_show/dummy_models/show.html.erb +1 -0
  96. data/spec/dummy/app/views/html/dummy_models/index.html.erb +3 -0
  97. data/spec/dummy/app/views/html_create/dummy_models/new.html.erb +1 -0
  98. data/spec/dummy/app/views/html_destroy/dummy_models/show.html.erb +1 -0
  99. data/spec/dummy/app/views/html_finder/dummy_models/edit.html.erb +1 -0
  100. data/spec/dummy/app/views/html_finder/dummy_models/new.html.erb +1 -0
  101. data/spec/dummy/app/views/html_finder/dummy_models/show.html.erb +1 -0
  102. data/spec/dummy/app/views/html_new/dummy_models/new.html.erb +1 -0
  103. data/spec/dummy/app/views/html_scoped/dummy_models/index.html.erb +3 -0
  104. data/spec/dummy/app/views/html_show/dummy_models/show.html.erb +1 -0
  105. data/spec/dummy/app/views/html_update/dummy_models/edit.html.erb +1 -0
  106. data/spec/dummy/app/views/invalid_status/dummy_models/new.html.erb +1 -0
  107. data/spec/dummy/app/views/layouts/application.html.erb +4 -0
  108. data/spec/dummy/app/views/redirect/dummy_models/new.html.erb +1 -0
  109. data/spec/dummy/config/routes.rb +110 -0
  110. data/spec/dummy/db/migrate/20260806000000_add_slug_to_dummy_models.rb +7 -0
  111. data/spec/dummy/db/migrate/20260807000000_add_classroom_slug_to_dummy_models.rb +7 -0
  112. data/spec/dummy/db/schema.rb +3 -1
  113. data/spec/dummy/spec/factories/dummy_models.rb +1 -0
  114. data/spec/dummy_controller_spec.rb +1 -0
  115. data/spec/dummy_model_policy_spec.rb +6 -4
  116. data/spec/finder/dummy_models_controller_spec.rb +13 -0
  117. data/spec/html/dummy_models_controller_spec.rb +7 -0
  118. data/spec/html_create/dummy_models_controller_spec.rb +7 -0
  119. data/spec/html_destroy/dummy_models_controller_spec.rb +7 -0
  120. data/spec/html_finder/dummy_models_controller_spec.rb +11 -0
  121. data/spec/html_new/dummy_models_controller_spec.rb +7 -0
  122. data/spec/html_scoped/dummy_models_controller_spec.rb +7 -0
  123. data/spec/html_show/dummy_models_controller_spec.rb +7 -0
  124. data/spec/html_update/dummy_models_controller_spec.rb +7 -0
  125. data/spec/invalid_status/dummy_models_controller_spec.rb +10 -0
  126. data/spec/nested/dummy_models_controller_spec.rb +12 -0
  127. data/spec/nested_route/dummy_models_controller_spec.rb +19 -0
  128. data/spec/redirect/dummy_models_controller_spec.rb +23 -0
  129. data/spec/redirect_auth/dummy_models_controller_spec.rb +11 -0
  130. data/spec/scoped/dummy_models_controller_spec.rb +7 -0
  131. data/spec/scoped_params/dummy_models_controller_spec.rb +12 -0
  132. data/spec/simple_crud/authorization/action_policy_adapter_spec.rb +1 -9
  133. data/spec/simple_crud/authorization/can_can_can_adapter_spec.rb +1 -7
  134. data/spec/simple_crud/authorization/pundit_adapter_spec.rb +41 -0
  135. data/spec/simple_crud/controller_helpers_spec.rb +49 -0
  136. data/spec/simple_crud/pagination/adapters_spec.rb +37 -13
  137. data/spec/simple_crud/rspec_config_spec.rb +100 -0
  138. data/spec/simple_crud_configuration_spec.rb +19 -16
  139. data/spec/simple_crud_controller_spec.rb +4 -4
  140. data/spec/spec_helper.rb +1 -0
  141. data/spec/strict/dummy_models_controller_spec.rb +23 -0
  142. data/spec/unpaginated_scoped/dummy_models_controller_spec.rb +7 -0
  143. metadata +108 -4
  144. data/lib/spec/shared_examples/helpers.rb +0 -50
@@ -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,48 @@ 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
49
+ it 'updates an model' do
50
+ expect(model_class_object.count).to be 1
51
+ end
52
+
53
+ it 'updates an model with valid attributes' do
54
+ expect(model_class_object.last).to have_attributes(update_attributes)
55
+ end
40
56
  end
41
57
 
42
- it 'updates an model with valid attributes' do
43
- expect(model_class.classify.constantize.last).to have_attributes(model_params)
58
+ it 'updates a record passing created_record_check', :aggregate_failures do
59
+ expect(model_class_object.last).to be_present
60
+ instance_exec(model_class_object.last, &created_record_check) if created_record_check
44
61
  end
45
62
  end
46
63
 
47
64
  context 'when updating a model that doesn\'t exist' do
48
65
  include_context 'with authenticated user'
49
66
 
50
- before { put :update, params: { id: 1 } }
67
+ before do
68
+ put :update, params: with_route_params(record_param(:update, nil, not_found: true)),
69
+ format: request_format(:update)
70
+ end
51
71
 
52
- it 'response with 422 status code' do
72
+ it 'response with 404 status code' do
53
73
  expect(response).to have_http_status(:not_found)
54
74
  end
55
75
 
@@ -57,5 +77,32 @@ shared_examples 'simple crud for update' do
57
77
  expect(model_class_object.count).to be 0
58
78
  end
59
79
  end
80
+
81
+ unless check_raise_on_invalid(:update)
82
+ context 'when updating with invalid attributes' do
83
+ include_context 'with authenticated user' if check_authenticate(:update)
84
+
85
+ before do
86
+ model
87
+ put :update, params: with_route_params(body_params({ required_attribute => nil }.merge(owner_params))
88
+ .merge(record_param(:update, model))),
89
+ format: request_format(:update)
90
+ end
91
+
92
+ unless check_block(:update)
93
+ if check_html(:update)
94
+ include_examples 'simple crud renders template', :edit, -> { invalid_status }
95
+ else
96
+ it 'responds with unprocessable entity' do
97
+ expect(response).to have_http_status(unprocessable_status)
98
+ end
99
+
100
+ it 'returns the validation errors' do
101
+ expect(response_body['errors']).to include(required_error)
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
60
107
  end
61
108
  end
@@ -0,0 +1,39 @@
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' if check_authenticate(:update)
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, format: request_format(:update)
16
+ end
17
+
18
+ it 'updates the model' do
19
+ expect(model.reload.public_send(required_attribute)).to eq('Updated')
20
+ end
21
+
22
+ if check_block(:update)
23
+ it 'passes the updated model to the block' do
24
+ expect(rendered_record.id).to eq(model.id)
25
+ end
26
+ elsif check_html(:update)
27
+ it 'redirects to the updated record' do
28
+ expect(response).to have_http_status(:found)
29
+ end
30
+ else
31
+ it 'responds with ok status' do
32
+ expect(response).to have_http_status(:ok)
33
+ end
34
+ end
35
+ end
36
+
37
+ include_examples 'simple crud not found with finder', :put, :update
38
+ end
39
+ end
@@ -0,0 +1,14 @@
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
+ format: request_format(action))
8
+ end
9
+
10
+ it 'responds with not found status' do
11
+ expect(response).to have_http_status(:not_found)
12
+ end
13
+ end
14
+ 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,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe BlockFinder::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for edit'
7
+ include_examples 'simple crud for show with finder'
8
+ include_examples 'simple crud for update with finder'
9
+ include_examples 'simple crud for destroy with finder'
10
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe BlockJson::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for index with block'
7
+ include_examples 'simple crud for show with block'
8
+ include_examples 'simple crud for new with block'
9
+ 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 BlockRedirect::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for destroy'
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,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlockFinder
4
+ class DummyModelsController < HtmlModes::BaseController
5
+ simple_crud_for :show, html: false, authenticate: false, authorize: false,
6
+ finder: ->(params) { DummyModel.find_by!(slug: params[:slug]) } do |record|
7
+ render json: { found_id: record.id }
8
+ end
9
+
10
+ simple_crud_for :update, html: false, authenticate: false, authorize: false,
11
+ finder: :find_by_slug do |_record, saved|
12
+ render json: { updated: saved }
13
+ end
14
+
15
+ simple_crud_for :destroy, html: false, authenticate: false, authorize: false,
16
+ finder: ->(params) { DummyModel.find_by!(slug: params[:slug]) } do |_record, destroyed|
17
+ render json: { destroyed: destroyed }
18
+ end
19
+
20
+ simple_crud_for :edit, html: false, authenticate: false, authorize: false,
21
+ finder: ->(params) { DummyModel.find_by!(slug: params[:slug]) } do |record|
22
+ render json: { edit_id: record.id }
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlockJson
4
+ class DummyModelsController < HtmlModes::BaseController
5
+ simple_crud_for :index, html: false, authenticate: false, authorize: false, paginate: false do |records|
6
+ render json: { ids: records.map(&:id) }
7
+ end
8
+
9
+ simple_crud_for :show, html: false, authenticate: false, authorize: false do |record|
10
+ render json: { id: record.id }
11
+ end
12
+
13
+ simple_crud_for :new, html: false, authenticate: false, authorize: false do |record|
14
+ render json: { id: record.id }
15
+ end
16
+ end
17
+ 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,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlockRedirect
4
+ class DummyModelsController < HtmlModes::BaseController
5
+ simple_crud_for :destroy, authenticate: false, authorize: false do |_record, destroyed|
6
+ if destroyed
7
+ redirect_to action: :show, notice: 'removed'
8
+ else
9
+ redirect_to action: :show, alert: 'could not remove'
10
+ end
11
+ end
12
+ end
13
+ 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,23 @@
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
+ simple_crud_for :edit, finder: ->(params) { DummyModel.find_by!(slug: params[:slug]) }
22
+ end
23
+ 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
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HtmlDestroy
4
+ class DummyModelsController < HtmlModes::BaseController
5
+ simple_crud_for :destroy, html: true, authenticate: false, authorize: false
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HtmlFinder
4
+ class DummyModelsController < HtmlModes::BaseController
5
+ include Pundit::Authorization
6
+
7
+ simple_crud_for :show, html: true, authenticate: false, authorize: false,
8
+ finder: ->(params) { DummyModel.find_by!(slug: params[:slug]) }
9
+ simple_crud_for :update, html: true, authenticate: false, authorize: false,
10
+ finder: :find_by_slug
11
+ simple_crud_for :destroy, html: true, authenticate: false, authorize: false,
12
+ finder: ->(params) { DummyModel.find_by!(slug: params[:slug]) }
13
+ simple_crud_for :new, html: true, build: -> { current_user.dummy_models.build }
14
+ simple_crud_for :edit, html: true, authenticate: false, authorize: false,
15
+ finder: ->(params) { DummyModel.find_by!(slug: params[:slug]) }
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HtmlModes
4
+ class BaseController < 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
+ def dummy_model_params
14
+ params.permit(:name, :something, :user_id, :slug)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HtmlNew
4
+ class DummyModelsController < HtmlModes::BaseController
5
+ simple_crud_for :new, html: true, authenticate: false, authorize: false
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HtmlScoped
4
+ class DummyModelsController < HtmlModes::BaseController
5
+ simple_crud_for :index, html: true, scope: ->(user) { DummyModel.where(user: user) },
6
+ authorize: false
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HtmlShow
4
+ class DummyModelsController < HtmlModes::BaseController
5
+ simple_crud_for :show, html: true, authenticate: false, authorize: false
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HtmlUpdate
4
+ class DummyModelsController < HtmlModes::BaseController
5
+ simple_crud_for :update, html: true, authenticate: false, authorize: false
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module InvalidStatus
4
+ class DummyModelsController < HtmlModes::BaseController
5
+ simple_crud_for :create, authenticate: false, authorize: false do |record, saved|
6
+ if saved
7
+ redirect_to(record)
8
+ else
9
+ render :new, status: 422
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nested
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.require(:dummy_model).permit(:name, :something, :user_id, :slug)
16
+ end
17
+
18
+ simple_crud_for :create
19
+ simple_crud_for :update
20
+ end
21
+ end