wor-simple_crud 0.4.0 → 0.6.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/.rubocop.yml +15 -0
- data/CHANGELOG.md +28 -0
- data/Gemfile +5 -1
- data/README.md +69 -3
- data/lib/simple_crud/action_context.rb +119 -0
- data/lib/simple_crud/action_lambdas.rb +4 -72
- data/lib/simple_crud/cache_helpers.rb +17 -0
- data/lib/simple_crud/create_context.rb +18 -0
- data/lib/simple_crud/destroy_context.rb +16 -0
- data/lib/simple_crud/edit_context.rb +13 -0
- data/lib/simple_crud/index_context.rb +71 -0
- data/lib/simple_crud/new_context.rb +13 -0
- data/lib/simple_crud/pagination/wor_paginate_adapter.rb +7 -1
- data/lib/simple_crud/persistence_context.rb +49 -0
- data/lib/simple_crud/rspec/config.rb +6 -1
- data/lib/simple_crud/rspec/helpers/authentication.rb +2 -1
- data/lib/simple_crud/rspec/helpers/controller_options.rb +2 -2
- data/lib/simple_crud/rspec/helpers/requests.rb +5 -2
- data/lib/simple_crud/serializer.rb +19 -0
- data/lib/simple_crud/show_context.rb +29 -0
- data/lib/simple_crud/simple_crud_controller.rb +17 -4
- data/lib/simple_crud/update_context.rb +17 -0
- data/lib/simple_crud/version.rb +1 -1
- data/lib/spec/matchers/have_been_serialized_with.rb +19 -2
- data/lib/spec/shared_examples/simple_crud_for_create.rb +6 -6
- data/lib/spec/shared_examples/simple_crud_for_create_with_block.rb +2 -2
- data/lib/spec/shared_examples/simple_crud_for_create_with_build.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_for_destroy.rb +9 -9
- data/lib/spec/shared_examples/simple_crud_for_destroy_with_block.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_for_destroy_with_finder.rb +3 -3
- data/lib/spec/shared_examples/simple_crud_for_edit.rb +2 -2
- data/lib/spec/shared_examples/simple_crud_for_index.rb +3 -3
- data/lib/spec/shared_examples/simple_crud_for_index_with_block.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_for_index_with_scope.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_for_new.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_for_new_with_block.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_for_new_with_build.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_for_show.rb +2 -2
- data/lib/spec/shared_examples/simple_crud_for_show_with_block.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_for_show_with_finder.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_for_update.rb +7 -7
- data/lib/spec/shared_examples/simple_crud_for_update_with_finder.rb +1 -1
- data/lib/spec/shared_examples/simple_crud_not_found_with_finder.rb +1 -1
- data/spec/blueprint/dummy_models_controller_spec.rb +25 -0
- data/spec/blueprint_unpaginated/dummy_models_controller_spec.rb +9 -0
- data/spec/cached/dummy_models_controller_spec.rb +23 -0
- data/spec/cached_defaults/dummy_models_controller_spec.rb +34 -0
- data/spec/dummy/app/blueprints/dummy_model_blueprint.rb +10 -0
- data/spec/dummy/app/controllers/blueprint/dummy_models_controller.rb +21 -0
- data/spec/dummy/app/controllers/blueprint_unpaginated/dummy_models_controller.rb +10 -0
- data/spec/dummy/app/controllers/cached/dummy_models_controller.rb +14 -0
- data/spec/dummy/app/controllers/cached_defaults/dummy_models_controller.rb +11 -0
- data/spec/dummy/config/routes.rb +16 -0
- data/spec/dummy_model_blueprint_spec.rb +16 -0
- data/spec/simple_crud/action_context_spec.rb +41 -0
- data/spec/simple_crud/index_context_spec.rb +31 -0
- data/spec/simple_crud/rspec_config_spec.rb +13 -0
- data/spec/simple_crud/serializer_spec.rb +28 -0
- data/spec/simple_crud/show_context_spec.rb +14 -0
- metadata +28 -5
- data/lib/simple_crud/controller_helpers.rb +0 -128
- data/spec/simple_crud/controller_helpers_spec.rb +0 -49
|
@@ -11,8 +11,11 @@ module SimpleCrud
|
|
|
11
11
|
Rack::Utils::SYMBOL_TO_STATUS_CODE.invert[422]
|
|
12
12
|
end
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
# Rails copies the controller-test `format:` kwarg into params, which
|
|
15
|
+
# collides with a model attribute named `format`. Only HTML requests
|
|
16
|
+
# need it, so leave params untouched for JSON.
|
|
17
|
+
def format_param(action)
|
|
18
|
+
check_html(action) ? :html : nil
|
|
16
19
|
end
|
|
17
20
|
|
|
18
21
|
# Extra params (e.g. a parent slug) added to every request, for nested
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SimpleCrud
|
|
4
|
+
# Renders a record with the app's serializer stack.
|
|
5
|
+
#
|
|
6
|
+
# Blueprinter blueprints respond to `render_as_hash` and receive the
|
|
7
|
+
# render options, so a blueprint can read `url_builder:` (or any other
|
|
8
|
+
# option) from them. For anything else, simple_crud assumes an
|
|
9
|
+
# ActiveModelSerializers-style serializer and calls `new(record, options)`.
|
|
10
|
+
# A `nil` serializer renders `record.as_json`.
|
|
11
|
+
module Serializer
|
|
12
|
+
def self.render(serializer, record, options = {})
|
|
13
|
+
return record.as_json if serializer.nil?
|
|
14
|
+
return serializer.render_as_hash(record, options) if serializer.respond_to?(:render_as_hash)
|
|
15
|
+
|
|
16
|
+
serializer.new(record, options).as_json
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SimpleCrud
|
|
4
|
+
class ShowContext < ActionContext
|
|
5
|
+
private
|
|
6
|
+
|
|
7
|
+
def action_name = :show
|
|
8
|
+
|
|
9
|
+
def run
|
|
10
|
+
cache_opts = parameters[:cache]
|
|
11
|
+
if cache_opts
|
|
12
|
+
render_cached(cache_opts) { payload }
|
|
13
|
+
else
|
|
14
|
+
record = find_record
|
|
15
|
+
maybe_authorize(record)
|
|
16
|
+
render_record(record, :show)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def payload
|
|
21
|
+
record = find_record
|
|
22
|
+
maybe_authorize(record)
|
|
23
|
+
controller.instance_variable_set(:@record, record)
|
|
24
|
+
return controller.instance_exec(record, &block) if block
|
|
25
|
+
|
|
26
|
+
SimpleCrud::Serializer.render(parameters[:serializer], record, serializer_options(record))
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -2,15 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
require 'active_support/all'
|
|
4
4
|
require_relative 'config'
|
|
5
|
-
require_relative '
|
|
5
|
+
require_relative 'serializer'
|
|
6
|
+
require_relative 'cache_helpers'
|
|
7
|
+
require_relative 'action_context'
|
|
8
|
+
require_relative 'persistence_context'
|
|
9
|
+
require_relative 'show_context'
|
|
10
|
+
require_relative 'index_context'
|
|
11
|
+
require_relative 'new_context'
|
|
12
|
+
require_relative 'edit_context'
|
|
13
|
+
require_relative 'create_context'
|
|
14
|
+
require_relative 'update_context'
|
|
15
|
+
require_relative 'destroy_context'
|
|
6
16
|
require_relative 'action_lambdas'
|
|
7
17
|
|
|
8
18
|
# Extended onto a controller for CRUD actions.
|
|
9
19
|
module SimpleCrudController
|
|
10
|
-
extend SimpleCrud::ControllerHelpers
|
|
11
|
-
# Include, not extend: controllers gain these by extending SimpleCrudController.
|
|
12
20
|
include SimpleCrud::ActionLambdas
|
|
13
21
|
|
|
22
|
+
def self.extended(base)
|
|
23
|
+
base.include(SimpleCrud::CacheHelpers)
|
|
24
|
+
end
|
|
25
|
+
|
|
14
26
|
# Possible options:
|
|
15
27
|
### authorize: check authorization via Config.authorization_adapter (Pundit by default)
|
|
16
28
|
### paginate: paginate the list via Config.pagination_adapter (wor-paginate by default)
|
|
@@ -52,7 +64,8 @@ module SimpleCrudController
|
|
|
52
64
|
def parameters_with_defaults(parameters)
|
|
53
65
|
defaults = {
|
|
54
66
|
authorize: true, paginate: true, authenticate: true, authenticate_headers: nil,
|
|
55
|
-
serializer: nil,
|
|
67
|
+
serializer: nil, serializer_options: nil, status: nil, after_persist: nil, html: false,
|
|
68
|
+
finder: nil, scope: nil, build: nil, raise_on_invalid: false
|
|
56
69
|
}
|
|
57
70
|
defaults.merge(simple_crud_inherited_defaults).each do |key, value|
|
|
58
71
|
parameters[key] = value unless parameters.key?(key)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SimpleCrud
|
|
4
|
+
class UpdateContext < PersistenceContext
|
|
5
|
+
private
|
|
6
|
+
|
|
7
|
+
def run
|
|
8
|
+
record = find_record
|
|
9
|
+
maybe_authorize(record)
|
|
10
|
+
attrs = permitted_params
|
|
11
|
+
persist = ->(bang:) { bang ? record.update!(attrs) : record.update(attrs) }
|
|
12
|
+
options = { status: parameters[:status] || :ok, failure_template: :edit,
|
|
13
|
+
redirect: parameters[:redirect] || record }
|
|
14
|
+
persist_and_render(record, options, persist)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
data/lib/simple_crud/version.rb
CHANGED
|
@@ -4,12 +4,29 @@ RSpec::Matchers.define :have_been_serialized_with do |serializer|
|
|
|
4
4
|
# TODO: Add support for optional relations `has_many :zarasa, if: zarasa2`
|
|
5
5
|
# TODO: Support multilevel serialization
|
|
6
6
|
match do |json_response|
|
|
7
|
+
attributes = serializer_attributes(serializer)
|
|
8
|
+
reflections = serializer_reflections(serializer)
|
|
9
|
+
|
|
7
10
|
[json_response].flatten.all? do |json_response_item|
|
|
8
|
-
all_attributes_included?(
|
|
9
|
-
all_attributes_included?(
|
|
11
|
+
all_attributes_included?(attributes, json_response_item) &&
|
|
12
|
+
all_attributes_included?(reflections, json_response_item)
|
|
10
13
|
end
|
|
11
14
|
end
|
|
12
15
|
|
|
16
|
+
# Blueprinter blueprints expose their fields through the view collection;
|
|
17
|
+
# ActiveModelSerializers expose `._attributes` / `._reflections`.
|
|
18
|
+
def serializer_attributes(serializer)
|
|
19
|
+
return serializer._attributes unless serializer.respond_to?(:view_collection)
|
|
20
|
+
|
|
21
|
+
serializer.view_collection.fields_for(:default).map(&:name)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def serializer_reflections(serializer)
|
|
25
|
+
return serializer._reflections.keys unless serializer.respond_to?(:view_collection)
|
|
26
|
+
|
|
27
|
+
[]
|
|
28
|
+
end
|
|
29
|
+
|
|
13
30
|
failure_message do |json_response|
|
|
14
31
|
"expected attributes of #{serializer} serializer to be included in #{json_response}"
|
|
15
32
|
end
|
|
@@ -4,7 +4,7 @@ RSpec.shared_examples 'simple crud for create' do
|
|
|
4
4
|
describe 'POST #create' do
|
|
5
5
|
context 'without authenticated user' do
|
|
6
6
|
subject!(:req) do
|
|
7
|
-
post :create, params: with_route_params(body_params(params_for(model_class))), format:
|
|
7
|
+
post :create, params: with_route_params(body_params(params_for(model_class))), format: format_param(:create)
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
include_examples 'unauthorized when not logged in' if check_authenticate(:create)
|
|
@@ -16,7 +16,7 @@ RSpec.shared_examples 'simple crud for create' do
|
|
|
16
16
|
|
|
17
17
|
before do
|
|
18
18
|
make_policies_fail(:create)
|
|
19
|
-
post :create, params: with_route_params(body_params(model_params)), format:
|
|
19
|
+
post :create, params: with_route_params(body_params(model_params)), format: format_param(:create)
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
it 'fails with forbidden' do
|
|
@@ -30,7 +30,7 @@ RSpec.shared_examples 'simple crud for create' do
|
|
|
30
30
|
let(:create_params) { model_params.merge(owner_params) }
|
|
31
31
|
|
|
32
32
|
before do
|
|
33
|
-
post :create, params: with_route_params(body_params(create_params)), format:
|
|
33
|
+
post :create, params: with_route_params(body_params(create_params)), format: format_param(:create)
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
if check_html(:create)
|
|
@@ -38,8 +38,8 @@ RSpec.shared_examples 'simple crud for create' do
|
|
|
38
38
|
expect(response).to have_http_status(:found)
|
|
39
39
|
end
|
|
40
40
|
else
|
|
41
|
-
it 'response with
|
|
42
|
-
expect(response).to have_http_status(:created)
|
|
41
|
+
it 'response with the configured status code' do
|
|
42
|
+
expect(response).to have_http_status(check_status(:create) || :created)
|
|
43
43
|
end
|
|
44
44
|
|
|
45
45
|
it 'creates an article with valid attributes' do
|
|
@@ -58,7 +58,7 @@ RSpec.shared_examples 'simple crud for create' do
|
|
|
58
58
|
include_context 'with authenticated user' if check_authenticate(:create)
|
|
59
59
|
|
|
60
60
|
before do
|
|
61
|
-
post :create, params: with_route_params(body_params(owner_params)), format:
|
|
61
|
+
post :create, params: with_route_params(body_params(owner_params)), format: format_param(:create)
|
|
62
62
|
end
|
|
63
63
|
|
|
64
64
|
unless check_block(:create)
|
|
@@ -7,7 +7,7 @@ RSpec.shared_examples 'simple crud for create with block' do
|
|
|
7
7
|
let(:create_params) { model_params.merge(owner_params) }
|
|
8
8
|
|
|
9
9
|
before do
|
|
10
|
-
post :create, params: with_route_params(body_params(create_params)), format:
|
|
10
|
+
post :create, params: with_route_params(body_params(create_params)), format: format_param(:create)
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
it 'passes the saved record to the block' do
|
|
@@ -24,7 +24,7 @@ RSpec.shared_examples 'simple crud for create with block' do
|
|
|
24
24
|
include_context 'with authenticated user' if check_authenticate(:create)
|
|
25
25
|
|
|
26
26
|
before do
|
|
27
|
-
post :create, params: with_route_params(body_params(owner_params)), format:
|
|
27
|
+
post :create, params: with_route_params(body_params(owner_params)), format: format_param(:create)
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
it 'does not create a model' do
|
|
@@ -5,7 +5,7 @@ RSpec.shared_examples 'simple crud for create with build' do
|
|
|
5
5
|
include_context 'with authenticated user' if check_authenticate(:create)
|
|
6
6
|
|
|
7
7
|
before do
|
|
8
|
-
post :create, params: with_route_params(body_params(params_for(model_class))), format:
|
|
8
|
+
post :create, params: with_route_params(body_params(params_for(model_class))), format: format_param(:create)
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
it 'creates the record owned by the current user' do
|
|
@@ -5,7 +5,7 @@ RSpec.shared_examples 'simple crud for destroy' do
|
|
|
5
5
|
context 'without authenticated user' do
|
|
6
6
|
subject!(:req) do
|
|
7
7
|
delete :destroy, params: with_route_params(record_param(:destroy, nil, not_found: true)),
|
|
8
|
-
format:
|
|
8
|
+
format: format_param(:destroy)
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
include_examples 'unauthorized when not logged in' if check_authenticate(:destroy)
|
|
@@ -17,7 +17,7 @@ RSpec.shared_examples 'simple crud for destroy' do
|
|
|
17
17
|
|
|
18
18
|
before do
|
|
19
19
|
make_policies_fail(:destroy)
|
|
20
|
-
delete :destroy, params: with_route_params(record_param(:destroy, model)), format:
|
|
20
|
+
delete :destroy, params: with_route_params(record_param(:destroy, model)), format: format_param(:destroy)
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
it 'fails with forbidden' do
|
|
@@ -31,7 +31,7 @@ RSpec.shared_examples 'simple crud for destroy' do
|
|
|
31
31
|
|
|
32
32
|
before do
|
|
33
33
|
model
|
|
34
|
-
delete :destroy, params: with_route_params(record_param(:destroy, model)), format:
|
|
34
|
+
delete :destroy, params: with_route_params(record_param(:destroy, model)), format: format_param(:destroy)
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
if check_html(:destroy)
|
|
@@ -40,8 +40,8 @@ RSpec.shared_examples 'simple crud for destroy' do
|
|
|
40
40
|
expect(model_class_object.exists?(model.id)).to be false
|
|
41
41
|
end
|
|
42
42
|
else
|
|
43
|
-
it 'response with
|
|
44
|
-
expect(response).to have_http_status(:ok)
|
|
43
|
+
it 'response with the configured status code' do
|
|
44
|
+
expect(response).to have_http_status(check_status(:destroy) || :ok)
|
|
45
45
|
end
|
|
46
46
|
end
|
|
47
47
|
end
|
|
@@ -52,8 +52,8 @@ RSpec.shared_examples 'simple crud for destroy' do
|
|
|
52
52
|
before do
|
|
53
53
|
model
|
|
54
54
|
allow(model).to receive(:destroy).and_return(false)
|
|
55
|
-
|
|
56
|
-
delete :destroy, params: with_route_params(record_param(:destroy, model)), format:
|
|
55
|
+
allow_any_instance_of(SimpleCrud::DestroyContext).to receive(:find_record).and_return(model)
|
|
56
|
+
delete :destroy, params: with_route_params(record_param(:destroy, model)), format: format_param(:destroy)
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
it 'keeps the record' do
|
|
@@ -77,7 +77,7 @@ RSpec.shared_examples 'simple crud for destroy' do
|
|
|
77
77
|
before do
|
|
78
78
|
model
|
|
79
79
|
delete :destroy, params: with_route_params(record_param(:destroy, nil, not_found: true)),
|
|
80
|
-
format:
|
|
80
|
+
format: format_param(:destroy)
|
|
81
81
|
end
|
|
82
82
|
|
|
83
83
|
it 'responds with not found status' do
|
|
@@ -92,7 +92,7 @@ RSpec.shared_examples 'simple crud for destroy' do
|
|
|
92
92
|
before do
|
|
93
93
|
model
|
|
94
94
|
make_policies_fail(:destroy)
|
|
95
|
-
delete :destroy, params: with_route_params(record_param(:destroy, model)), format:
|
|
95
|
+
delete :destroy, params: with_route_params(record_param(:destroy, model)), format: format_param(:destroy)
|
|
96
96
|
end
|
|
97
97
|
|
|
98
98
|
it 'responds with forbidden status' do
|
|
@@ -7,7 +7,7 @@ RSpec.shared_examples 'simple crud for destroy with block' do
|
|
|
7
7
|
let!(:record) { model }
|
|
8
8
|
|
|
9
9
|
before do
|
|
10
|
-
delete :destroy, params: with_route_params(record_param(:destroy, record)), format:
|
|
10
|
+
delete :destroy, params: with_route_params(record_param(:destroy, record)), format: format_param(:destroy)
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
it 'passes the destroyed record to the block' do
|
|
@@ -8,7 +8,7 @@ RSpec.shared_examples 'simple crud for destroy with finder' do
|
|
|
8
8
|
before do
|
|
9
9
|
model
|
|
10
10
|
delete :destroy, params: with_route_params(finder_key => model.public_send(finder_key)),
|
|
11
|
-
format:
|
|
11
|
+
format: format_param(:destroy)
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
it 'destroys the model' do
|
|
@@ -24,8 +24,8 @@ RSpec.shared_examples 'simple crud for destroy with finder' do
|
|
|
24
24
|
expect(response).to have_http_status(:found)
|
|
25
25
|
end
|
|
26
26
|
else
|
|
27
|
-
it 'responds with
|
|
28
|
-
expect(response).to have_http_status(:ok)
|
|
27
|
+
it 'responds with the configured status code' do
|
|
28
|
+
expect(response).to have_http_status(check_status(:destroy) || :ok)
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
31
|
end
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
RSpec.shared_examples 'simple crud for edit' do
|
|
4
4
|
describe 'GET #edit' do
|
|
5
|
-
subject(:edit_request) { get :edit, params: with_route_params(edit_params), format:
|
|
5
|
+
subject(:edit_request) { get :edit, params: with_route_params(edit_params), format: format_param(:edit) }
|
|
6
6
|
|
|
7
7
|
before do
|
|
8
8
|
model
|
|
@@ -10,7 +10,7 @@ RSpec.shared_examples 'simple crud for edit' do
|
|
|
10
10
|
|
|
11
11
|
context 'without authenticated user' do
|
|
12
12
|
subject!(:req) do
|
|
13
|
-
get :edit, params: with_route_params(record_param(:edit, model)), format:
|
|
13
|
+
get :edit, params: with_route_params(record_param(:edit, model)), format: format_param(:edit)
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
include_examples 'unauthorized when not logged in' if check_authenticate(:edit)
|
|
@@ -14,7 +14,7 @@ RSpec.shared_examples 'simple crud for index' do
|
|
|
14
14
|
|
|
15
15
|
before do
|
|
16
16
|
make_policies_succeed(:index) if check_authorize(:index)
|
|
17
|
-
get :index, params: with_route_params({}), format:
|
|
17
|
+
get :index, params: with_route_params({}), format: format_param(:index)
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
if check_html(:index)
|
|
@@ -25,7 +25,7 @@ RSpec.shared_examples 'simple crud for index' do
|
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
it 'renders the correct paginated models' do
|
|
28
|
-
expect(response_body['page'].map { |a| a['id'] }).to
|
|
28
|
+
expect(response_body['page'].map { |a| a['id'] }).to match_array(model_class_object.all.map(&:id))
|
|
29
29
|
end
|
|
30
30
|
else
|
|
31
31
|
it 'renders unpaginated models correctly serialized' do
|
|
@@ -33,7 +33,7 @@ RSpec.shared_examples 'simple crud for index' do
|
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
it 'renders the correct unpaginated models' do
|
|
36
|
-
expect(response_body.map { |a| a['id'] }).to
|
|
36
|
+
expect(response_body.map { |a| a['id'] }).to match_array(model_class_object.all.map(&:id))
|
|
37
37
|
end
|
|
38
38
|
end
|
|
39
39
|
end
|
|
@@ -8,7 +8,7 @@ RSpec.shared_examples 'simple crud for index with block' do
|
|
|
8
8
|
|
|
9
9
|
before do
|
|
10
10
|
created_models
|
|
11
|
-
get :index, params: with_route_params({}), format:
|
|
11
|
+
get :index, params: with_route_params({}), format: format_param(:index)
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
if check_html(:index)
|
|
@@ -13,7 +13,7 @@ RSpec.shared_examples 'simple crud for index with scope' do
|
|
|
13
13
|
make_policies_succeed(:index) if check_authorize(:index)
|
|
14
14
|
my_models
|
|
15
15
|
other_models
|
|
16
|
-
get :index, params: with_route_params(request_params), format:
|
|
16
|
+
get :index, params: with_route_params(request_params), format: format_param(:index)
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
if check_html(:index)
|
|
@@ -10,7 +10,7 @@ RSpec.shared_examples 'simple crud for new' do
|
|
|
10
10
|
|
|
11
11
|
before do
|
|
12
12
|
make_policies_succeed(:new) if check_authorize(:new)
|
|
13
|
-
get :new, params: with_route_params({}), format:
|
|
13
|
+
get :new, params: with_route_params({}), format: format_param(:new)
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
it 'responds with ok status' do
|
|
@@ -6,7 +6,7 @@ RSpec.shared_examples 'simple crud for new with block' do
|
|
|
6
6
|
|
|
7
7
|
before do
|
|
8
8
|
make_policies_succeed(:new) if check_authorize(:new)
|
|
9
|
-
get :new, params: with_route_params({}), format:
|
|
9
|
+
get :new, params: with_route_params({}), format: format_param(:new)
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
if check_html(:new)
|
|
@@ -5,7 +5,7 @@ RSpec.shared_examples 'simple crud for new with build' do
|
|
|
5
5
|
include_context 'with authenticated user' if check_authenticate(:new)
|
|
6
6
|
|
|
7
7
|
before do
|
|
8
|
-
get :new, params: with_route_params({}), format:
|
|
8
|
+
get :new, params: with_route_params({}), format: format_param(:new)
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
it 'builds the record owned by the current user', :aggregate_failures do
|
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
RSpec.shared_examples 'simple crud for show' do
|
|
4
4
|
describe 'GET #show' do
|
|
5
|
-
subject(:show_request) { get :show, params: with_route_params(show_params), format:
|
|
5
|
+
subject(:show_request) { get :show, params: with_route_params(show_params), format: format_param(: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: with_route_params(record_param(:show, model)), format:
|
|
12
|
+
subject!(:req) { get :show, params: with_route_params(record_param(:show, model)), format: format_param(:show) }
|
|
13
13
|
|
|
14
14
|
include_examples 'unauthorized when not logged in' if check_authenticate(:show)
|
|
15
15
|
end
|
|
@@ -8,7 +8,7 @@ RSpec.shared_examples 'simple crud for show with block' do
|
|
|
8
8
|
|
|
9
9
|
before do
|
|
10
10
|
make_policies_succeed(:show) if check_authorize(:show)
|
|
11
|
-
get :show, params: with_route_params(record_param(:show, record)), format:
|
|
11
|
+
get :show, params: with_route_params(record_param(:show, record)), format: format_param(:show)
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
if check_html(:show)
|
|
@@ -8,7 +8,7 @@ RSpec.shared_examples 'simple crud for show with finder' do
|
|
|
8
8
|
before do
|
|
9
9
|
model
|
|
10
10
|
get :show, params: with_route_params(finder_key => model.public_send(finder_key)),
|
|
11
|
-
format:
|
|
11
|
+
format: format_param(:show)
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
if check_block(:show)
|
|
@@ -5,7 +5,7 @@ RSpec.shared_examples 'simple crud for update' do
|
|
|
5
5
|
context 'without authenticated user' do
|
|
6
6
|
subject!(:req) do
|
|
7
7
|
update_params = body_params(params_for(model_class)).merge(record_param(:update, model))
|
|
8
|
-
put :update, params: with_route_params(update_params), format:
|
|
8
|
+
put :update, params: with_route_params(update_params), format: format_param(:update)
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
include_examples 'unauthorized when not logged in' if check_authenticate(:update)
|
|
@@ -18,7 +18,7 @@ RSpec.shared_examples 'simple crud for update' do
|
|
|
18
18
|
before do
|
|
19
19
|
make_policies_fail(:update)
|
|
20
20
|
put :update, params: with_route_params(body_params(model_params).merge(record_param(:update, model))),
|
|
21
|
-
format:
|
|
21
|
+
format: format_param(:update)
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
it 'fails with forbidden' do
|
|
@@ -34,7 +34,7 @@ RSpec.shared_examples 'simple crud for update' do
|
|
|
34
34
|
|
|
35
35
|
before do
|
|
36
36
|
model
|
|
37
|
-
put :update, params: with_route_params(update_params), format:
|
|
37
|
+
put :update, params: with_route_params(update_params), format: format_param(:update)
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
if check_html(:update)
|
|
@@ -42,8 +42,8 @@ RSpec.shared_examples 'simple crud for update' do
|
|
|
42
42
|
expect(response).to have_http_status(:found)
|
|
43
43
|
end
|
|
44
44
|
else
|
|
45
|
-
it 'response with
|
|
46
|
-
expect(response).to have_http_status(:ok)
|
|
45
|
+
it 'response with the configured status code' do
|
|
46
|
+
expect(response).to have_http_status(check_status(:update) || :ok)
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
it 'updates an model' do
|
|
@@ -66,7 +66,7 @@ RSpec.shared_examples 'simple crud for update' do
|
|
|
66
66
|
|
|
67
67
|
before do
|
|
68
68
|
put :update, params: with_route_params(record_param(:update, nil, not_found: true)),
|
|
69
|
-
format:
|
|
69
|
+
format: format_param(:update)
|
|
70
70
|
end
|
|
71
71
|
|
|
72
72
|
it 'response with 404 status code' do
|
|
@@ -86,7 +86,7 @@ RSpec.shared_examples 'simple crud for update' do
|
|
|
86
86
|
model
|
|
87
87
|
put :update, params: with_route_params(body_params({ required_attribute => nil }.merge(owner_params))
|
|
88
88
|
.merge(record_param(:update, model))),
|
|
89
|
-
format:
|
|
89
|
+
format: format_param(:update)
|
|
90
90
|
end
|
|
91
91
|
|
|
92
92
|
unless check_block(:update)
|
|
@@ -12,7 +12,7 @@ RSpec.shared_examples 'simple crud for update with finder' do
|
|
|
12
12
|
|
|
13
13
|
before do
|
|
14
14
|
model
|
|
15
|
-
put :update, params: update_params, format:
|
|
15
|
+
put :update, params: update_params, format: format_param(:update)
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
it 'updates the model' do
|
|
@@ -4,7 +4,7 @@ RSpec.shared_examples 'simple crud not found with finder' do |http_method, actio
|
|
|
4
4
|
context 'when the model does not exist' do
|
|
5
5
|
before do
|
|
6
6
|
send(http_method, action, params: with_route_params(finder_key => "nonexistent-#{finder_key}"),
|
|
7
|
-
format:
|
|
7
|
+
format: format_param(action))
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
it 'responds with not found status' do
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe Blueprint::DummyModelsController, type: :controller do
|
|
6
|
+
let(:serializer) { DummyModelBlueprint }
|
|
7
|
+
|
|
8
|
+
include_examples 'simple crud for index'
|
|
9
|
+
include_examples 'simple crud for show'
|
|
10
|
+
include_examples 'simple crud for create'
|
|
11
|
+
include_examples 'simple crud for update'
|
|
12
|
+
include_examples 'simple crud for destroy'
|
|
13
|
+
|
|
14
|
+
describe 'GET #show' do
|
|
15
|
+
include_context 'with authenticated user'
|
|
16
|
+
|
|
17
|
+
let!(:record) { create(:dummy_model, user: current_user) }
|
|
18
|
+
|
|
19
|
+
it 'passes serializer_options to the blueprint' do
|
|
20
|
+
get :show, params: { id: record.id }
|
|
21
|
+
|
|
22
|
+
expect(response.parsed_body['url']).to eq(dummy_model_url(record))
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe Cached::DummyModelsController, type: :controller do
|
|
6
|
+
before { Rails.cache.clear }
|
|
7
|
+
|
|
8
|
+
include_examples 'simple crud for show with finder'
|
|
9
|
+
|
|
10
|
+
describe 'GET #show' do
|
|
11
|
+
let!(:record) { create(:dummy_model) }
|
|
12
|
+
|
|
13
|
+
it 'serves from cache on repeated requests' do
|
|
14
|
+
get :show, params: { slug: record.slug }
|
|
15
|
+
first_body = response.parsed_body
|
|
16
|
+
|
|
17
|
+
record.update!(name: 'updated')
|
|
18
|
+
get :show, params: { slug: record.slug }
|
|
19
|
+
|
|
20
|
+
expect(response.parsed_body).to eq(first_body)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe CachedDefaults::DummyModelsController, type: :controller do
|
|
6
|
+
before { Rails.cache.clear }
|
|
7
|
+
|
|
8
|
+
include_examples 'simple crud for show'
|
|
9
|
+
|
|
10
|
+
describe 'GET #index' do
|
|
11
|
+
before { create_list(:dummy_model, 2) }
|
|
12
|
+
|
|
13
|
+
it 'returns all records' do
|
|
14
|
+
get :index
|
|
15
|
+
expect(response.parsed_body.length).to eq(2)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
describe 'expire_simple_crud_cache' do
|
|
20
|
+
let!(:record) { create(:dummy_model) }
|
|
21
|
+
|
|
22
|
+
context 'when called after a cached show' do
|
|
23
|
+
before { get :show, params: { id: record.id } }
|
|
24
|
+
|
|
25
|
+
it 'clears the entry so the next request fetches fresh data' do
|
|
26
|
+
first_body = response.parsed_body
|
|
27
|
+
record.update!(name: 'updated')
|
|
28
|
+
controller.expire_simple_crud_cache(:show)
|
|
29
|
+
get :show, params: { id: record.id }
|
|
30
|
+
expect(response.parsed_body).not_to eq(first_body)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|