ibrain-core 0.4.5 → 0.4.6

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