wor-simple_crud 0.3.0 → 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.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/CHANGELOG.md +16 -2
- data/README.md +57 -14
- 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 +19 -0
- data/lib/simple_crud/rspec/helpers/models.rb +55 -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 +11 -74
- data/lib/simple_crud/version.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_for_create.rb +15 -12
- data/lib/spec/shared_examples/simple_crud_for_create_with_block.rb +25 -7
- data/lib/spec/shared_examples/simple_crud_for_create_with_build.rb +2 -2
- data/lib/spec/shared_examples/simple_crud_for_destroy.rb +25 -0
- data/lib/spec/shared_examples/simple_crud_for_destroy_with_block.rb +10 -2
- data/lib/spec/shared_examples/simple_crud_for_destroy_with_finder.rb +17 -6
- data/lib/spec/shared_examples/simple_crud_for_edit.rb +79 -0
- data/lib/spec/shared_examples/simple_crud_for_index.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_for_index_with_block.rb +11 -3
- data/lib/spec/shared_examples/simple_crud_for_index_with_scope.rb +25 -13
- data/lib/spec/shared_examples/simple_crud_for_new.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_for_new_with_block.rb +13 -2
- data/lib/spec/shared_examples/simple_crud_for_new_with_build.rb +10 -4
- data/lib/spec/shared_examples/simple_crud_for_show_with_block.rb +13 -2
- data/lib/spec/shared_examples/simple_crud_for_show_with_finder.rb +16 -7
- data/lib/spec/shared_examples/simple_crud_for_update.rb +16 -9
- data/lib/spec/shared_examples/simple_crud_for_update_with_finder.rb +16 -6
- data/lib/spec/shared_examples/simple_crud_not_found_with_finder.rb +2 -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_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_redirect/dummy_models_controller.rb +13 -0
- data/spec/dummy/app/controllers/finder/dummy_models_controller.rb +1 -0
- data/spec/dummy/app/controllers/html_finder/dummy_models_controller.rb +17 -0
- data/spec/dummy/app/controllers/html_scoped/dummy_models_controller.rb +1 -1
- data/spec/dummy/app/controllers/unpaginated_scoped/dummy_models_controller.rb +16 -0
- 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 +33 -0
- data/spec/unpaginated_scoped/dummy_models_controller_spec.rb +7 -0
- metadata +23 -2
|
@@ -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
|
|
|
@@ -2,8 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
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
|
|
@@ -2,12 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
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
|
|
@@ -2,10 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
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
|
|
@@ -2,20 +2,29 @@
|
|
|
2
2
|
|
|
3
3
|
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
|
|
|
@@ -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
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
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
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
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
|
|
@@ -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,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,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
|
|
@@ -18,5 +18,6 @@ module Finder
|
|
|
18
18
|
simple_crud_for :show, finder: ->(params) { DummyModel.find_by!(slug: params[:slug]) }
|
|
19
19
|
simple_crud_for :update, finder: :find_by_slug
|
|
20
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]) }
|
|
21
22
|
end
|
|
22
23
|
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,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module UnpaginatedScoped
|
|
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
|
+
simple_crud_for :index, paginate: false, scope: ->(user) { DummyModel.where(user: user) }
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<h1>Edit dummy model</h1>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<h1>New dummy model</h1>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<span class="record"><%= @record.name %></span>
|
data/spec/dummy/config/routes.rb
CHANGED
|
@@ -17,13 +17,25 @@ Rails.application.routes.draw do
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
namespace :finder do
|
|
20
|
-
resources :dummy_models, only: %i[show update destroy], param: :slug
|
|
20
|
+
resources :dummy_models, only: %i[show edit update destroy], param: :slug
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
namespace :html_finder do
|
|
24
|
+
resources :dummy_models, only: %i[new show edit update destroy], param: :slug
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
namespace :block_finder do
|
|
28
|
+
resources :dummy_models, only: %i[show edit update destroy], param: :slug
|
|
21
29
|
end
|
|
22
30
|
|
|
23
31
|
namespace :scoped do
|
|
24
32
|
resources :dummy_models, only: :index
|
|
25
33
|
end
|
|
26
34
|
|
|
35
|
+
namespace :unpaginated_scoped do
|
|
36
|
+
resources :dummy_models, only: :index
|
|
37
|
+
end
|
|
38
|
+
|
|
27
39
|
namespace :scoped_params do
|
|
28
40
|
resources :dummy_models, only: :index
|
|
29
41
|
end
|
|
@@ -72,6 +84,14 @@ Rails.application.routes.draw do
|
|
|
72
84
|
resources :dummy_models, only: :destroy
|
|
73
85
|
end
|
|
74
86
|
|
|
87
|
+
namespace :block_redirect do
|
|
88
|
+
resources :dummy_models, only: %i[show destroy]
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
namespace :block_json do
|
|
92
|
+
resources :dummy_models, only: %i[index show new]
|
|
93
|
+
end
|
|
94
|
+
|
|
75
95
|
namespace :built do
|
|
76
96
|
resources :dummy_models, only: %i[new create]
|
|
77
97
|
end
|
|
@@ -7,15 +7,25 @@ describe DummyModelPolicy do
|
|
|
7
7
|
let(:other_user) { create(:user) }
|
|
8
8
|
let(:dummy_model) { create(:dummy_model, user: user) }
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
describe '#destroy?' do
|
|
11
|
+
it 'allows the owning user' do
|
|
12
|
+
expect(described_class.new(user, dummy_model).destroy?).to be true
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it 'denies a different user' do
|
|
16
|
+
expect(described_class.new(other_user, dummy_model).destroy?).to be false
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# :index authorizes the model class, not an instance, so the policy can
|
|
21
|
+
# only reason about the user.
|
|
22
|
+
describe '#index?' do
|
|
23
|
+
it 'allows any signed-in user' do
|
|
24
|
+
expect(described_class.new(other_user, dummy_model).index?).to be true
|
|
25
|
+
end
|
|
15
26
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
end
|
|
27
|
+
it 'denies an anonymous user' do
|
|
28
|
+
expect(described_class.new(nil, dummy_model).index?).to be false
|
|
19
29
|
end
|
|
20
30
|
end
|
|
21
31
|
end
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require 'spec_helper'
|
|
4
4
|
|
|
5
5
|
describe Finder::DummyModelsController, type: :controller do
|
|
6
|
+
include_examples 'simple crud for edit'
|
|
6
7
|
include_examples 'simple crud for show with finder'
|
|
7
8
|
include_examples 'simple crud for update with finder'
|
|
8
9
|
include_examples 'simple crud for destroy with finder'
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe HtmlFinder::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
|
+
include_examples 'simple crud for new with build'
|
|
11
|
+
end
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
require 'spec_helper'
|
|
4
4
|
|
|
5
|
-
describe ScopedParams::DummyModelsController,
|
|
5
|
+
describe ScopedParams::DummyModelsController, simple_crud: {
|
|
6
|
+
scoped_attributes: -> { { user: current_user, something: 42 } },
|
|
7
|
+
other_scoped_attributes: -> { { user: other_user, something: 7 } }
|
|
8
|
+
}, type: :controller do
|
|
6
9
|
include_examples 'simple crud for index with scope' do
|
|
7
|
-
let(:my_models) { create_list(:dummy_model, 2, user: user, something: 42) }
|
|
8
|
-
let(:other_models) { create_list(:dummy_model, 1, user: user, something: 7) }
|
|
9
10
|
let(:request_params) { { something: 42 } }
|
|
10
11
|
end
|
|
11
12
|
end
|
|
@@ -39,6 +39,16 @@ describe SimpleCrud::RSpec do
|
|
|
39
39
|
end
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
+
it 'derives other_scoped_attributes from model_attributes and the other user' do
|
|
43
|
+
expect(other_scoped_attributes).to eq(user: other_user)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it 'requires a Hash model_attributes before deriving other_scoped_attributes' do
|
|
47
|
+
with_config_override(:model_attributes, -> { 'not-a-hash' }) do
|
|
48
|
+
expect { other_scoped_attributes }.to raise_error(ArgumentError, /override other_scoped_attributes instead/)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
42
52
|
it 'lets settings be overridden and restored', :aggregate_failures do
|
|
43
53
|
original = config.required_attribute
|
|
44
54
|
|
|
@@ -63,5 +73,28 @@ describe SimpleCrud::RSpec do
|
|
|
63
73
|
|
|
64
74
|
SimpleCrud::RSpec.configure { |c| c.required_attribute = original }
|
|
65
75
|
end
|
|
76
|
+
|
|
77
|
+
it 'falls back to global config when no metadata overrides' do
|
|
78
|
+
expect(setting(:finder_key)).to eq(:slug)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it 'defaults model_attributes to the owner association and current user' do
|
|
82
|
+
expect(model_attributes).to eq(user: current_user)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
describe 'with simple_crud metadata',
|
|
86
|
+
simple_crud: { finder_key: :token, model_attributes: -> { { overridden: true } } } do
|
|
87
|
+
it 'prefers metadata for overridden settings' do
|
|
88
|
+
expect(setting(:finder_key)).to eq(:token)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
it 'still falls back to global config for the rest' do
|
|
92
|
+
expect(required_attribute).to eq(:name)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it 'resolves model_attributes from metadata in the example context' do
|
|
96
|
+
expect(model_attributes).to eq(overridden: true)
|
|
97
|
+
end
|
|
98
|
+
end
|
|
66
99
|
end
|
|
67
100
|
end
|