wor-simple_crud 0.3.0 → 0.4.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.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/CHANGELOG.md +31 -2
- data/README.md +93 -26
- data/lib/simple_crud/action_lambdas.rb +81 -0
- data/lib/simple_crud/controller_helpers.rb +11 -2
- data/lib/simple_crud/rspec/config.rb +18 -5
- data/lib/simple_crud/rspec/helpers/authentication.rb +32 -0
- data/lib/simple_crud/rspec/helpers/controller_options.rb +24 -0
- data/lib/simple_crud/rspec/helpers/models.rb +70 -0
- data/lib/simple_crud/rspec/helpers/policies.rb +25 -0
- data/lib/simple_crud/rspec/helpers/requests.rb +45 -0
- data/lib/simple_crud/rspec/helpers/settings.rb +49 -0
- data/lib/simple_crud/rspec/helpers.rb +16 -138
- data/lib/simple_crud/rspec.rb +1 -0
- data/lib/simple_crud/simple_crud_controller.rb +33 -81
- data/lib/simple_crud/version.rb +1 -1
- data/lib/simple_crud.rb +1 -0
- data/lib/spec/shared_examples/authorization_adapter_authorize.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_for_create.rb +16 -13
- data/lib/spec/shared_examples/simple_crud_for_create_with_block.rb +26 -8
- data/lib/spec/shared_examples/simple_crud_for_create_with_build.rb +3 -3
- data/lib/spec/shared_examples/simple_crud_for_destroy.rb +26 -1
- data/lib/spec/shared_examples/simple_crud_for_destroy_with_block.rb +11 -3
- data/lib/spec/shared_examples/simple_crud_for_destroy_with_finder.rb +18 -7
- data/lib/spec/shared_examples/simple_crud_for_edit.rb +79 -0
- data/lib/spec/shared_examples/simple_crud_for_index.rb +2 -2
- data/lib/spec/shared_examples/simple_crud_for_index_with_block.rb +12 -4
- data/lib/spec/shared_examples/simple_crud_for_index_with_scope.rb +26 -14
- data/lib/spec/shared_examples/simple_crud_for_new.rb +2 -2
- data/lib/spec/shared_examples/simple_crud_for_new_with_block.rb +14 -3
- data/lib/spec/shared_examples/simple_crud_for_new_with_build.rb +11 -5
- data/lib/spec/shared_examples/simple_crud_for_show.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_for_show_with_block.rb +14 -3
- data/lib/spec/shared_examples/simple_crud_for_show_with_finder.rb +17 -8
- data/lib/spec/shared_examples/simple_crud_for_update.rb +17 -10
- data/lib/spec/shared_examples/simple_crud_for_update_with_finder.rb +17 -7
- data/lib/spec/shared_examples/simple_crud_not_found_with_finder.rb +3 -2
- data/lib/spec/shared_examples/simple_crud_renders_template.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_when_not_authorized.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_without_authenticated_user.rb +1 -1
- data/lib/spec/shared_examples/unauthorized_when_not_logged_in.rb +1 -1
- data/spec/block_finder/dummy_models_controller_spec.rb +10 -0
- data/spec/block_json/dummy_models_controller_spec.rb +9 -0
- data/spec/block_redirect/dummy_models_controller_spec.rb +7 -0
- data/spec/dummy/app/controllers/block/dummy_models_controller.rb +1 -6
- data/spec/dummy/app/controllers/block_finder/dummy_models_controller.rb +25 -0
- data/spec/dummy/app/controllers/block_json/dummy_models_controller.rb +17 -0
- data/spec/dummy/app/controllers/block_new/dummy_models_controller.rb +1 -1
- data/spec/dummy/app/controllers/block_redirect/dummy_models_controller.rb +13 -0
- data/spec/dummy/app/controllers/block_show/dummy_models_controller.rb +1 -1
- data/spec/dummy/app/controllers/built/dummy_models_controller.rb +0 -5
- data/spec/dummy/app/controllers/dummy_models_controller.rb +0 -5
- data/spec/dummy/app/controllers/finder/dummy_models_controller.rb +1 -5
- data/spec/dummy/app/controllers/html_finder/dummy_models_controller.rb +17 -0
- data/spec/dummy/app/controllers/html_modes/base_controller.rb +0 -5
- data/spec/dummy/app/controllers/html_scoped/dummy_models_controller.rb +1 -1
- data/spec/dummy/app/controllers/invalid_status/dummy_models_controller.rb +1 -1
- data/spec/dummy/app/controllers/nested/dummy_models_controller.rb +0 -5
- data/spec/dummy/app/controllers/nested_route/dummy_models_controller.rb +0 -5
- data/spec/dummy/app/controllers/redirect_auth/dummy_models_controller.rb +0 -5
- data/spec/dummy/app/controllers/scoped/dummy_models_controller.rb +0 -5
- data/spec/dummy/app/controllers/scoped_params/dummy_models_controller.rb +0 -5
- data/spec/dummy/app/controllers/strict/dummy_models_controller.rb +0 -5
- data/spec/dummy/app/controllers/unpaginated_scoped/dummy_models_controller.rb +11 -0
- data/spec/dummy/app/controllers/without_pagination/dummy_models_controller.rb +0 -5
- data/spec/dummy/app/models/dummy_model_policy.rb +5 -1
- data/spec/dummy/app/views/html_finder/dummy_models/edit.html.erb +1 -0
- data/spec/dummy/app/views/html_finder/dummy_models/new.html.erb +1 -0
- data/spec/dummy/app/views/html_finder/dummy_models/show.html.erb +1 -0
- data/spec/dummy/config/routes.rb +21 -1
- data/spec/dummy_model_policy_spec.rb +18 -8
- data/spec/finder/dummy_models_controller_spec.rb +1 -0
- data/spec/html_finder/dummy_models_controller_spec.rb +11 -0
- data/spec/html_scoped/dummy_models_controller_spec.rb +1 -1
- data/spec/scoped_params/dummy_models_controller_spec.rb +4 -3
- data/spec/simple_crud/rspec_config_spec.rb +39 -0
- data/spec/simple_crud_controller_spec.rb +38 -0
- data/spec/unpaginated_scoped/dummy_models_controller_spec.rb +7 -0
- metadata +23 -2
|
@@ -1,22 +1,33 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
shared_examples 'simple crud for destroy with finder' do
|
|
3
|
+
RSpec.shared_examples 'simple crud for destroy with finder' do
|
|
4
4
|
describe 'DELETE #destroy with a custom finder' do
|
|
5
|
-
include_context 'with authenticated user'
|
|
5
|
+
include_context 'with authenticated user' if check_authenticate(:destroy)
|
|
6
6
|
|
|
7
7
|
context 'when the model exists' do
|
|
8
8
|
before do
|
|
9
9
|
model
|
|
10
|
-
delete :destroy, params: with_route_params(finder_key => model.public_send(finder_key))
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
it 'responds with ok status' do
|
|
14
|
-
expect(response).to have_http_status(:ok)
|
|
10
|
+
delete :destroy, params: with_route_params(finder_key => model.public_send(finder_key)),
|
|
11
|
+
format: request_format(:destroy)
|
|
15
12
|
end
|
|
16
13
|
|
|
17
14
|
it 'destroys the model' do
|
|
18
15
|
expect(model_class_object.exists?(model.id)).to be false
|
|
19
16
|
end
|
|
17
|
+
|
|
18
|
+
if check_block(:destroy)
|
|
19
|
+
it 'passes the destroyed record to the block' do
|
|
20
|
+
expect(rendered_record.id).to eq(model.id)
|
|
21
|
+
end
|
|
22
|
+
elsif check_html(:destroy)
|
|
23
|
+
it 'redirects to the collection' do
|
|
24
|
+
expect(response).to have_http_status(:found)
|
|
25
|
+
end
|
|
26
|
+
else
|
|
27
|
+
it 'responds with ok status' do
|
|
28
|
+
expect(response).to have_http_status(:ok)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
20
31
|
end
|
|
21
32
|
|
|
22
33
|
include_examples 'simple crud not found with finder', :delete, :destroy
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
RSpec.shared_examples 'simple crud for edit' do
|
|
4
|
+
describe 'GET #edit' do
|
|
5
|
+
subject(:edit_request) { get :edit, params: with_route_params(edit_params), format: request_format(:edit) }
|
|
6
|
+
|
|
7
|
+
before do
|
|
8
|
+
model
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
context 'without authenticated user' do
|
|
12
|
+
subject!(:req) do
|
|
13
|
+
get :edit, params: with_route_params(record_param(:edit, model)), format: request_format(:edit)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
include_examples 'unauthorized when not logged in' if check_authenticate(:edit)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
if check_authorize(:edit)
|
|
20
|
+
context 'when the user is not authorized' do
|
|
21
|
+
include_context 'with authenticated user' if check_authenticate(:edit)
|
|
22
|
+
|
|
23
|
+
let(:edit_params) { record_param(:edit, model) }
|
|
24
|
+
|
|
25
|
+
before do
|
|
26
|
+
model
|
|
27
|
+
make_policies_fail(:edit)
|
|
28
|
+
edit_request
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'responds with forbidden status' do
|
|
32
|
+
expect(response).to have_http_status(:forbidden)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
context 'when the model exists' do
|
|
38
|
+
include_context 'with authenticated user' if check_authenticate(:edit)
|
|
39
|
+
|
|
40
|
+
let(:edit_params) { record_param(:edit, model) }
|
|
41
|
+
|
|
42
|
+
before do
|
|
43
|
+
model
|
|
44
|
+
make_policies_succeed(:edit) if check_authorize(:edit)
|
|
45
|
+
edit_request
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it 'responds with ok status' do
|
|
49
|
+
expect(response).to have_http_status(:ok)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
if check_block(:edit)
|
|
53
|
+
it 'renders the asked model through the block' do
|
|
54
|
+
expect(response).to be_successful
|
|
55
|
+
end
|
|
56
|
+
elsif check_html(:edit)
|
|
57
|
+
include_examples 'simple crud renders template', :edit
|
|
58
|
+
else
|
|
59
|
+
it 'returns the asked model' do
|
|
60
|
+
expect(response_body['id']).to eq(model.id)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
context 'when the model does not exist' do
|
|
66
|
+
include_context 'with authenticated user'
|
|
67
|
+
|
|
68
|
+
let(:edit_params) { record_param(:edit, nil, not_found: true) }
|
|
69
|
+
|
|
70
|
+
before do
|
|
71
|
+
edit_request
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it 'responds with not found status' do
|
|
75
|
+
expect(response).to have_http_status(:not_found)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
shared_examples 'simple crud for index' do
|
|
3
|
+
RSpec.shared_examples 'simple crud for index' do
|
|
4
4
|
describe 'GET #index' do
|
|
5
5
|
let(:created_models) { create_records(model_class, 10, model_attributes) }
|
|
6
6
|
|
|
@@ -13,7 +13,7 @@ shared_examples 'simple crud for index' do
|
|
|
13
13
|
include_context 'with authenticated user' if check_authenticate(:index)
|
|
14
14
|
|
|
15
15
|
before do
|
|
16
|
-
make_policies_succeed(:index)
|
|
16
|
+
make_policies_succeed(:index) if check_authorize(:index)
|
|
17
17
|
get :index, params: with_route_params({}), format: request_format(:index)
|
|
18
18
|
end
|
|
19
19
|
|
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
shared_examples 'simple crud for index with block' do
|
|
3
|
+
RSpec.shared_examples 'simple crud for index with block' do
|
|
4
4
|
describe 'GET #index with a render block' do
|
|
5
|
-
let(:created_models) { create_records(model_class, 2,
|
|
5
|
+
let(:created_models) { create_records(model_class, 2, model_attributes) }
|
|
6
|
+
|
|
7
|
+
include_context 'with authenticated user' if check_authenticate(:index)
|
|
6
8
|
|
|
7
9
|
before do
|
|
8
10
|
created_models
|
|
9
|
-
get :index, params: with_route_params({}), format: :
|
|
11
|
+
get :index, params: with_route_params({}), format: request_format(:index)
|
|
10
12
|
end
|
|
11
13
|
|
|
12
|
-
|
|
14
|
+
if check_html(:index)
|
|
15
|
+
include_examples 'simple crud renders template', :index
|
|
16
|
+
else
|
|
17
|
+
it 'renders the records through the block' do
|
|
18
|
+
expect(response).to be_successful
|
|
19
|
+
end
|
|
20
|
+
end
|
|
13
21
|
end
|
|
14
22
|
end
|
|
@@ -1,21 +1,33 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
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) }
|
|
3
|
+
RSpec.shared_examples 'simple crud for index with scope' do
|
|
8
4
|
let(:request_params) { {} }
|
|
5
|
+
let(:my_models) { create_records(model_class, 2, scoped_attributes) }
|
|
6
|
+
let(:other_models) { create_records(model_class, 3, other_scoped_attributes) }
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
# Hooks stay inside this context so co-including the example never
|
|
9
|
+
# authenticates or stubs policies for sibling examples.
|
|
10
|
+
context 'when listing the scoped records' do
|
|
11
|
+
before do
|
|
12
|
+
authenticate_request if check_authenticate(:index)
|
|
13
|
+
make_policies_succeed(:index) if check_authorize(:index)
|
|
14
|
+
my_models
|
|
15
|
+
other_models
|
|
16
|
+
get :index, params: with_route_params(request_params), format: request_format(:index)
|
|
17
|
+
end
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
if check_html(:index)
|
|
20
|
+
it 'returns only the assigned scoped records' do
|
|
21
|
+
expect(rendered_records.map(&:id)).to match_array(my_models.map(&:id))
|
|
22
|
+
end
|
|
23
|
+
elsif check_paginate(:index)
|
|
24
|
+
it 'returns only the paginated scoped records' do
|
|
25
|
+
expect(response_body['page'].map { |m| m['id'] }).to match_array(my_models.map(&:id))
|
|
26
|
+
end
|
|
27
|
+
else
|
|
28
|
+
it 'returns only the unpaginated scoped records' do
|
|
29
|
+
expect(response_body.map { |m| m['id'] }).to match_array(my_models.map(&:id))
|
|
30
|
+
end
|
|
31
|
+
end
|
|
20
32
|
end
|
|
21
33
|
end
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
shared_examples 'simple crud for new' do
|
|
3
|
+
RSpec.shared_examples 'simple crud for new' do
|
|
4
4
|
describe 'GET #new' do
|
|
5
5
|
include_examples 'simple crud without authenticated user', :new
|
|
6
6
|
include_examples 'simple crud when not authorized', :new, :get
|
|
@@ -9,7 +9,7 @@ shared_examples 'simple crud for new' do
|
|
|
9
9
|
include_context 'with authenticated user' if check_authenticate(:new)
|
|
10
10
|
|
|
11
11
|
before do
|
|
12
|
-
make_policies_succeed(:new)
|
|
12
|
+
make_policies_succeed(:new) if check_authorize(:new)
|
|
13
13
|
get :new, params: with_route_params({}), format: request_format(:new)
|
|
14
14
|
end
|
|
15
15
|
|
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
shared_examples 'simple crud for new with block' do
|
|
3
|
+
RSpec.shared_examples 'simple crud for new with block' do
|
|
4
4
|
describe 'GET #new with a render block' do
|
|
5
|
-
|
|
5
|
+
include_context 'with authenticated user' if check_authenticate(:new)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
before do
|
|
8
|
+
make_policies_succeed(:new) if check_authorize(:new)
|
|
9
|
+
get :new, params: with_route_params({}), format: request_format(:new)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
if check_html(:new)
|
|
13
|
+
include_examples 'simple crud renders template', :new
|
|
14
|
+
else
|
|
15
|
+
it 'renders the built record through the block' do
|
|
16
|
+
expect(response).to be_successful
|
|
17
|
+
end
|
|
18
|
+
end
|
|
8
19
|
end
|
|
9
20
|
end
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
shared_examples 'simple crud for new with build' do
|
|
3
|
+
RSpec.shared_examples 'simple crud for new with build' do
|
|
4
4
|
describe 'GET #new with a build hook' do
|
|
5
|
-
include_context 'with authenticated user'
|
|
5
|
+
include_context 'with authenticated user' if check_authenticate(:new)
|
|
6
6
|
|
|
7
|
-
before
|
|
7
|
+
before do
|
|
8
|
+
get :new, params: with_route_params({}), format: request_format(:new)
|
|
9
|
+
end
|
|
8
10
|
|
|
9
|
-
it 'builds the record owned by the current user' do
|
|
10
|
-
|
|
11
|
+
it 'builds the record owned by the current user', :aggregate_failures do
|
|
12
|
+
if check_html(:new) || check_block(:new)
|
|
13
|
+
expect(rendered_record.public_send(owner_foreign_key)).to eq(current_user.id)
|
|
14
|
+
else
|
|
15
|
+
expect(response_body[owner_foreign_key.to_s]).to eq(current_user.id)
|
|
16
|
+
end
|
|
11
17
|
end
|
|
12
18
|
end
|
|
13
19
|
end
|
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
shared_examples 'simple crud for show with block' do
|
|
3
|
+
RSpec.shared_examples 'simple crud for show with block' do
|
|
4
4
|
describe 'GET #show with a render block' do
|
|
5
|
+
include_context 'with authenticated user' if check_authenticate(:show)
|
|
6
|
+
|
|
5
7
|
let!(:record) { model }
|
|
6
8
|
|
|
7
|
-
before
|
|
9
|
+
before do
|
|
10
|
+
make_policies_succeed(:show) if check_authorize(:show)
|
|
11
|
+
get :show, params: with_route_params(record_param(:show, record)), format: request_format(:show)
|
|
12
|
+
end
|
|
8
13
|
|
|
9
|
-
|
|
14
|
+
if check_html(:show)
|
|
15
|
+
include_examples 'simple crud renders template', :show
|
|
16
|
+
else
|
|
17
|
+
it 'renders the record through the block' do
|
|
18
|
+
expect(response).to be_successful
|
|
19
|
+
end
|
|
20
|
+
end
|
|
10
21
|
end
|
|
11
22
|
end
|
|
@@ -1,21 +1,30 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
shared_examples 'simple crud for show with finder' do
|
|
3
|
+
RSpec.shared_examples 'simple crud for show with finder' do
|
|
4
4
|
describe 'GET #show with a custom finder' do
|
|
5
|
-
include_context 'with authenticated user'
|
|
5
|
+
include_context 'with authenticated user' if check_authenticate(:show)
|
|
6
6
|
|
|
7
7
|
context 'when the model exists' do
|
|
8
8
|
before do
|
|
9
9
|
model
|
|
10
|
-
get :show, params: with_route_params(finder_key => model.public_send(finder_key))
|
|
10
|
+
get :show, params: with_route_params(finder_key => model.public_send(finder_key)),
|
|
11
|
+
format: request_format(:show)
|
|
11
12
|
end
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
if check_block(:show)
|
|
15
|
+
it 'finds the asked model' do
|
|
16
|
+
expect(rendered_record.id).to eq(model.id)
|
|
17
|
+
end
|
|
18
|
+
elsif check_html(:show)
|
|
19
|
+
include_examples 'simple crud renders template', :show
|
|
20
|
+
else
|
|
21
|
+
it 'responds with ok status' do
|
|
22
|
+
expect(response).to have_http_status(:ok)
|
|
23
|
+
end
|
|
16
24
|
|
|
17
|
-
|
|
18
|
-
|
|
25
|
+
it 'returns the asked model' do
|
|
26
|
+
expect(response_body['id']).to eq(model.id)
|
|
27
|
+
end
|
|
19
28
|
end
|
|
20
29
|
end
|
|
21
30
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
shared_examples 'simple crud for update' do
|
|
3
|
+
RSpec.shared_examples 'simple crud for update' do
|
|
4
4
|
describe 'PUT #update' do
|
|
5
5
|
context 'without authenticated user' do
|
|
6
6
|
subject!(:req) do
|
|
@@ -54,6 +54,11 @@ shared_examples 'simple crud for update' do
|
|
|
54
54
|
expect(model_class_object.last).to have_attributes(update_attributes)
|
|
55
55
|
end
|
|
56
56
|
end
|
|
57
|
+
|
|
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
|
|
61
|
+
end
|
|
57
62
|
end
|
|
58
63
|
|
|
59
64
|
context 'when updating a model that doesn\'t exist' do
|
|
@@ -84,15 +89,17 @@ shared_examples 'simple crud for update' do
|
|
|
84
89
|
format: request_format(:update)
|
|
85
90
|
end
|
|
86
91
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
|
96
103
|
end
|
|
97
104
|
end
|
|
98
105
|
end
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
shared_examples 'simple crud for update with finder' do
|
|
3
|
+
RSpec.shared_examples 'simple crud for update with finder' do
|
|
4
4
|
describe 'PUT #update with a custom finder' do
|
|
5
|
-
include_context 'with authenticated user'
|
|
5
|
+
include_context 'with authenticated user' if check_authenticate(:update)
|
|
6
6
|
|
|
7
7
|
context 'when successfully updating a model' do
|
|
8
8
|
let(:update_params) do
|
|
@@ -12,16 +12,26 @@ shared_examples 'simple crud for update with finder' do
|
|
|
12
12
|
|
|
13
13
|
before do
|
|
14
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)
|
|
15
|
+
put :update, params: update_params, format: request_format(:update)
|
|
20
16
|
end
|
|
21
17
|
|
|
22
18
|
it 'updates the model' do
|
|
23
19
|
expect(model.reload.public_send(required_attribute)).to eq('Updated')
|
|
24
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
|
|
25
35
|
end
|
|
26
36
|
|
|
27
37
|
include_examples 'simple crud not found with finder', :put, :update
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
shared_examples 'simple crud not found with finder' do |http_method, action|
|
|
3
|
+
RSpec.shared_examples 'simple crud not found with finder' do |http_method, action|
|
|
4
4
|
context 'when the model does not exist' do
|
|
5
5
|
before do
|
|
6
|
-
send(http_method, action, params: with_route_params(finder_key => "nonexistent-#{finder_key}")
|
|
6
|
+
send(http_method, action, params: with_route_params(finder_key => "nonexistent-#{finder_key}"),
|
|
7
|
+
format: request_format(action))
|
|
7
8
|
end
|
|
8
9
|
|
|
9
10
|
it 'responds with not found status' do
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
shared_examples 'simple crud renders template' do |template, status = :ok|
|
|
3
|
+
RSpec.shared_examples 'simple crud renders template' do |template, status = :ok|
|
|
4
4
|
it "renders the #{template} template", :aggregate_failures do
|
|
5
5
|
expect(response).to have_http_status(resolve(status))
|
|
6
6
|
expect(response).to render_template(template) if assert_html_template
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
shared_examples 'simple crud when not authorized' do |action, verb, request_params = {}|
|
|
3
|
+
RSpec.shared_examples 'simple crud when not authorized' do |action, verb, request_params = {}|
|
|
4
4
|
if check_authorize(action)
|
|
5
5
|
context 'when not authorized' do
|
|
6
6
|
include_context 'with authenticated user' if check_authenticate(action)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
shared_examples 'simple crud without authenticated user' do |action|
|
|
3
|
+
RSpec.shared_examples 'simple crud without authenticated user' do |action|
|
|
4
4
|
context 'without authenticated user' do
|
|
5
5
|
subject!(:req) { get action, params: with_route_params({}) }
|
|
6
6
|
|
|
@@ -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
|
|
@@ -5,12 +5,7 @@ module Block
|
|
|
5
5
|
include Wor::Paginate
|
|
6
6
|
extend SimpleCrudController
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
def set_params
|
|
10
|
-
SimpleCrudController.params = params
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
simple_crud_for :index, paginate: false, authenticate: false, authorize: false do |records|
|
|
8
|
+
simple_crud_for :index, html: true, paginate: false, authenticate: false, authorize: false do |records|
|
|
14
9
|
render :index, locals: { models: records }
|
|
15
10
|
end
|
|
16
11
|
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
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
module BlockNew
|
|
4
4
|
class DummyModelsController < BlockModes::BaseController
|
|
5
|
-
simple_crud_for :new, authenticate: false, authorize: false do |record|
|
|
5
|
+
simple_crud_for :new, html: true, authenticate: false, authorize: false do |record|
|
|
6
6
|
render_model(record, :new)
|
|
7
7
|
end
|
|
8
8
|
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module BlockRedirect
|
|
4
|
+
class DummyModelsController < HtmlModes::BaseController
|
|
5
|
+
simple_crud_for :destroy, html: true, 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
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
module BlockShow
|
|
4
4
|
class DummyModelsController < BlockModes::BaseController
|
|
5
|
-
simple_crud_for :show, authenticate: false, authorize: false do |record|
|
|
5
|
+
simple_crud_for :show, html: true, authenticate: false, authorize: false do |record|
|
|
6
6
|
render_model(record, :show)
|
|
7
7
|
end
|
|
8
8
|
end
|
|
@@ -6,11 +6,6 @@ class DummyModelsController < ApplicationController
|
|
|
6
6
|
include Pundit::Authorization
|
|
7
7
|
extend SimpleCrudController
|
|
8
8
|
|
|
9
|
-
before_action :set_params
|
|
10
|
-
def set_params
|
|
11
|
-
SimpleCrudController.params = params
|
|
12
|
-
end
|
|
13
|
-
|
|
14
9
|
def dummy_model_params
|
|
15
10
|
params.permit(:name, :something, :user_id, :slug)
|
|
16
11
|
end
|