ibrain-core 0.4.6 → 0.4.7
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/app/controllers/ibrain/base_controller.rb +14 -12
- data/app/controllers/ibrain/core/graphql_controller.rb +83 -79
- data/app/graphql/ibrain/base_schema.rb +52 -50
- data/app/graphql/ibrain/extentions/default_value.rb +11 -7
- data/app/graphql/ibrain/extentions/roles.rb +20 -15
- data/app/graphql/ibrain/extentions/session_required.rb +14 -10
- data/app/graphql/ibrain/lazy/base.rb +6 -2
- data/app/graphql/ibrain/loaders/association_loader.rb +55 -51
- data/app/graphql/ibrain/mutations/base_mutation.rb +56 -52
- data/app/graphql/ibrain/policies/base_policy.rb +47 -43
- data/app/graphql/ibrain/policies/graphql_policy.rb +6 -2
- data/app/graphql/ibrain/resolvers/base_aggregate.rb +10 -6
- data/app/graphql/ibrain/resolvers/base_resolver.rb +12 -8
- data/app/graphql/ibrain/types/aggregate_type.rb +8 -4
- data/app/graphql/ibrain/types/attribute_type.rb +5 -2
- data/app/graphql/ibrain/types/base_api_connection.rb +10 -6
- data/app/graphql/ibrain/types/base_api_edge.rb +8 -4
- data/app/graphql/ibrain/types/base_api_field.rb +11 -7
- data/app/graphql/ibrain/types/base_api_object.rb +10 -6
- data/app/graphql/ibrain/types/base_argument.rb +7 -3
- data/app/graphql/ibrain/types/base_connection.rb +11 -7
- data/app/graphql/ibrain/types/base_edge.rb +7 -3
- data/app/graphql/ibrain/types/base_enum.rb +5 -1
- data/app/graphql/ibrain/types/base_field.rb +10 -6
- data/app/graphql/ibrain/types/base_input_object.rb +10 -6
- data/app/graphql/ibrain/types/base_interface.rb +9 -5
- data/app/graphql/ibrain/types/base_object.rb +16 -12
- data/app/graphql/ibrain/types/base_scalar.rb +5 -1
- data/app/graphql/ibrain/types/base_type.rb +10 -4
- data/app/graphql/ibrain/types/base_union.rb +7 -3
- data/app/graphql/ibrain/types/filter_type.rb +5 -1
- data/app/graphql/ibrain/types/node_type.rb +8 -4
- data/app/graphql/ibrain/util/field_combiner.rb +9 -5
- data/app/graphql/ibrain/util/query_combiner.rb +8 -4
- data/app/models/concerns/ibrain/soft_deletable.rb +8 -6
- data/app/models/concerns/ibrain/user_api_authentication.rb +16 -14
- data/app/models/concerns/ibrain/user_methods.rb +13 -11
- data/app/models/ibrain/ability.rb +32 -31
- data/app/models/ibrain/aggregate.rb +7 -5
- data/app/models/ibrain/application_record.rb +4 -2
- data/app/models/ibrain/legacy_user.rb +13 -7
- data/app/models/ibrain/role.rb +8 -6
- data/app/models/ibrain/role_user.rb +10 -8
- data/app/repositories/ibrain/base_repository.rb +11 -9
- data/lib/generators/ibrain/core/model_generator.rb +21 -17
- data/lib/generators/ibrain/graphql/core.rb +59 -55
- data/lib/generators/ibrain/graphql/mutation_generator.rb +66 -58
- data/lib/generators/ibrain/graphql/object_generator.rb +70 -58
- data/lib/generators/ibrain/graphql/resolver_generator.rb +25 -17
- data/lib/generators/ibrain/graphql/resolvers_generator.rb +67 -59
- data/lib/generators/ibrain/graphql/templates/aggregate.erb +7 -5
- data/lib/generators/ibrain/graphql/templates/input.erb +9 -5
- data/lib/generators/ibrain/graphql/templates/mutation.erb +26 -24
- data/lib/generators/ibrain/graphql/templates/object.erb +10 -6
- data/lib/generators/ibrain/graphql/templates/resolver.erb +11 -9
- data/lib/generators/ibrain/graphql/templates/resolvers.erb +10 -8
- data/lib/generators/ibrain/graphql/type_generator.rb +88 -84
- data/lib/generators/ibrain/install/install_generator.rb +140 -137
- data/lib/generators/ibrain/install/templates/graphql/types/mutation_type.rb.tt +15 -13
- data/lib/generators/ibrain/install/templates/graphql/types/query_type.rb.tt +13 -11
- data/lib/generators/ibrain/install/templates/rubocop.yml.tt +30 -83
- data/lib/ibrain/app_configuration.rb +36 -34
- data/lib/ibrain/core/class_constantizer.rb +33 -30
- data/lib/ibrain/core/controller_helpers/auth.rb +59 -53
- data/lib/ibrain/core/controller_helpers/current_host.rb +11 -5
- data/lib/ibrain/core/controller_helpers/response.rb +49 -43
- data/lib/ibrain/core/controller_helpers/strong_parameters.rb +15 -9
- data/lib/ibrain/core/engine.rb +9 -5
- data/lib/ibrain/core/environment.rb +9 -5
- data/lib/ibrain/core/environment_extension.rb +17 -13
- data/lib/ibrain/core/role_configuration.rb +54 -52
- data/lib/ibrain/core/validators/email.rb +17 -15
- data/lib/ibrain/core/version.rb +2 -2
- data/lib/ibrain/core/versioned_value.rb +65 -61
- data/lib/ibrain/encryptor.rb +20 -18
- data/lib/ibrain/logger.rb +15 -13
- data/lib/ibrain/permission_sets/base.rb +26 -22
- data/lib/ibrain/permission_sets/super_user.rb +7 -3
- data/lib/ibrain/permitted_attributes.rb +20 -18
- data/lib/ibrain/preferences/configuration.rb +137 -135
- data/lib/ibrain/preferences/preferable.rb +162 -158
- data/lib/ibrain/preferences/preferable_class_methods.rb +114 -112
- data/lib/ibrain/user_class_handle.rb +24 -22
- data/lib/ibrain_core.rb +1 -7
- metadata +1 -1
|
@@ -3,136 +3,138 @@
|
|
|
3
3
|
require 'logger'
|
|
4
4
|
require 'ibrain/encryptor'
|
|
5
5
|
|
|
6
|
-
module Ibrain::Preferences
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
6
|
+
module Ibrain::Preferences
|
|
7
|
+
module PreferableClassMethods
|
|
8
|
+
DEFAULT_ADMIN_FORM_PREFERENCE_TYPES = %i(
|
|
9
|
+
boolean
|
|
10
|
+
decimal
|
|
11
|
+
integer
|
|
12
|
+
password
|
|
13
|
+
string
|
|
14
|
+
text
|
|
15
|
+
encrypted_string
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
def defined_preferences
|
|
19
|
+
[]
|
|
20
|
+
end
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
def preference(name, type, options = {})
|
|
23
|
+
options.assert_valid_keys(:default, :encryption_key)
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
if type == :encrypted_string
|
|
26
|
+
preference_encryptor = preference_encryptor(options)
|
|
27
|
+
options[:default] = preference_encryptor.encrypt(options[:default])
|
|
28
|
+
end
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
30
|
+
default = begin
|
|
31
|
+
given = options[:default]
|
|
32
|
+
if self <= Ibrain::Preferences::Configuration &&
|
|
33
|
+
given.is_a?(Proc) &&
|
|
34
|
+
given.lambda? &&
|
|
35
|
+
given.arity.zero?
|
|
36
|
+
Ibrain::Logger.warn <<~MSG
|
|
37
|
+
The arity of a proc given as the default for a preference
|
|
38
|
+
has changed from 0 to 1 on Ibrain 3.1. The Ibrain
|
|
39
|
+
version for the loaded preference defaults is given as the
|
|
40
|
+
proc's argument from this point on.
|
|
41
|
+
|
|
42
|
+
If you don't need to return a different default value
|
|
43
|
+
depending on the loaded Ibrain version, you can change
|
|
44
|
+
the proc so that it doesn't have lambda semantics (lambdas
|
|
45
|
+
raise when extra arguments are supplied, while raw procs
|
|
46
|
+
don't). E.g.:
|
|
47
|
+
|
|
48
|
+
preference :foo, :string, default: proc { true }
|
|
49
|
+
|
|
50
|
+
If you want to branch on the provided Ibrain version, you can do like the following:
|
|
51
|
+
|
|
52
|
+
preference :foo, :string, default: by_version(true, "3.2.0" => false)
|
|
53
|
+
|
|
54
|
+
MSG
|
|
55
|
+
->(_default_context) { given.call }
|
|
56
|
+
elsif given.is_a?(Proc)
|
|
57
|
+
given
|
|
58
|
+
else
|
|
59
|
+
proc { given }
|
|
60
|
+
end
|
|
61
|
+
end
|
|
61
62
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
63
|
+
# The defined preferences on a class are all those defined directly on
|
|
64
|
+
# that class as well as those defined on ancestors.
|
|
65
|
+
# We store these as a class instance variable on each class which has a
|
|
66
|
+
# preference. super() collects preferences defined on ancestors.
|
|
67
|
+
singleton_preferences = (@defined_singleton_preferences ||= [])
|
|
68
|
+
singleton_preferences << name.to_sym
|
|
68
69
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
define_singleton_method :defined_preferences do
|
|
71
|
+
super() + singleton_preferences
|
|
72
|
+
end
|
|
72
73
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
74
|
+
# cache_key will be nil for new objects, then if we check if there
|
|
75
|
+
# is a pending preference before going to default
|
|
76
|
+
define_method preference_getter_method(name) do
|
|
77
|
+
value = preferences.fetch(name) do
|
|
78
|
+
default.call(*context_for_default)
|
|
79
|
+
end
|
|
80
|
+
value = preference_encryptor.decrypt(value) if preference_encryptor.present?
|
|
81
|
+
value
|
|
78
82
|
end
|
|
79
|
-
value = preference_encryptor.decrypt(value) if preference_encryptor.present?
|
|
80
|
-
value
|
|
81
|
-
end
|
|
82
83
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
define_method preference_setter_method(name) do |value|
|
|
85
|
+
value = convert_preference_value(value, type, preference_encryptor)
|
|
86
|
+
preferences[name] = value
|
|
86
87
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
88
|
+
# If this is an activerecord object, we need to inform
|
|
89
|
+
# ActiveRecord::Dirty that this value has changed, since this is an
|
|
90
|
+
# in-place update to the preferences hash.
|
|
91
|
+
preferences_will_change! if respond_to?(:preferences_will_change!)
|
|
92
|
+
end
|
|
92
93
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
94
|
+
define_method preference_default_getter_method(name) do
|
|
95
|
+
default.call(*context_for_default)
|
|
96
|
+
end
|
|
96
97
|
|
|
97
|
-
|
|
98
|
-
|
|
98
|
+
define_method preference_type_getter_method(name) do
|
|
99
|
+
type
|
|
100
|
+
end
|
|
99
101
|
end
|
|
100
|
-
end
|
|
101
102
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
103
|
+
def preference_getter_method(name)
|
|
104
|
+
"preferred_#{name}".to_sym
|
|
105
|
+
end
|
|
105
106
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
def preference_setter_method(name)
|
|
108
|
+
"preferred_#{name}=".to_sym
|
|
109
|
+
end
|
|
109
110
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
111
|
+
def preference_default_getter_method(name)
|
|
112
|
+
"preferred_#{name}_default".to_sym
|
|
113
|
+
end
|
|
113
114
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
115
|
+
def preference_type_getter_method(name)
|
|
116
|
+
"preferred_#{name}_type".to_sym
|
|
117
|
+
end
|
|
117
118
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
119
|
+
def preference_encryptor(options)
|
|
120
|
+
key = options[:encryption_key] ||
|
|
121
|
+
ENV['IBRAIN_PREFERENCES_MASTER_KEY'] ||
|
|
122
|
+
Rails.application.credentials.secret_key_base
|
|
122
123
|
|
|
123
|
-
|
|
124
|
-
|
|
124
|
+
Ibrain::Encryptor.new(key)
|
|
125
|
+
end
|
|
125
126
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
127
|
+
# List of preference types allowed as form fields in the Ibrain admin
|
|
128
|
+
#
|
|
129
|
+
# Overwrite this method in your class that includes +Ibrain::Preferable+
|
|
130
|
+
# if you want to provide more fields. If you do so, you also need to provide
|
|
131
|
+
# a preference field partial that lives in:
|
|
132
|
+
#
|
|
133
|
+
# +app/views/Ibrain/admin/shared/preference_fields/+
|
|
134
|
+
#
|
|
135
|
+
# @return [Array]
|
|
136
|
+
def allowed_admin_form_preference_types
|
|
137
|
+
DEFAULT_ADMIN_FORM_PREFERENCE_TYPES
|
|
138
|
+
end
|
|
137
139
|
end
|
|
138
140
|
end
|
|
@@ -1,27 +1,29 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
#
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
3
|
+
module Ibrain
|
|
4
|
+
# Configuration point for User model implementation.
|
|
5
|
+
#
|
|
6
|
+
# `Ibrain::UserClassHandle` allows you to configure your own implementation of a
|
|
7
|
+
# User class or use an extension like `ibrain-auth`.
|
|
8
|
+
#
|
|
9
|
+
# @note Placeholder for name of Ibrain.user_class to ensure later evaluation at
|
|
10
|
+
# runtime.
|
|
11
|
+
#
|
|
12
|
+
# Unfortunately, it is possible for classes to get loaded before
|
|
13
|
+
# Ibrain.user_class has been set in the initializer. As a result, they end up
|
|
14
|
+
# with class_name: "" in their association definitions. For obvious reasons,
|
|
15
|
+
# that doesn't work.
|
|
16
|
+
#
|
|
17
|
+
# For now, Rails does not call to_s on the instance passed in until runtime.
|
|
18
|
+
# So this little hack provides a wrapper around Ibrain.user_class so that we
|
|
19
|
+
# can basically lazy-evaluate it. Yay! Problem solved forever.
|
|
20
|
+
class UserClassHandle
|
|
21
|
+
# @return [String] the name of the user class as a string.
|
|
22
|
+
# @raise [RuntimeError] if Ibrain.user_class is nil
|
|
23
|
+
def to_s
|
|
24
|
+
fail "'Ibrain.user_class' has not been set yet." unless Ibrain.user_class
|
|
24
25
|
|
|
25
|
-
|
|
26
|
+
"::#{Ibrain.user_class}"
|
|
27
|
+
end
|
|
26
28
|
end
|
|
27
29
|
end
|
data/lib/ibrain_core.rb
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
loader = Zeitwerk::Loader.new
|
|
5
|
-
loader.tag = File.basename(__FILE__, ".rb")
|
|
6
|
-
loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
|
|
7
|
-
loader.enable_reloading
|
|
8
|
-
loader.setup
|
|
9
|
-
loader.reload
|
|
3
|
+
require 'ibrain/core'
|