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
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NestedRoute
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, :classroom_slug)
16
+ end
17
+
18
+ NESTED_FINDER = lambda do |params|
19
+ DummyModel.find_by!(classroom_slug: params[:classroom_slug], slug: params[:slug])
20
+ end
21
+
22
+ simple_crud_for :show, finder: NESTED_FINDER
23
+ simple_crud_for :destroy, finder: NESTED_FINDER
24
+ simple_crud_for :create
25
+ end
26
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Redirect
4
+ class DummyModelsController < HtmlModes::BaseController
5
+ simple_crud_for :create, html: true, authenticate: false, authorize: false,
6
+ redirect: ->(_record) { dummy_models_path }
7
+ simple_crud_for :destroy, html: true, authenticate: false, authorize: false,
8
+ redirect: '/block_new/dummy_models/new'
9
+ end
10
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RedirectAuth
4
+ class UnauthenticatedError < StandardError; end
5
+
6
+ class DummyModelsController < ApplicationController
7
+ include Wor::Paginate
8
+ include Pundit::Authorization
9
+ extend SimpleCrudController
10
+
11
+ rescue_from UnauthenticatedError, with: :redirect_to_login
12
+
13
+ before_action :set_params
14
+ def set_params
15
+ SimpleCrudController.params = params
16
+ end
17
+
18
+ def authenticate_user!
19
+ raise UnauthenticatedError unless current_user
20
+ end
21
+
22
+ def redirect_to_login(_exception)
23
+ redirect_to '/login'
24
+ end
25
+
26
+ simple_crud_for :index, authorize: false
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Scoped
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, scope: ->(user) { DummyModel.where(user: user) }
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ScopedParams
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, scope: ->(user, params) { DummyModel.where(user: user, something: params[:something]) }
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Strict
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 :create, raise_on_invalid: true
19
+ simple_crud_for :update, raise_on_invalid: true
20
+ end
21
+ 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
@@ -4,4 +4,8 @@ require_relative 'dummy_model_policy'
4
4
  class DummyModel < ApplicationRecord
5
5
  belongs_to :user
6
6
  validates :name, presence: true
7
+
8
+ def self.find_by_slug(params)
9
+ find_by!(slug: params[:slug])
10
+ end
7
11
  end
@@ -20,15 +20,23 @@ class DummyModelPolicy
20
20
  check_user
21
21
  end
22
22
 
23
+ def edit?
24
+ update?
25
+ end
26
+
23
27
  def create?
24
28
  check_user
25
29
  end
26
30
 
31
+ def new?
32
+ check_user
33
+ end
34
+
27
35
  def destroy?
28
36
  check_user
29
37
  end
30
38
 
31
39
  def index?
32
- check_user
40
+ user.present?
33
41
  end
34
42
  end
@@ -0,0 +1,3 @@
1
+ <% models.each do |model| %>
2
+ <span class="model"><%= model.name %></span>
3
+ <% end %>
@@ -0,0 +1 @@
1
+ <span class="record">new <%= model.new_record? %></span>
@@ -0,0 +1 @@
1
+ <span class="record"><%= model.name %></span>
@@ -0,0 +1,3 @@
1
+ <% @records.each do |record| %>
2
+ <span class="record"><%= record.name %></span>
3
+ <% end %>
@@ -0,0 +1 @@
1
+ <span class="record">errors: <%= @record.errors.full_messages.join(',') %></span>
@@ -0,0 +1 @@
1
+ <span class="record"><%= @record.name %></span>
@@ -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>
@@ -0,0 +1 @@
1
+ <span class="record">new <%= @record.new_record? %></span>
@@ -0,0 +1,3 @@
1
+ <% @records.each do |record| %>
2
+ <span class="record"><%= record.name %></span>
3
+ <% end %>
@@ -0,0 +1 @@
1
+ <span class="record"><%= @record.name %></span>
@@ -0,0 +1 @@
1
+ <span class="record">errors: <%= @record.errors.full_messages.join(',') %></span>
@@ -0,0 +1 @@
1
+ <span class="record">errors: <%= @record.errors.full_messages.join(',') %></span>
@@ -0,0 +1,4 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <body><%= yield %></body>
4
+ </html>
@@ -7,4 +7,114 @@ Rails.application.routes.draw do
7
7
  namespace :without_pagination do
8
8
  resources :dummy_models, only: :index
9
9
  end
10
+
11
+ namespace :html do
12
+ resources :dummy_models, only: :index
13
+ end
14
+
15
+ namespace :block do
16
+ resources :dummy_models, only: :index
17
+ end
18
+
19
+ namespace :finder do
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
29
+ end
30
+
31
+ namespace :scoped do
32
+ resources :dummy_models, only: :index
33
+ end
34
+
35
+ namespace :unpaginated_scoped do
36
+ resources :dummy_models, only: :index
37
+ end
38
+
39
+ namespace :scoped_params do
40
+ resources :dummy_models, only: :index
41
+ end
42
+
43
+ namespace :strict do
44
+ resources :dummy_models, only: %i[create update]
45
+ end
46
+
47
+ namespace :html_show do
48
+ resources :dummy_models, only: :show
49
+ end
50
+
51
+ namespace :block_show do
52
+ resources :dummy_models, only: :show
53
+ end
54
+
55
+ namespace :html_new do
56
+ resources :dummy_models, only: :new
57
+ end
58
+
59
+ namespace :block_new do
60
+ resources :dummy_models, only: :new
61
+ end
62
+
63
+ namespace :html_create do
64
+ resources :dummy_models, only: :create
65
+ end
66
+
67
+ namespace :redirect do
68
+ resources :dummy_models, only: %i[create destroy]
69
+ end
70
+
71
+ namespace :block_create do
72
+ resources :dummy_models, only: :create
73
+ end
74
+
75
+ namespace :html_update do
76
+ resources :dummy_models, only: :update
77
+ end
78
+
79
+ namespace :html_destroy do
80
+ resources :dummy_models, only: :destroy
81
+ end
82
+
83
+ namespace :block_destroy do
84
+ resources :dummy_models, only: :destroy
85
+ end
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
+
95
+ namespace :built do
96
+ resources :dummy_models, only: %i[new create]
97
+ end
98
+
99
+ namespace :redirect_auth do
100
+ resources :dummy_models, only: :index
101
+ end
102
+
103
+ namespace :html_scoped do
104
+ resources :dummy_models, only: :index
105
+ end
106
+
107
+ namespace :nested do
108
+ resources :dummy_models, only: %i[create update]
109
+ end
110
+
111
+ namespace :invalid_status do
112
+ resources :dummy_models, only: :create
113
+ end
114
+
115
+ namespace :nested_route do
116
+ resources :classrooms, only: [], param: :slug do
117
+ resources :dummy_models, param: :slug, only: %i[show create destroy]
118
+ end
119
+ end
10
120
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddSlugToDummyModels < ActiveRecord::Migration[6.0]
4
+ def change
5
+ add_column :dummy_models, :slug, :string
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddClassroomSlugToDummyModels < ActiveRecord::Migration[6.0]
4
+ def change
5
+ add_column :dummy_models, :classroom_slug, :string
6
+ end
7
+ end
@@ -12,9 +12,11 @@
12
12
  #
13
13
  # It's strongly recommended that you check this file into your version control system.
14
14
 
15
- ActiveRecord::Schema.define(version: 20_200_120_165_657) do
15
+ ActiveRecord::Schema.define(version: 20_260_807_000_000) do
16
16
  create_table 'dummy_models', force: :cascade do |t|
17
17
  t.string 'name'
18
+ t.string 'slug'
19
+ t.string 'classroom_slug'
18
20
  t.integer 'something'
19
21
  t.integer 'user_id'
20
22
  t.index ['user_id'], name: 'index_dummy_models_on_user_id'
@@ -4,6 +4,7 @@ FactoryBot.define do
4
4
  factory :dummy_model do
5
5
  name { rand(36 * 4).to_s(36) }
6
6
  something { rand(100) }
7
+ slug { "slug-#{rand(36 * 6).to_s(36)}" }
7
8
  user
8
9
  end
9
10
  end
@@ -7,6 +7,7 @@ describe DummyModelsController, type: :controller do
7
7
  include_examples 'simple crud for create'
8
8
  include_examples 'simple crud for index'
9
9
  include_examples 'simple crud for destroy'
10
+ include_examples 'simple crud for new'
10
11
 
11
12
  describe 'POST #create with invalid attributes' do
12
13
  include_context 'with authenticated user'
@@ -17,13 +17,15 @@ describe DummyModelPolicy do
17
17
  end
18
18
  end
19
19
 
20
+ # :index authorizes the model class, not an instance, so the policy can
21
+ # only reason about the user.
20
22
  describe '#index?' do
21
- it 'allows the owning user' do
22
- expect(described_class.new(user, dummy_model).index?).to be true
23
+ it 'allows any signed-in user' do
24
+ expect(described_class.new(other_user, dummy_model).index?).to be true
23
25
  end
24
26
 
25
- it 'denies a different user' do
26
- expect(described_class.new(other_user, dummy_model).index?).to be false
27
+ it 'denies an anonymous user' do
28
+ expect(described_class.new(nil, dummy_model).index?).to be false
27
29
  end
28
30
  end
29
31
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Finder::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 show'
11
+ include_examples 'simple crud for update'
12
+ include_examples 'simple crud for destroy'
13
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Html::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for index'
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe HtmlCreate::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for create'
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe HtmlDestroy::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for destroy'
7
+ end
@@ -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
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe HtmlNew::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for new'
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe HtmlScoped::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for index with scope'
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe HtmlShow::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for show'
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe HtmlUpdate::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for update'
7
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe InvalidStatus::DummyModelsController, type: :controller do
6
+ before { SimpleCrud::RSpec.configure { |c| c.invalid_status = 422 } }
7
+ after { SimpleCrud::RSpec.configure { |c| c.invalid_status = :ok } }
8
+
9
+ include_examples 'simple crud for create'
10
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Nested::DummyModelsController, type: :controller do
6
+ around do |example|
7
+ with_config_override(:params_key, :dummy_model) { example.run }
8
+ end
9
+
10
+ include_examples 'simple crud for create'
11
+ include_examples 'simple crud for update'
12
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe NestedRoute::DummyModelsController, type: :controller do
6
+ let(:model_attributes) { { classroom_slug: 'room-1', user: current_user } }
7
+
8
+ around do |example|
9
+ with_config_override(:route_params, -> { { classroom_slug: 'room-1' } }) do
10
+ with_config_override(:params_key, :dummy_model) do
11
+ example.run
12
+ end
13
+ end
14
+ end
15
+
16
+ include_examples 'simple crud for show'
17
+ include_examples 'simple crud for create'
18
+ include_examples 'simple crud for destroy'
19
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Redirect::DummyModelsController, type: :controller do
6
+ describe 'POST #create' do
7
+ it 'redirects to the target resolved from the redirect proc' do
8
+ post :create, params: attributes_for(:dummy_model).merge(user_id: create(:user).id), format: :html
9
+
10
+ expect(response).to redirect_to(dummy_models_path)
11
+ end
12
+ end
13
+
14
+ describe 'DELETE #destroy' do
15
+ it 'redirects to the literal redirect path' do
16
+ record = create(:dummy_model)
17
+
18
+ delete :destroy, params: { id: record.id }, format: :html
19
+
20
+ expect(response).to redirect_to('/block_new/dummy_models/new')
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe RedirectAuth::DummyModelsController, type: :controller do
6
+ around do |example|
7
+ with_config_override(:unauthenticated_status, :found) { example.run }
8
+ end
9
+
10
+ include_examples 'simple crud for index'
11
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Scoped::DummyModelsController, type: :controller do
6
+ include_examples 'simple crud for index with scope'
7
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
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
9
+ include_examples 'simple crud for index with scope' do
10
+ let(:request_params) { { something: 42 } }
11
+ end
12
+ end
@@ -35,15 +35,7 @@ describe SimpleCrud::Authorization::ActionPolicyAdapter do
35
35
  end
36
36
 
37
37
  describe '#authorize' do
38
- it 'passes for a record the policy allows' do
39
- expect { adapter.authorize(controller, ActionPolicyTestRecord.new(1)) }.not_to raise_error
40
- end
41
-
42
- it 'raises for a record the policy denies' do
43
- expect do
44
- adapter.authorize(controller, ActionPolicyTestRecord.new(2))
45
- end.to raise_error(ActionPolicy::Unauthorized)
46
- end
38
+ include_examples 'authorization adapter #authorize', ActionPolicyTestRecord, ActionPolicy::Unauthorized
47
39
  end
48
40
 
49
41
  describe '#policy_defined?' do
@@ -36,13 +36,7 @@ describe SimpleCrud::Authorization::CanCanCanAdapter do
36
36
  end
37
37
 
38
38
  describe '#authorize' do
39
- it 'passes for a record the ability grants' do
40
- expect { adapter.authorize(controller, CanCanCanTestRecord.new(1)) }.not_to raise_error
41
- end
42
-
43
- it 'raises for a record the ability denies' do
44
- expect { adapter.authorize(controller, CanCanCanTestRecord.new(2)) }.to raise_error(CanCan::AccessDenied)
45
- end
39
+ include_examples 'authorization adapter #authorize', CanCanCanTestRecord, CanCan::AccessDenied
46
40
  end
47
41
 
48
42
  describe '#policy_defined?' do