wallaby-core 0.2.0 → 0.2.4
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/README.md +42 -1
- data/app/controllers/wallaby/resources_controller.rb +15 -375
- data/app/security/ability.rb +1 -1
- data/config/locales/wallaby.en.yml +92 -128
- data/config/locales/wallaby_class.en.yml +2 -23
- data/lib/adaptors/wallaby/custom/default_provider.rb +1 -1
- data/lib/adaptors/wallaby/custom/model_decorator.rb +8 -7
- data/lib/adaptors/wallaby/custom/model_finder.rb +3 -2
- data/lib/adaptors/wallaby/custom/model_pagination_provider.rb +1 -1
- data/lib/adaptors/wallaby/custom/model_service_provider.rb +1 -40
- data/lib/authorizers/wallaby/cancancan_authorization_provider.rb +30 -24
- data/lib/authorizers/wallaby/default_authorization_provider.rb +6 -13
- data/lib/authorizers/wallaby/model_authorizer.rb +43 -67
- data/lib/authorizers/wallaby/pundit_authorization_provider.rb +21 -30
- data/lib/concerns/wallaby/application_concern.rb +110 -0
- data/lib/concerns/wallaby/authentication_concern.rb +162 -0
- data/lib/concerns/wallaby/authorizable.rb +8 -8
- data/lib/concerns/wallaby/baseable.rb +91 -10
- data/lib/concerns/wallaby/decoratable.rb +3 -3
- data/lib/concerns/wallaby/engineable.rb +1 -1
- data/lib/concerns/wallaby/fieldable.rb +4 -4
- data/lib/concerns/wallaby/paginatable.rb +3 -3
- data/lib/concerns/wallaby/resourcable.rb +4 -35
- data/lib/concerns/wallaby/resources_concern.rb +434 -0
- data/lib/concerns/wallaby/servicable.rb +4 -4
- data/lib/decorators/wallaby/resource_decorator.rb +53 -80
- data/lib/errors/wallaby/class_not_found.rb +6 -0
- data/lib/errors/wallaby/model_not_found.rb +3 -1
- data/lib/errors/wallaby/resource_not_found.rb +1 -1
- data/lib/helpers/wallaby/application_helper.rb +6 -0
- data/lib/helpers/wallaby/form_helper.rb +2 -3
- data/lib/helpers/wallaby/index_helper.rb +2 -2
- data/lib/helpers/wallaby/links_helper.rb +5 -5
- data/lib/helpers/wallaby/resources_helper.rb +3 -0
- data/lib/helpers/wallaby/secure_helper.rb +3 -3
- data/lib/helpers/wallaby/styling_helper.rb +17 -3
- data/lib/interfaces/wallaby/mode.rb +5 -5
- data/lib/interfaces/wallaby/model_authorization_provider.rb +15 -13
- data/lib/interfaces/wallaby/model_decorator.rb +15 -3
- data/lib/paginators/wallaby/model_paginator.rb +14 -45
- data/lib/routes/wallaby/resources_router.rb +1 -1
- data/lib/servicers/wallaby/model_servicer.rb +32 -62
- data/lib/services/wallaby/map/mode_mapper.rb +14 -14
- data/lib/services/wallaby/map/model_class_collector.rb +2 -2
- data/lib/services/wallaby/map/model_class_mapper.rb +7 -26
- data/lib/services/wallaby/type_renderer.rb +2 -12
- data/lib/utils/wallaby/locale.rb +53 -0
- data/lib/utils/wallaby/model_utils.rb +4 -3
- data/lib/utils/wallaby/module_utils.rb +1 -1
- data/lib/utils/wallaby/utils.rb +23 -9
- data/lib/wallaby/class_array.rb +75 -0
- data/lib/wallaby/class_hash.rb +94 -0
- data/lib/wallaby/classifier.rb +29 -0
- data/lib/wallaby/configuration/mapping.rb +33 -21
- data/lib/wallaby/configuration/metadata.rb +1 -1
- data/lib/wallaby/configuration/models.rb +5 -9
- data/lib/wallaby/configuration/security.rb +6 -3
- data/lib/wallaby/configuration/sorting.rb +1 -1
- data/lib/wallaby/configuration.rb +31 -2
- data/lib/wallaby/core/version.rb +1 -1
- data/lib/wallaby/core.rb +18 -6
- data/lib/wallaby/engine.rb +9 -20
- data/lib/wallaby/logger.rb +35 -0
- data/lib/wallaby/map.rb +20 -17
- data/lib/wallaby/preloader.rb +77 -0
- metadata +16 -9
- data/app/controllers/wallaby/application_controller.rb +0 -84
- data/app/controllers/wallaby/secure_controller.rb +0 -81
- data/lib/utils/wallaby/preload_utils.rb +0 -44
data/lib/wallaby/core.rb
CHANGED
@@ -7,10 +7,15 @@ require 'wallaby/view'
|
|
7
7
|
|
8
8
|
require 'wallaby/core/version'
|
9
9
|
require 'wallaby/constants'
|
10
|
+
require 'wallaby/logger'
|
11
|
+
require 'wallaby/preloader'
|
10
12
|
require 'wallaby/engine'
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
+
# Global class attributes that holds the most important configuration/information
|
15
|
+
# NOTE: DO NOT store any Class in both configuration and map
|
16
|
+
require 'wallaby/classifier'
|
17
|
+
require 'wallaby/class_array'
|
18
|
+
require 'wallaby/class_hash'
|
14
19
|
require 'wallaby/configuration'
|
15
20
|
require 'wallaby/configuration/features'
|
16
21
|
require 'wallaby/configuration/mapping'
|
@@ -21,19 +26,25 @@ require 'wallaby/configuration/security'
|
|
21
26
|
require 'wallaby/configuration/sorting'
|
22
27
|
require 'wallaby/map'
|
23
28
|
|
29
|
+
require 'support/action_dispatch/routing/mapper'
|
30
|
+
|
24
31
|
require 'routes/wallaby/resources_router'
|
25
32
|
require 'tree/wallaby/node'
|
26
33
|
require 'parsers/wallaby/parser'
|
27
34
|
|
28
35
|
require 'utils/wallaby/field_utils'
|
29
36
|
require 'utils/wallaby/filter_utils'
|
37
|
+
require 'utils/wallaby/locale'
|
30
38
|
require 'utils/wallaby/model_utils'
|
31
39
|
require 'utils/wallaby/module_utils'
|
32
40
|
require 'utils/wallaby/params_utils'
|
33
|
-
require 'utils/wallaby/preload_utils'
|
34
41
|
require 'utils/wallaby/test_utils'
|
35
42
|
require 'utils/wallaby/utils'
|
36
43
|
|
44
|
+
require 'concerns/wallaby/application_concern'
|
45
|
+
require 'concerns/wallaby/authentication_concern'
|
46
|
+
require 'concerns/wallaby/resources_concern'
|
47
|
+
|
37
48
|
require 'concerns/wallaby/authorizable'
|
38
49
|
require 'concerns/wallaby/baseable'
|
39
50
|
require 'concerns/wallaby/decoratable'
|
@@ -45,7 +56,6 @@ require 'concerns/wallaby/prefixable'
|
|
45
56
|
require 'concerns/wallaby/resourcable'
|
46
57
|
require 'concerns/wallaby/servicable'
|
47
58
|
require 'concerns/wallaby/shared_helpers'
|
48
|
-
# require 'concerns/wallaby/themeable'
|
49
59
|
|
50
60
|
require 'interfaces/wallaby/mode'
|
51
61
|
require 'interfaces/wallaby/model_decorator'
|
@@ -55,6 +65,8 @@ require 'interfaces/wallaby/model_pagination_provider'
|
|
55
65
|
require 'interfaces/wallaby/model_authorization_provider'
|
56
66
|
|
57
67
|
require 'errors/wallaby/general_error'
|
68
|
+
|
69
|
+
require 'errors/wallaby/class_not_found'
|
58
70
|
require 'errors/wallaby/invalid_error'
|
59
71
|
require 'errors/wallaby/not_implemented'
|
60
72
|
require 'errors/wallaby/not_found'
|
@@ -95,9 +107,9 @@ require 'helpers/wallaby/links_helper'
|
|
95
107
|
require 'helpers/wallaby/styling_helper'
|
96
108
|
|
97
109
|
require 'helpers/wallaby/base_helper'
|
98
|
-
require 'helpers/wallaby/resources_helper'
|
99
|
-
require 'helpers/wallaby/secure_helper'
|
100
110
|
require 'helpers/wallaby/application_helper'
|
111
|
+
require 'helpers/wallaby/secure_helper'
|
112
|
+
require 'helpers/wallaby/resources_helper'
|
101
113
|
|
102
114
|
require 'responders/wallaby/resources_responder'
|
103
115
|
require 'responders/wallaby/json_api_responder'
|
data/lib/wallaby/engine.rb
CHANGED
@@ -3,40 +3,29 @@
|
|
3
3
|
module Wallaby
|
4
4
|
# Wallaby engine
|
5
5
|
class Engine < ::Rails::Engine
|
6
|
-
initializer 'wallaby.autoload_paths', before: :set_load_path do |_|
|
7
|
-
# NOTE: this needs to be run before `set_load_path`
|
8
|
-
# so that files under `app/views` can be eager loaded
|
9
|
-
# and therefore, Wallaby's renderer can function properly
|
10
|
-
[Rails.configuration].each do |conf|
|
11
|
-
next if conf.paths['app/views'].eager_load?
|
12
|
-
|
13
|
-
conf.paths.add 'app/views', eager_load: true
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
6
|
initializer 'wallaby.development.reload' do |_|
|
18
7
|
# NOTE: Rails reload! will hit here
|
19
8
|
# @see http://rmosolgo.github.io/blog/2017/04/12/watching-files-during-rails-development/
|
20
9
|
config.to_prepare do
|
21
10
|
if Rails.env.development? || Rails.configuration.eager_load
|
22
|
-
|
23
|
-
|
24
|
-
|
11
|
+
Logger.debug 'Reloading after Rails\' reload...', sourcing: false
|
12
|
+
Map.clear
|
13
|
+
Preloader.require_all
|
25
14
|
end
|
26
15
|
end
|
27
16
|
end
|
28
17
|
|
29
18
|
config.before_eager_load do
|
30
|
-
# NOTE:
|
31
|
-
|
32
|
-
|
19
|
+
# NOTE: The models must be loaded before everything else
|
20
|
+
Logger.debug 'Preload all `app/models` files.', sourcing: false
|
21
|
+
Preloader.require_models
|
33
22
|
end
|
34
23
|
|
35
|
-
# Preload the rest files
|
36
24
|
config.after_initialize do
|
25
|
+
# Load the rest files
|
37
26
|
unless Rails.env.development? || Rails.configuration.eager_load
|
38
|
-
|
39
|
-
|
27
|
+
Logger.debug 'Preload all other eager load files after initialize.', sourcing: false
|
28
|
+
Preloader.require_all
|
40
29
|
end
|
41
30
|
end
|
42
31
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Wallaby
|
4
|
+
# Custom logger
|
5
|
+
module Logger
|
6
|
+
class << self
|
7
|
+
%i(unknown fatal error warn info debug deprecated).each do |method_id|
|
8
|
+
define_method method_id do |message, replacements = {}|
|
9
|
+
sourcing = replacements.delete :sourcing # sourcing can be set to false
|
10
|
+
heading = replacements.delete(:heading) || 'WALLABY '
|
11
|
+
new_message, from = normalize message, sourcing != false && Array(caller[sourcing || 0]) || nil
|
12
|
+
Rails.logger.public_send(
|
13
|
+
method_id == :deprecated ? :warn : method_id,
|
14
|
+
"#{heading}#{method_id.to_s.upcase}: #{format new_message, replacements}#{from}"
|
15
|
+
)
|
16
|
+
nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
# @param message [Object]
|
23
|
+
def normalize(message, sources)
|
24
|
+
case message
|
25
|
+
when String
|
26
|
+
[message, sources && "\nfrom #{sources.join(" \n")}"]
|
27
|
+
when StandardError
|
28
|
+
[message.message, sources && "\n#{message.backtrace.join("\n")}"]
|
29
|
+
else
|
30
|
+
[message.inspect, sources && "\nfrom #{sources.join(" \n")}"]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/wallaby/map.rb
CHANGED
@@ -1,26 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Wallaby
|
4
|
-
#
|
4
|
+
# All the lookups that Wallaby needs.
|
5
5
|
class Map
|
6
6
|
class << self
|
7
|
-
|
7
|
+
include Classifier
|
8
|
+
|
9
|
+
# @!attribute [w] modes
|
8
10
|
attr_writer :modes
|
9
11
|
|
10
|
-
#
|
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 [
|
18
|
+
# @return [Wallaby::ClassHash] { Model Class => {Wallaby::Mode} }
|
16
19
|
def mode_map
|
17
|
-
@mode_map ||= ModeMapper.
|
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
|
-
|
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 :@
|
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
|
-
|
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,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wallaby-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
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:
|
11
|
+
date: 2022-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -117,9 +117,7 @@ extra_rdoc_files: []
|
|
117
117
|
files:
|
118
118
|
- LICENSE
|
119
119
|
- README.md
|
120
|
-
- app/controllers/wallaby/application_controller.rb
|
121
120
|
- app/controllers/wallaby/resources_controller.rb
|
122
|
-
- app/controllers/wallaby/secure_controller.rb
|
123
121
|
- app/security/ability.rb
|
124
122
|
- config/locales/wallaby.en.yml
|
125
123
|
- config/locales/wallaby_class.en.yml
|
@@ -134,6 +132,8 @@ files:
|
|
134
132
|
- lib/authorizers/wallaby/default_authorization_provider.rb
|
135
133
|
- lib/authorizers/wallaby/model_authorizer.rb
|
136
134
|
- lib/authorizers/wallaby/pundit_authorization_provider.rb
|
135
|
+
- lib/concerns/wallaby/application_concern.rb
|
136
|
+
- lib/concerns/wallaby/authentication_concern.rb
|
137
137
|
- lib/concerns/wallaby/authorizable.rb
|
138
138
|
- lib/concerns/wallaby/baseable.rb
|
139
139
|
- lib/concerns/wallaby/decoratable.rb
|
@@ -143,9 +143,11 @@ files:
|
|
143
143
|
- lib/concerns/wallaby/paginatable.rb
|
144
144
|
- lib/concerns/wallaby/prefixable.rb
|
145
145
|
- lib/concerns/wallaby/resourcable.rb
|
146
|
+
- lib/concerns/wallaby/resources_concern.rb
|
146
147
|
- lib/concerns/wallaby/servicable.rb
|
147
148
|
- lib/concerns/wallaby/shared_helpers.rb
|
148
149
|
- lib/decorators/wallaby/resource_decorator.rb
|
150
|
+
- lib/errors/wallaby/class_not_found.rb
|
149
151
|
- lib/errors/wallaby/forbidden.rb
|
150
152
|
- lib/errors/wallaby/general_error.rb
|
151
153
|
- lib/errors/wallaby/invalid_error.rb
|
@@ -193,12 +195,15 @@ files:
|
|
193
195
|
- lib/tree/wallaby/node.rb
|
194
196
|
- lib/utils/wallaby/field_utils.rb
|
195
197
|
- lib/utils/wallaby/filter_utils.rb
|
198
|
+
- lib/utils/wallaby/locale.rb
|
196
199
|
- lib/utils/wallaby/model_utils.rb
|
197
200
|
- lib/utils/wallaby/module_utils.rb
|
198
201
|
- lib/utils/wallaby/params_utils.rb
|
199
|
-
- lib/utils/wallaby/preload_utils.rb
|
200
202
|
- lib/utils/wallaby/test_utils.rb
|
201
203
|
- lib/utils/wallaby/utils.rb
|
204
|
+
- lib/wallaby/class_array.rb
|
205
|
+
- lib/wallaby/class_hash.rb
|
206
|
+
- lib/wallaby/classifier.rb
|
202
207
|
- lib/wallaby/configuration.rb
|
203
208
|
- lib/wallaby/configuration/features.rb
|
204
209
|
- lib/wallaby/configuration/mapping.rb
|
@@ -211,7 +216,9 @@ files:
|
|
211
216
|
- lib/wallaby/core.rb
|
212
217
|
- lib/wallaby/core/version.rb
|
213
218
|
- lib/wallaby/engine.rb
|
219
|
+
- lib/wallaby/logger.rb
|
214
220
|
- lib/wallaby/map.rb
|
221
|
+
- lib/wallaby/preloader.rb
|
215
222
|
homepage: https://github.com/wallaby-rails/wallaby-core
|
216
223
|
licenses:
|
217
224
|
- MIT
|
@@ -219,7 +226,7 @@ metadata:
|
|
219
226
|
homepage_uri: https://github.com/wallaby-rails/wallaby-core
|
220
227
|
source_code_uri: https://github.com/wallaby-rails/wallaby-core
|
221
228
|
changelog_uri: https://github.com/wallaby-rails/wallaby-core/blob/master/CHANGELOG.md
|
222
|
-
post_install_message:
|
229
|
+
post_install_message:
|
223
230
|
rdoc_options: []
|
224
231
|
require_paths:
|
225
232
|
- lib
|
@@ -234,8 +241,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
234
241
|
- !ruby/object:Gem::Version
|
235
242
|
version: '0'
|
236
243
|
requirements: []
|
237
|
-
rubygems_version: 3.
|
238
|
-
signing_key:
|
244
|
+
rubygems_version: 3.1.6
|
245
|
+
signing_key:
|
239
246
|
specification_version: 4
|
240
247
|
summary: The core of Wallaby
|
241
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
|
-
render_error exception, __callee__
|
35
|
-
end
|
36
|
-
|
37
|
-
# Bad request page
|
38
|
-
# @param exception [Exception] comes from **rescue_from**
|
39
|
-
def bad_request(exception = nil)
|
40
|
-
render_error exception, __callee__
|
41
|
-
end
|
42
|
-
|
43
|
-
# Unprocessable entity page
|
44
|
-
# @param exception [Exception] comes from **rescue_from**
|
45
|
-
def unprocessable_entity(exception = nil)
|
46
|
-
render_error 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
|
-
render_error exception, __callee__
|
53
|
-
end
|
54
|
-
|
55
|
-
# Not implemented
|
56
|
-
# @param exception [Exception] comes from **rescue_from**
|
57
|
-
def not_implemented(exception = nil)
|
58
|
-
render_error 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 render_error(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
|
-
render_error exception, __callee__
|
16
|
-
end
|
17
|
-
|
18
|
-
# Forbidden page.
|
19
|
-
# @param exception [Exception] exception comes from `rescue_from`
|
20
|
-
def forbidden(exception = nil)
|
21
|
-
render_error 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,44 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Wallaby
|
4
|
-
# Preload utils
|
5
|
-
module PreloadUtils
|
6
|
-
class << self
|
7
|
-
# Preload all files under app folder
|
8
|
-
def require_all
|
9
|
-
eager_load_paths.map(&method(:require_one))
|
10
|
-
end
|
11
|
-
|
12
|
-
# Require files under a load path
|
13
|
-
# @param load_path [String, Pathname]
|
14
|
-
# @see https://api.rubyonrails.org/classes/Rails/Engine.html#method-i-eager_load-21 Rails::Engine#eager_load!
|
15
|
-
def require_one(load_path)
|
16
|
-
Dir.glob("#{load_path}/**/*.rb").sort.each(&method(:load_class_for))
|
17
|
-
end
|
18
|
-
|
19
|
-
protected
|
20
|
-
|
21
|
-
# @return [Array<String, Pathname>] a list of sorted eager load paths which lists `app/models`
|
22
|
-
# at highest precedence
|
23
|
-
def eager_load_paths
|
24
|
-
Rails.configuration.eager_load_paths.sort_by do |path|
|
25
|
-
- path.to_s.index(%r{/models$}).to_i
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
# `constantize` is used to make Rails to handle all sort of load errors
|
30
|
-
#
|
31
|
-
# NOTE: don't try to use `ActiveSupport::Dependencies::Loadable.require_dependency`.
|
32
|
-
# As `require_dependency` does not take care all errors raised when class/module is loaded.
|
33
|
-
# @param file_path [Pathname, String]
|
34
|
-
def load_class_for(file_path)
|
35
|
-
module_name = file_path[%r{app/[^/]+/(.+)\.rb}, 1].gsub('/concerns/', '/')
|
36
|
-
class_name = module_name.camelize
|
37
|
-
class_name.constantize unless Module.const_defined? class_name
|
38
|
-
rescue NameError, LoadError => e
|
39
|
-
Rails.logger.debug " [WALLABY] Preload warning: #{e.message} from #{file_path}"
|
40
|
-
Rails.logger.debug e.backtrace.slice(0, 5)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|