wallaby-core 0.1.2 → 0.2.3

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 (83) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +60 -13
  3. data/app/controllers/wallaby/resources_controller.rb +15 -374
  4. data/app/security/ability.rb +1 -1
  5. data/config/locales/wallaby.en.yml +92 -128
  6. data/config/locales/wallaby_class.en.yml +2 -23
  7. data/lib/adaptors/wallaby/custom/default_provider.rb +1 -1
  8. data/lib/adaptors/wallaby/custom/model_decorator.rb +8 -7
  9. data/lib/adaptors/wallaby/custom/model_finder.rb +3 -2
  10. data/lib/adaptors/wallaby/custom/model_pagination_provider.rb +1 -1
  11. data/lib/adaptors/wallaby/custom/model_service_provider.rb +1 -40
  12. data/lib/authorizers/wallaby/cancancan_authorization_provider.rb +30 -24
  13. data/lib/authorizers/wallaby/default_authorization_provider.rb +6 -13
  14. data/lib/authorizers/wallaby/model_authorizer.rb +43 -67
  15. data/lib/authorizers/wallaby/pundit_authorization_provider.rb +21 -30
  16. data/lib/concerns/wallaby/application_concern.rb +110 -0
  17. data/lib/concerns/wallaby/authentication_concern.rb +162 -0
  18. data/lib/concerns/wallaby/authorizable.rb +8 -14
  19. data/lib/concerns/wallaby/baseable.rb +91 -10
  20. data/lib/concerns/wallaby/decoratable.rb +3 -3
  21. data/lib/concerns/wallaby/engineable.rb +1 -1
  22. data/lib/concerns/wallaby/fieldable.rb +4 -4
  23. data/lib/concerns/wallaby/paginatable.rb +3 -3
  24. data/lib/concerns/wallaby/prefixable.rb +21 -0
  25. data/lib/concerns/wallaby/resourcable.rb +4 -35
  26. data/lib/concerns/wallaby/resources_concern.rb +434 -0
  27. data/lib/concerns/wallaby/servicable.rb +4 -10
  28. data/lib/decorators/wallaby/resource_decorator.rb +53 -80
  29. data/lib/errors/wallaby/{cell_handling.rb → class_not_found.rb} +1 -1
  30. data/lib/errors/wallaby/model_not_found.rb +3 -1
  31. data/lib/errors/wallaby/resource_not_found.rb +1 -1
  32. data/lib/helpers/wallaby/application_helper.rb +6 -9
  33. data/lib/helpers/wallaby/form_helper.rb +2 -9
  34. data/lib/helpers/wallaby/index_helper.rb +2 -14
  35. data/lib/helpers/wallaby/links_helper.rb +5 -5
  36. data/lib/helpers/wallaby/resources_helper.rb +3 -7
  37. data/lib/helpers/wallaby/secure_helper.rb +3 -3
  38. data/lib/helpers/wallaby/styling_helper.rb +17 -3
  39. data/lib/interfaces/wallaby/mode.rb +5 -5
  40. data/lib/interfaces/wallaby/model_authorization_provider.rb +15 -13
  41. data/lib/interfaces/wallaby/model_decorator.rb +15 -3
  42. data/lib/paginators/wallaby/model_paginator.rb +14 -45
  43. data/lib/parsers/wallaby/parser.rb +49 -14
  44. data/lib/routes/wallaby/resources_router.rb +1 -1
  45. data/lib/servicers/wallaby/model_servicer.rb +32 -62
  46. data/lib/services/wallaby/map/mode_mapper.rb +14 -14
  47. data/lib/services/wallaby/map/model_class_collector.rb +2 -2
  48. data/lib/services/wallaby/map/model_class_mapper.rb +7 -26
  49. data/lib/services/wallaby/prefixes_builder.rb +15 -49
  50. data/lib/services/wallaby/type_renderer.rb +3 -13
  51. data/lib/utils/wallaby/locale.rb +53 -0
  52. data/lib/utils/wallaby/model_utils.rb +4 -3
  53. data/lib/utils/wallaby/module_utils.rb +1 -1
  54. data/lib/utils/wallaby/utils.rb +10 -15
  55. data/lib/wallaby/class_array.rb +75 -0
  56. data/lib/wallaby/class_hash.rb +94 -0
  57. data/lib/wallaby/classifier.rb +29 -0
  58. data/lib/wallaby/configuration/mapping.rb +33 -27
  59. data/lib/wallaby/configuration/metadata.rb +1 -1
  60. data/lib/wallaby/configuration/models.rb +5 -9
  61. data/lib/wallaby/configuration/security.rb +6 -3
  62. data/lib/wallaby/configuration/sorting.rb +1 -1
  63. data/lib/wallaby/configuration.rb +31 -2
  64. data/lib/wallaby/constants.rb +10 -8
  65. data/lib/wallaby/core/version.rb +1 -1
  66. data/lib/wallaby/core.rb +21 -16
  67. data/lib/wallaby/engine.rb +9 -20
  68. data/lib/wallaby/logger.rb +35 -0
  69. data/lib/wallaby/map.rb +20 -17
  70. data/lib/wallaby/preloader.rb +77 -0
  71. metadata +48 -22
  72. data/app/controllers/wallaby/application_controller.rb +0 -84
  73. data/app/controllers/wallaby/secure_controller.rb +0 -81
  74. data/lib/concerns/wallaby/rails_overridden_methods.rb +0 -42
  75. data/lib/concerns/wallaby/themeable.rb +0 -40
  76. data/lib/paginators/wallaby/resource_paginator.rb +0 -12
  77. data/lib/renderers/wallaby/cell.rb +0 -137
  78. data/lib/renderers/wallaby/cell_resolver.rb +0 -89
  79. data/lib/renderers/wallaby/custom_lookup_context.rb +0 -64
  80. data/lib/renderers/wallaby/custom_partial_renderer.rb +0 -33
  81. data/lib/renderers/wallaby/custom_renderer.rb +0 -16
  82. data/lib/utils/wallaby/cell_utils.rb +0 -34
  83. data/lib/utils/wallaby/preload_utils.rb +0 -44
data/lib/wallaby/map.rb CHANGED
@@ -1,26 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- # Global storage to hold all the information that Wallaby needs to look up.
4
+ # All the lookups that Wallaby needs.
5
5
  class Map
6
6
  class << self
7
- # To store modes
7
+ include Classifier
8
+
9
+ # @!attribute [w] modes
8
10
  attr_writer :modes
9
11
 
10
- # @return [Array<Class>] descendants of Mode
12
+ # @!attribute [r] modes
13
+ # @return [Array<String>] all {Wallaby::Mode}s
11
14
  def modes
12
- @modes ||= Mode.descendants
15
+ @modes ||= ClassArray.new Mode.descendants
13
16
  end
14
17
 
15
- # @return [Hash] { model => mode }
18
+ # @return [Wallaby::ClassHash] { Model Class => {Wallaby::Mode} }
16
19
  def mode_map
17
- @mode_map ||= ModeMapper.new(modes).map.freeze
20
+ @mode_map ||= ModeMapper.execute(modes).freeze
18
21
  end
19
22
 
20
23
  # TODO: remove this method
21
24
  # @return [Array] [ model classes ]
22
25
  def model_classes
23
- @model_classes ||= ModelClassCollector.new(configuration, mode_map.keys).collect.freeze
26
+ ModelClassCollector.new(configuration, mode_map.keys).collect
24
27
  end
25
28
 
26
29
  # { model => resources name }
@@ -30,7 +33,7 @@ module Wallaby
30
33
  # @param value [String, nil] resources name
31
34
  # @return [String] resources name
32
35
  def resources_name_map(model_class, value = nil)
33
- @resources_name_map ||= {}
36
+ @resources_name_map ||= ClassHash.new
34
37
  @resources_name_map[model_class] ||= value || ModelUtils.to_resources_name(model_class)
35
38
  end
36
39
 
@@ -40,7 +43,7 @@ module Wallaby
40
43
  # @param resources_name [String]
41
44
  # @return [Class]
42
45
  def model_class_map(resources_name, value = nil)
43
- @model_class_map ||= {}
46
+ @model_class_map ||= ClassHash.new
44
47
  @model_class_map[resources_name] ||= value || ModelUtils.to_model_class(resources_name)
45
48
  end
46
49
  end
@@ -61,7 +64,7 @@ module Wallaby
61
64
  # @return [Class] resource decorator class, default to `mapping.resource_decorator`
62
65
  def resource_decorator_map(model_class, application_decorator = nil)
63
66
  application_decorator ||= mapping.resource_decorator
64
- map_of :@decorator_map, model_class, application_decorator
67
+ map_of :@resource_decorator_map, model_class, application_decorator
65
68
  end
66
69
 
67
70
  # { model => model decorator }
@@ -70,8 +73,8 @@ module Wallaby
70
73
  # @return [Wallaby::ModelDecorator] model decorator instance
71
74
  def model_decorator_map(model_class, application_decorator = nil)
72
75
  application_decorator ||= mapping.resource_decorator
73
- @model_decorator_map ||= {}
74
- @model_decorator_map[application_decorator] ||= {}
76
+ @model_decorator_map ||= ClassHash.new
77
+ @model_decorator_map[application_decorator] ||= ClassHash.new
75
78
  @model_decorator_map[application_decorator][model_class] ||=
76
79
  mode_map[model_class].try(:model_decorator).try :new, model_class
77
80
  end
@@ -109,7 +112,7 @@ module Wallaby
109
112
  # @param model_class [Class]
110
113
  # @return [Class] model service provider instance
111
114
  def service_provider_map(model_class)
112
- @service_provider_map ||= {}
115
+ @service_provider_map ||= ClassHash.new
113
116
  @service_provider_map[model_class] ||= mode_map[model_class].try :model_service_provider
114
117
  end
115
118
 
@@ -117,7 +120,7 @@ module Wallaby
117
120
  # @param model_class [Class]
118
121
  # @return [Class] model pagination provider class
119
122
  def pagination_provider_map(model_class)
120
- @pagination_provider_map ||= {}
123
+ @pagination_provider_map ||= ClassHash.new
121
124
  @pagination_provider_map[model_class] ||= mode_map[model_class].try :model_pagination_provider
122
125
  end
123
126
 
@@ -125,7 +128,7 @@ module Wallaby
125
128
  # @param model_class [Class]
126
129
  # @return [Class] model authorizer provider class
127
130
  def authorizer_provider_map(model_class)
128
- @authorizer_provider_map ||= {}
131
+ @authorizer_provider_map ||= ClassHash.new
129
132
  @authorizer_provider_map[model_class] ||= mode_map[model_class].try :model_authorization_providers
130
133
  end
131
134
  end
@@ -157,10 +160,10 @@ module Wallaby
157
160
  return unless model_class
158
161
 
159
162
  unless mode_map[model_class]
160
- Rails.logger.warn I18n.t('wallaby.map.missing_mode_for_model_class', model: model_class.name)
163
+ Logger.warn Locale.t('map.missing_mode_for_model_class', model: model_class.name), sourcing: 2..5
161
164
  return
162
165
  end
163
- instance_variable_set(variable_name, instance_variable_get(variable_name) || {})
166
+ instance_variable_set(variable_name, instance_variable_get(variable_name) || ClassHash.new)
164
167
  map = instance_variable_get variable_name
165
168
  map[application_class] ||= ModelClassMapper.map application_class.descendants
166
169
  map[application_class][model_class] ||= application_class
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wallaby
4
+ # Preload files for eager load paths.
5
+ #
6
+ # As Wallaby is built upon the {Wallaby::Map} which will not be completed
7
+ # until all models and decorators/controllers/servicers/authorizers/paginators
8
+ # are loaded. Therefore, when Rails app is initialized,
9
+ # all files under eager load paths (mostly `app/*` folders),
10
+ # especially the files under `app/models`, need to be loaded before everything else.
11
+ class Preloader
12
+ include ActiveModel::Model
13
+
14
+ # Require all files
15
+ # @see #all_file_paths
16
+ def self.require_all
17
+ new.all_file_paths.each(&method(:require_dependency))
18
+ end
19
+
20
+ # Require models under {Wallaby::Configuration#model_paths}
21
+ # @see #model_file_paths
22
+ def self.require_models
23
+ new.model_file_paths.each(&method(:require_dependency))
24
+ end
25
+
26
+ # @return [Array<String>] all files under **Rails.configuration.eager_load_paths**
27
+ def all_file_paths
28
+ sort all_eager_load_file_paths
29
+ end
30
+
31
+ # @return [Array<String>] model files under {Wallaby::Configuration#model_paths}
32
+ def model_file_paths
33
+ sort(all_eager_load_file_paths).select(&method(:indexed))
34
+ end
35
+
36
+ # @!attribute [w] eager_load_paths
37
+ attr_writer :eager_load_paths
38
+
39
+ # @!attribute [r] eager_load_paths
40
+ # @return [Array<String, Pathname>]
41
+ def eager_load_paths # :nodoc:
42
+ @eager_load_paths ||= Rails.configuration.eager_load_paths
43
+ end
44
+
45
+ # @!attribute [w] model_paths
46
+ attr_writer :model_paths
47
+
48
+ # @!attribute [r] model_paths
49
+ # @return [Array<String>]
50
+ def model_paths # :nodoc:
51
+ @model_paths ||= Wallaby.configuration.model_paths
52
+ end
53
+
54
+ private
55
+
56
+ def all_eager_load_file_paths # :nodoc:
57
+ Dir.glob(eager_load_paths.map { |load_path| "#{load_path}/**/*.rb" })
58
+ end
59
+
60
+ # All files need to be sorted in the following orders:
61
+ # 1. {Wallaby::Configuration#model_paths} order
62
+ # 2. Alphabet order
63
+ def sort(file_paths)
64
+ file_paths.sort { |p1, p2| conditions_for(p1) <=> conditions_for(p2) }
65
+ end
66
+
67
+ # @see #sort
68
+ def conditions_for(path)
69
+ [indexed(path) || model_paths.length, path]
70
+ end
71
+
72
+ # Check if the path is in the {Wallaby::Configuration#model_paths}
73
+ def indexed(path)
74
+ model_paths.index(&path.method(:include?))
75
+ end
76
+ end
77
+ end
metadata CHANGED
@@ -1,31 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wallaby-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tian Chen
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-17 00:00:00.000000000 Z
11
+ date: 2022-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: parslet
14
+ name: activemodel
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 4.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 4.2.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: rails
28
+ name: railties
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 4.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: parslet
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: responders
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +66,20 @@ dependencies:
52
66
  - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: wallaby-view
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.1.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.1.2
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: rspec-rails
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -89,9 +117,7 @@ extra_rdoc_files: []
89
117
  files:
90
118
  - LICENSE
91
119
  - README.md
92
- - app/controllers/wallaby/application_controller.rb
93
120
  - app/controllers/wallaby/resources_controller.rb
94
- - app/controllers/wallaby/secure_controller.rb
95
121
  - app/security/ability.rb
96
122
  - config/locales/wallaby.en.yml
97
123
  - config/locales/wallaby_class.en.yml
@@ -106,6 +132,8 @@ files:
106
132
  - lib/authorizers/wallaby/default_authorization_provider.rb
107
133
  - lib/authorizers/wallaby/model_authorizer.rb
108
134
  - lib/authorizers/wallaby/pundit_authorization_provider.rb
135
+ - lib/concerns/wallaby/application_concern.rb
136
+ - lib/concerns/wallaby/authentication_concern.rb
109
137
  - lib/concerns/wallaby/authorizable.rb
110
138
  - lib/concerns/wallaby/baseable.rb
111
139
  - lib/concerns/wallaby/decoratable.rb
@@ -113,13 +141,13 @@ files:
113
141
  - lib/concerns/wallaby/engineable.rb
114
142
  - lib/concerns/wallaby/fieldable.rb
115
143
  - lib/concerns/wallaby/paginatable.rb
116
- - lib/concerns/wallaby/rails_overridden_methods.rb
144
+ - lib/concerns/wallaby/prefixable.rb
117
145
  - lib/concerns/wallaby/resourcable.rb
146
+ - lib/concerns/wallaby/resources_concern.rb
118
147
  - lib/concerns/wallaby/servicable.rb
119
148
  - lib/concerns/wallaby/shared_helpers.rb
120
- - lib/concerns/wallaby/themeable.rb
121
149
  - lib/decorators/wallaby/resource_decorator.rb
122
- - lib/errors/wallaby/cell_handling.rb
150
+ - lib/errors/wallaby/class_not_found.rb
123
151
  - lib/errors/wallaby/forbidden.rb
124
152
  - lib/errors/wallaby/general_error.rb
125
153
  - lib/errors/wallaby/invalid_error.rb
@@ -146,13 +174,7 @@ files:
146
174
  - lib/interfaces/wallaby/model_pagination_provider.rb
147
175
  - lib/interfaces/wallaby/model_service_provider.rb
148
176
  - lib/paginators/wallaby/model_paginator.rb
149
- - lib/paginators/wallaby/resource_paginator.rb
150
177
  - lib/parsers/wallaby/parser.rb
151
- - lib/renderers/wallaby/cell.rb
152
- - lib/renderers/wallaby/cell_resolver.rb
153
- - lib/renderers/wallaby/custom_lookup_context.rb
154
- - lib/renderers/wallaby/custom_partial_renderer.rb
155
- - lib/renderers/wallaby/custom_renderer.rb
156
178
  - lib/responders/wallaby/json_api_responder.rb
157
179
  - lib/responders/wallaby/resources_responder.rb
158
180
  - lib/routes/wallaby/resources_router.rb
@@ -171,15 +193,17 @@ files:
171
193
  - lib/services/wallaby/type_renderer.rb
172
194
  - lib/support/action_dispatch/routing/mapper.rb
173
195
  - lib/tree/wallaby/node.rb
174
- - lib/utils/wallaby/cell_utils.rb
175
196
  - lib/utils/wallaby/field_utils.rb
176
197
  - lib/utils/wallaby/filter_utils.rb
198
+ - lib/utils/wallaby/locale.rb
177
199
  - lib/utils/wallaby/model_utils.rb
178
200
  - lib/utils/wallaby/module_utils.rb
179
201
  - lib/utils/wallaby/params_utils.rb
180
- - lib/utils/wallaby/preload_utils.rb
181
202
  - lib/utils/wallaby/test_utils.rb
182
203
  - lib/utils/wallaby/utils.rb
204
+ - lib/wallaby/class_array.rb
205
+ - lib/wallaby/class_hash.rb
206
+ - lib/wallaby/classifier.rb
183
207
  - lib/wallaby/configuration.rb
184
208
  - lib/wallaby/configuration/features.rb
185
209
  - lib/wallaby/configuration/mapping.rb
@@ -192,7 +216,9 @@ files:
192
216
  - lib/wallaby/core.rb
193
217
  - lib/wallaby/core/version.rb
194
218
  - lib/wallaby/engine.rb
219
+ - lib/wallaby/logger.rb
195
220
  - lib/wallaby/map.rb
221
+ - lib/wallaby/preloader.rb
196
222
  homepage: https://github.com/wallaby-rails/wallaby-core
197
223
  licenses:
198
224
  - MIT
@@ -200,7 +226,7 @@ metadata:
200
226
  homepage_uri: https://github.com/wallaby-rails/wallaby-core
201
227
  source_code_uri: https://github.com/wallaby-rails/wallaby-core
202
228
  changelog_uri: https://github.com/wallaby-rails/wallaby-core/blob/master/CHANGELOG.md
203
- post_install_message:
229
+ post_install_message:
204
230
  rdoc_options: []
205
231
  require_paths:
206
232
  - lib
@@ -216,7 +242,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
216
242
  version: '0'
217
243
  requirements: []
218
244
  rubygems_version: 3.0.3
219
- signing_key:
245
+ signing_key:
220
246
  specification_version: 4
221
247
  summary: The core of Wallaby
222
248
  test_files: []
@@ -1,84 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Wallaby
4
- # Like ordinary Rails application, it's the base controller that
5
- # other Wallaby controllers inherit from.
6
- #
7
- # However, the difference is that the controller class that {Wallaby::ApplicationController} inherits from
8
- # can be configured via {Wallaby::Configuration#base_controller}
9
- #
10
- # Here, it provides the most basic functions e.g. error handling for common 4xx HTTP status, helpers method,
11
- # and URL handling.
12
- class ApplicationController < configuration.base_controller
13
- extend Engineable::ClassMethods
14
- include Engineable
15
- include SharedHelpers
16
- helper ApplicationHelper
17
-
18
- rescue_from NotFound, with: :not_found
19
- rescue_from ::ActionController::ParameterMissing, with: :bad_request
20
- rescue_from ::ActiveRecord::StatementInvalid, with: :unprocessable_entity
21
- rescue_from NotImplemented, with: :not_implemented
22
- rescue_from UnprocessableEntity, with: :unprocessable_entity
23
-
24
- delegate(*ConfigurationHelper.instance_methods(false), :url_for, to: :helpers)
25
-
26
- # Health check page for e.g. NewRelic
27
- def healthy
28
- render plain: 'healthy'
29
- end
30
-
31
- # Not found page
32
- # @param exception [Exception] comes from **rescue_from**
33
- def not_found(exception = nil)
34
- error_rendering exception, __callee__
35
- end
36
-
37
- # Bad request page
38
- # @param exception [Exception] comes from **rescue_from**
39
- def bad_request(exception = nil)
40
- error_rendering exception, __callee__
41
- end
42
-
43
- # Unprocessable entity page
44
- # @param exception [Exception] comes from **rescue_from**
45
- def unprocessable_entity(exception = nil)
46
- error_rendering exception, __callee__
47
- end
48
-
49
- # Internal server error page
50
- # @param exception [Exception] comes from **rescue_from**
51
- def internal_server_error(exception = nil)
52
- error_rendering exception, __callee__
53
- end
54
-
55
- # Not implemented
56
- # @param exception [Exception] comes from **rescue_from**
57
- def not_implemented(exception = nil)
58
- error_rendering exception, __callee__
59
- end
60
-
61
- # {https://api.rubyonrails.org/classes/ActionController/Helpers.html#method-i-helpers helpers}
62
- # exists since Rails 5.0, need to mimic this to support Rails 4.2.
63
- # @see https://api.rubyonrails.org/classes/ActionController/Helpers.html#method-i-helpers
64
- # ActionController::Helpers#helpers
65
- # @see https://github.com/rails/rails/blob/5-0-stable/actionpack/lib/action_controller/metal/helpers.rb#L118
66
- def helpers
67
- @helpers ||= defined?(super) ? super : view_context
68
- end
69
-
70
- protected
71
-
72
- # Capture exceptions and display the error using error template.
73
- # @param exception [Exception]
74
- # @param symbol [Symbol] http status symbol
75
- def error_rendering(exception, symbol)
76
- Rails.logger.error exception
77
-
78
- @exception = exception
79
- @symbol = symbol
80
- @code = Rack::Utils::SYMBOL_TO_STATUS_CODE[symbol].to_i
81
- respond_with @exception, status: @code, template: ERROR_PATH, prefixes: _prefixes
82
- end
83
- end
84
- end
@@ -1,81 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Wallaby
4
- # This is the controller with only authentication logics.
5
- class SecureController < ::Wallaby::ApplicationController
6
- helper SecureHelper
7
- helper_method :current_user
8
-
9
- rescue_from NotAuthenticated, with: :unauthorized
10
- rescue_from Forbidden, with: :forbidden
11
-
12
- # Unauthorized page.
13
- # @param exception [Exception] exception comes from `rescue_from`
14
- def unauthorized(exception = nil)
15
- error_rendering exception, __callee__
16
- end
17
-
18
- # Forbidden page.
19
- # @param exception [Exception] exception comes from `rescue_from`
20
- def forbidden(exception = nil)
21
- error_rendering exception, __callee__
22
- end
23
-
24
- # @note This is a template method that can be overridden by subclasses
25
- # This {current_user} method will try to looking up the actual implementation from the following
26
- # places from high precedence to low:
27
- #
28
- # - {Wallaby::Configuration::Security#current_user}
29
- # - `super`
30
- # - do nothing
31
- #
32
- # It can be replaced completely in subclasses:
33
- #
34
- # ```
35
- # def current_user
36
- # # NOTE: please ensure `@current_user` is assigned, for instance:
37
- # @current_user ||= User.new params.slice(:email)
38
- # end
39
- # ```
40
- # @return [Object] a user object
41
- def current_user
42
- @current_user ||=
43
- if security.current_user? || !defined? super
44
- instance_exec(&security.current_user)
45
- else
46
- super
47
- end
48
- end
49
-
50
- # @note This is a template method that can be overridden by subclasses
51
- # This {authenticate_user!} method will try to looking up the actual implementation from the following
52
- # places from high precedence to low:
53
- #
54
- # - {Wallaby::Configuration::Security#authenticate}
55
- # - `super`
56
- # - do nothing
57
- #
58
- # It can be replaced completely in subclasses:
59
- #
60
- # ```
61
- # def authenticate_user!
62
- # authenticate_or_request_with_http_basic do |username, password|
63
- # username == 'too_simple' && password == 'too_naive'
64
- # end
65
- # end
66
- # ```
67
- # @return [true] when user is authenticated successfully
68
- # @raise [Wallaby::NotAuthenticated] when user fails to authenticate
69
- def authenticate_user!
70
- authenticated =
71
- if security.authenticate? || !defined? super
72
- instance_exec(&security.authenticate)
73
- else
74
- super
75
- end
76
- raise NotAuthenticated unless authenticated
77
-
78
- true
79
- end
80
- end
81
- end
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Wallaby
4
- # This is a collection of the helper methods that overrides the rails methods
5
- module RailsOverriddenMethods
6
- protected
7
-
8
- # Override {https://github.com/rails/rails/blob/master/actionview/lib/action_view/view_paths.rb
9
- # ActionView::ViewPaths::ClassMethods#_prefixes} to extend the prefixes for **ActionView::ViewPaths** to look up
10
- # in below precedence from high to low:
11
- #
12
- # - :mounted_path/:resources_name/:action_prefix (e.g. `admin/products/index`)
13
- # - :mounted_path/:resources_name (e.g. `admin/products`)
14
- # - :controller_path/:action_prefix
15
- # - :controller_path
16
- # - :parent_controller_path/:action_prefix
17
- # - :parent_controller_path
18
- # - :more_parent_controller_path/:action_prefix
19
- # - :more_parent_controller_path
20
- # - :theme_name/:action_prefix
21
- # - :theme_name
22
- # - wallaby/resources/:action_prefix
23
- # - wallaby/resources
24
- # @return [Array<String>]
25
- def _prefixes
26
- @_prefixes ||= PrefixesBuilder.build(
27
- origin_prefixes: super,
28
- theme_name: current_theme_name,
29
- resources_name: current_resources_name,
30
- script_name: request.env[SCRIPT_NAME],
31
- action_name: params[:action]
32
- )
33
- end
34
-
35
- # Override to provide support for cell lookup
36
- # @return [Wallaby::CustomLookupContext]
37
- def lookup_context
38
- @_lookup_context ||= # rubocop:disable Naming/MemoizedInstanceVariableName
39
- CustomLookupContext.normalize(super, prefixes: _prefixes)
40
- end
41
- end
42
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Wallaby
4
- # Theme related methods
5
- module Themeable
6
- # Configurable attributes
7
- module ClassMethods
8
- # @!attribute [w] theme_name
9
- def theme_name=(theme_name)
10
- layout theme_name
11
- @theme_name = theme_name
12
- end
13
-
14
- # @!attribute [r] theme_name
15
- # The theme name is used to apply a set of frontend (html/css/javascript) implementation.
16
- #
17
- # When theme name is set to e.g. `custom_theme`, the following changes will be made:
18
- #
19
- # - layout will be set to the same name `custom_theme`
20
- # - it will be added to the partial lookup prefixes right on top of `wallaby/resources` prefix.
21
- #
22
- # Once theme name is set, all its controller subclasses will inherit the same theme name
23
- # @example To set an theme name:
24
- # class Admin::ApplicationController < Wallaby::ResourcesController
25
- # self.theme_name = 'admin_theme'
26
- # end
27
- # @return [String, Symbol, nil] theme name
28
- # @since 5.2.0
29
- def theme_name
30
- @theme_name ||= ModuleUtils.try_to superclass, :theme_name
31
- end
32
- end
33
-
34
- # @return [String, Symbol, nil] theme name
35
- # @since 5.2.0
36
- def current_theme_name
37
- controller_to_get __callee__, :theme_name
38
- end
39
- end
40
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Wallaby
4
- # Resource paginator
5
- class ResourcePaginator < ModelPaginator
6
- base_class!
7
-
8
- def self.inherited(_sub_class)
9
- Utils.deprecate 'deprecation.resource_paginator_inheirtance', caller: caller
10
- end
11
- end
12
- end