wallaby-core 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/app/controllers/wallaby/resources_controller.rb +10 -1
- data/config/locales/wallaby_class.en.yml +2 -2
- 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 +29 -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 +1 -2
- data/lib/concerns/wallaby/authentication_concern.rb +74 -5
- 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 +0 -35
- data/lib/concerns/wallaby/resources_concern.rb +3 -2
- 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 +2 -0
- data/lib/helpers/wallaby/resources_helper.rb +3 -0
- data/lib/helpers/wallaby/secure_helper.rb +3 -3
- data/lib/interfaces/wallaby/mode.rb +3 -3
- 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/servicers/wallaby/model_servicer.rb +31 -62
- data/lib/services/wallaby/map/mode_mapper.rb +14 -14
- data/lib/services/wallaby/map/model_class_collector.rb +1 -1
- data/lib/services/wallaby/map/model_class_mapper.rb +7 -26
- data/lib/services/wallaby/type_renderer.rb +0 -10
- data/lib/utils/wallaby/model_utils.rb +4 -3
- data/lib/utils/wallaby/utils.rb +9 -8
- 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.rb +31 -2
- 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/core.rb +13 -7
- data/lib/wallaby/core/version.rb +1 -1
- 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 +8 -4
- data/lib/utils/wallaby/logger.rb +0 -21
- 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,6 +26,8 @@ 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'
|
@@ -28,11 +35,9 @@ require 'parsers/wallaby/parser'
|
|
28
35
|
require 'utils/wallaby/field_utils'
|
29
36
|
require 'utils/wallaby/filter_utils'
|
30
37
|
require 'utils/wallaby/locale'
|
31
|
-
require 'utils/wallaby/logger'
|
32
38
|
require 'utils/wallaby/model_utils'
|
33
39
|
require 'utils/wallaby/module_utils'
|
34
40
|
require 'utils/wallaby/params_utils'
|
35
|
-
require 'utils/wallaby/preload_utils'
|
36
41
|
require 'utils/wallaby/test_utils'
|
37
42
|
require 'utils/wallaby/utils'
|
38
43
|
|
@@ -51,7 +56,6 @@ require 'concerns/wallaby/prefixable'
|
|
51
56
|
require 'concerns/wallaby/resourcable'
|
52
57
|
require 'concerns/wallaby/servicable'
|
53
58
|
require 'concerns/wallaby/shared_helpers'
|
54
|
-
# require 'concerns/wallaby/themeable'
|
55
59
|
|
56
60
|
require 'interfaces/wallaby/mode'
|
57
61
|
require 'interfaces/wallaby/model_decorator'
|
@@ -61,6 +65,8 @@ require 'interfaces/wallaby/model_pagination_provider'
|
|
61
65
|
require 'interfaces/wallaby/model_authorization_provider'
|
62
66
|
|
63
67
|
require 'errors/wallaby/general_error'
|
68
|
+
|
69
|
+
require 'errors/wallaby/class_not_found'
|
64
70
|
require 'errors/wallaby/invalid_error'
|
65
71
|
require 'errors/wallaby/not_implemented'
|
66
72
|
require 'errors/wallaby/not_found'
|
@@ -101,9 +107,9 @@ require 'helpers/wallaby/links_helper'
|
|
101
107
|
require 'helpers/wallaby/styling_helper'
|
102
108
|
|
103
109
|
require 'helpers/wallaby/base_helper'
|
104
|
-
require 'helpers/wallaby/resources_helper'
|
105
|
-
require 'helpers/wallaby/secure_helper'
|
106
110
|
require 'helpers/wallaby/application_helper'
|
111
|
+
require 'helpers/wallaby/secure_helper'
|
112
|
+
require 'helpers/wallaby/resources_helper'
|
107
113
|
|
108
114
|
require 'responders/wallaby/resources_responder'
|
109
115
|
require 'responders/wallaby/json_api_responder'
|
data/lib/wallaby/core/version.rb
CHANGED
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
|
-
Logger.debug '
|
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
|
-
Logger.debug '
|
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
|
-
Logger.debug '
|
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
|
-
Logger.warn Locale.t('map.missing_mode_for_model_class', model: model_class.name), sourcing: 2
|
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tian Chen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-04-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -147,6 +147,7 @@ files:
|
|
147
147
|
- lib/concerns/wallaby/servicable.rb
|
148
148
|
- lib/concerns/wallaby/shared_helpers.rb
|
149
149
|
- lib/decorators/wallaby/resource_decorator.rb
|
150
|
+
- lib/errors/wallaby/class_not_found.rb
|
150
151
|
- lib/errors/wallaby/forbidden.rb
|
151
152
|
- lib/errors/wallaby/general_error.rb
|
152
153
|
- lib/errors/wallaby/invalid_error.rb
|
@@ -195,13 +196,14 @@ files:
|
|
195
196
|
- lib/utils/wallaby/field_utils.rb
|
196
197
|
- lib/utils/wallaby/filter_utils.rb
|
197
198
|
- lib/utils/wallaby/locale.rb
|
198
|
-
- lib/utils/wallaby/logger.rb
|
199
199
|
- lib/utils/wallaby/model_utils.rb
|
200
200
|
- lib/utils/wallaby/module_utils.rb
|
201
201
|
- lib/utils/wallaby/params_utils.rb
|
202
|
-
- lib/utils/wallaby/preload_utils.rb
|
203
202
|
- lib/utils/wallaby/test_utils.rb
|
204
203
|
- lib/utils/wallaby/utils.rb
|
204
|
+
- lib/wallaby/class_array.rb
|
205
|
+
- lib/wallaby/class_hash.rb
|
206
|
+
- lib/wallaby/classifier.rb
|
205
207
|
- lib/wallaby/configuration.rb
|
206
208
|
- lib/wallaby/configuration/features.rb
|
207
209
|
- lib/wallaby/configuration/mapping.rb
|
@@ -214,7 +216,9 @@ files:
|
|
214
216
|
- lib/wallaby/core.rb
|
215
217
|
- lib/wallaby/core/version.rb
|
216
218
|
- lib/wallaby/engine.rb
|
219
|
+
- lib/wallaby/logger.rb
|
217
220
|
- lib/wallaby/map.rb
|
221
|
+
- lib/wallaby/preloader.rb
|
218
222
|
homepage: https://github.com/wallaby-rails/wallaby-core
|
219
223
|
licenses:
|
220
224
|
- MIT
|
data/lib/utils/wallaby/logger.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Wallaby
|
4
|
-
# Custom logger
|
5
|
-
module Logger
|
6
|
-
class << self
|
7
|
-
%w(unknown fatal error warn info debug).each do |method_id|
|
8
|
-
define_method method_id do |message, replacements = {}|
|
9
|
-
message = message.inspect unless message.is_a? String
|
10
|
-
sourcing = replacements.delete :sourcing # sourcing can be set to false
|
11
|
-
|
12
|
-
from = "\nfrom #{caller[sourcing || 0]}" unless sourcing == false
|
13
|
-
Rails.logger.public_send(
|
14
|
-
method_id, "#{method_id.upcase}: #{format message, replacements}#{from}"
|
15
|
-
)
|
16
|
-
nil
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
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
|