ibrain-core 0.4.6 → 0.4.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/ibrain/base_controller.rb +14 -12
  3. data/app/controllers/ibrain/core/graphql_controller.rb +83 -79
  4. data/app/graphql/ibrain/base_schema.rb +52 -50
  5. data/app/graphql/ibrain/extentions/default_value.rb +11 -7
  6. data/app/graphql/ibrain/extentions/roles.rb +20 -15
  7. data/app/graphql/ibrain/extentions/session_required.rb +14 -10
  8. data/app/graphql/ibrain/lazy/base.rb +6 -2
  9. data/app/graphql/ibrain/loaders/association_loader.rb +55 -51
  10. data/app/graphql/ibrain/mutations/base_mutation.rb +56 -52
  11. data/app/graphql/ibrain/policies/base_policy.rb +47 -43
  12. data/app/graphql/ibrain/policies/graphql_policy.rb +6 -2
  13. data/app/graphql/ibrain/resolvers/base_aggregate.rb +10 -6
  14. data/app/graphql/ibrain/resolvers/base_resolver.rb +12 -8
  15. data/app/graphql/ibrain/types/aggregate_type.rb +8 -4
  16. data/app/graphql/ibrain/types/attribute_type.rb +5 -2
  17. data/app/graphql/ibrain/types/base_api_connection.rb +10 -6
  18. data/app/graphql/ibrain/types/base_api_edge.rb +8 -4
  19. data/app/graphql/ibrain/types/base_api_field.rb +11 -7
  20. data/app/graphql/ibrain/types/base_api_object.rb +10 -6
  21. data/app/graphql/ibrain/types/base_argument.rb +7 -3
  22. data/app/graphql/ibrain/types/base_connection.rb +11 -7
  23. data/app/graphql/ibrain/types/base_edge.rb +7 -3
  24. data/app/graphql/ibrain/types/base_enum.rb +5 -1
  25. data/app/graphql/ibrain/types/base_field.rb +10 -6
  26. data/app/graphql/ibrain/types/base_input_object.rb +10 -6
  27. data/app/graphql/ibrain/types/base_interface.rb +9 -5
  28. data/app/graphql/ibrain/types/base_object.rb +16 -12
  29. data/app/graphql/ibrain/types/base_scalar.rb +5 -1
  30. data/app/graphql/ibrain/types/base_type.rb +10 -4
  31. data/app/graphql/ibrain/types/base_union.rb +7 -3
  32. data/app/graphql/ibrain/types/filter_type.rb +5 -1
  33. data/app/graphql/ibrain/types/node_type.rb +8 -4
  34. data/app/graphql/ibrain/util/field_combiner.rb +9 -5
  35. data/app/graphql/ibrain/util/query_combiner.rb +8 -4
  36. data/app/models/concerns/ibrain/soft_deletable.rb +8 -6
  37. data/app/models/concerns/ibrain/user_api_authentication.rb +16 -14
  38. data/app/models/concerns/ibrain/user_methods.rb +13 -11
  39. data/app/models/ibrain/ability.rb +32 -31
  40. data/app/models/ibrain/aggregate.rb +7 -5
  41. data/app/models/ibrain/application_record.rb +4 -2
  42. data/app/models/ibrain/legacy_user.rb +13 -7
  43. data/app/models/ibrain/role.rb +8 -6
  44. data/app/models/ibrain/role_user.rb +10 -8
  45. data/app/repositories/ibrain/base_repository.rb +11 -9
  46. data/lib/generators/ibrain/core/model_generator.rb +21 -17
  47. data/lib/generators/ibrain/graphql/core.rb +59 -55
  48. data/lib/generators/ibrain/graphql/mutation_generator.rb +66 -58
  49. data/lib/generators/ibrain/graphql/object_generator.rb +70 -58
  50. data/lib/generators/ibrain/graphql/resolver_generator.rb +25 -17
  51. data/lib/generators/ibrain/graphql/resolvers_generator.rb +67 -59
  52. data/lib/generators/ibrain/graphql/templates/aggregate.erb +7 -5
  53. data/lib/generators/ibrain/graphql/templates/input.erb +9 -5
  54. data/lib/generators/ibrain/graphql/templates/mutation.erb +26 -24
  55. data/lib/generators/ibrain/graphql/templates/object.erb +10 -6
  56. data/lib/generators/ibrain/graphql/templates/resolver.erb +11 -9
  57. data/lib/generators/ibrain/graphql/templates/resolvers.erb +10 -8
  58. data/lib/generators/ibrain/graphql/type_generator.rb +88 -84
  59. data/lib/generators/ibrain/install/install_generator.rb +140 -137
  60. data/lib/generators/ibrain/install/templates/graphql/types/mutation_type.rb.tt +15 -13
  61. data/lib/generators/ibrain/install/templates/graphql/types/query_type.rb.tt +13 -11
  62. data/lib/generators/ibrain/install/templates/rubocop.yml.tt +30 -83
  63. data/lib/ibrain/app_configuration.rb +36 -34
  64. data/lib/ibrain/core/class_constantizer.rb +33 -30
  65. data/lib/ibrain/core/controller_helpers/auth.rb +59 -53
  66. data/lib/ibrain/core/controller_helpers/current_host.rb +11 -5
  67. data/lib/ibrain/core/controller_helpers/response.rb +49 -43
  68. data/lib/ibrain/core/controller_helpers/strong_parameters.rb +15 -9
  69. data/lib/ibrain/core/engine.rb +9 -5
  70. data/lib/ibrain/core/environment.rb +9 -5
  71. data/lib/ibrain/core/environment_extension.rb +17 -13
  72. data/lib/ibrain/core/role_configuration.rb +54 -52
  73. data/lib/ibrain/core/validators/email.rb +17 -15
  74. data/lib/ibrain/core/version.rb +2 -2
  75. data/lib/ibrain/core/versioned_value.rb +65 -61
  76. data/lib/ibrain/encryptor.rb +20 -18
  77. data/lib/ibrain/logger.rb +15 -13
  78. data/lib/ibrain/permission_sets/base.rb +26 -22
  79. data/lib/ibrain/permission_sets/super_user.rb +7 -3
  80. data/lib/ibrain/permitted_attributes.rb +20 -18
  81. data/lib/ibrain/preferences/configuration.rb +137 -135
  82. data/lib/ibrain/preferences/preferable.rb +162 -158
  83. data/lib/ibrain/preferences/preferable_class_methods.rb +114 -112
  84. data/lib/ibrain/user_class_handle.rb +24 -22
  85. data/lib/ibrain_core.rb +1 -7
  86. metadata +1 -1
@@ -3,166 +3,168 @@
3
3
  require 'ibrain/core/versioned_value'
4
4
  require 'ibrain/preferences/preferable'
5
5
 
6
- # This takes the preferrable methods and adds some
7
- # syntatic sugar to access the preferences
8
- #
9
- # class App < Configuration
10
- # preference :color, :string
11
- # end
12
- #
13
- # a = App.new
14
- #
15
- # Provides the following setters:
16
- #
17
- # a.color = :blue
18
- # a[:color] = :blue
19
- # a.set color: :blue
20
- # a.preferred_color = :blue
21
- #
22
- # and the following getters:
23
- #
24
- # a.color
25
- # a[:color]
26
- # a.get :color
27
- # a.preferred_color
28
- #
29
- class Ibrain::Preferences::Configuration
30
- include Ibrain::Preferences::Preferable
31
-
32
- # @!attribute [r] loaded_defaults
33
- # @return [String]
34
- # Some configuration defaults can be added or changed when a new Ibrain
35
- # version is released. Setting this to an older Ibrain version allows keeping
36
- # backward compatibility until the application code is updated to the new
37
- # defaults. Set via {#load_defaults}
38
- attr_reader :loaded_defaults
39
-
40
- attr_reader :load_defaults_called
41
-
42
- def initialize
43
- @loaded_defaults = Ibrain.ibrain_version
44
- @load_defaults_called = false
45
- end
6
+ module Ibrain::Preferences
7
+ # This takes the preferrable methods and adds some
8
+ # syntatic sugar to access the preferences
9
+ #
10
+ # class App < Configuration
11
+ # preference :color, :string
12
+ # end
13
+ #
14
+ # a = App.new
15
+ #
16
+ # Provides the following setters:
17
+ #
18
+ # a.color = :blue
19
+ # a[:color] = :blue
20
+ # a.set color: :blue
21
+ # a.preferred_color = :blue
22
+ #
23
+ # and the following getters:
24
+ #
25
+ # a.color
26
+ # a[:color]
27
+ # a.get :color
28
+ # a.preferred_color
29
+ #
30
+ class Configuration
31
+ include Ibrain::Preferences::Preferable
32
+
33
+ # @!attribute [r] loaded_defaults
34
+ # @return [String]
35
+ # Some configuration defaults can be added or changed when a new Ibrain
36
+ # version is released. Setting this to an older Ibrain version allows keeping
37
+ # backward compatibility until the application code is updated to the new
38
+ # defaults. Set via {#load_defaults}
39
+ attr_reader :loaded_defaults
40
+
41
+ attr_reader :load_defaults_called
42
+
43
+ def initialize
44
+ @loaded_defaults = Ibrain.ibrain_version
45
+ @load_defaults_called = false
46
+ end
46
47
 
47
- # @param [String] Ibrain version from which take defaults when not
48
- # overriden.
49
- # @see #load_defaults
50
- def load_defaults(version)
51
- @loaded_defaults = version
52
- @load_defaults_called = true
53
- reset
54
- end
48
+ # @param [String] Ibrain version from which take defaults when not
49
+ # overriden.
50
+ # @see #load_defaults
51
+ def load_defaults(version)
52
+ @loaded_defaults = version
53
+ @load_defaults_called = true
54
+ reset
55
+ end
55
56
 
56
- def check_load_defaults_called(instance_constant_name = nil)
57
- return if load_defaults_called || !Ibrain::Core.has_install_generator_been_run?
57
+ def check_load_defaults_called(instance_constant_name = nil)
58
+ return if load_defaults_called || !Ibrain::Core.has_install_generator_been_run?
58
59
 
59
- target_name = instance_constant_name || "#{self.class.name}.new"
60
- Ibrain::Deprecation.warn <<~MSG
61
- It's recommended that you explicitly load the default configuration for
62
- your current Ibrain version. You can do it by adding the following call
63
- to your Ibrain initializer within the #{target_name} block:
60
+ target_name = instance_constant_name || "#{self.class.name}.new"
61
+ Ibrain::Deprecation.warn <<~MSG
62
+ It's recommended that you explicitly load the default configuration for
63
+ your current Ibrain version. You can do it by adding the following call
64
+ to your Ibrain initializer within the #{target_name} block:
64
65
 
65
- config.load_defaults('#{Ibrain.ibrain_version}')
66
+ config.load_defaults('#{Ibrain.ibrain_version}')
66
67
 
67
- MSG
68
- end
68
+ MSG
69
+ end
69
70
 
70
- # @yield [config] Yields this configuration object to a block
71
- def configure
72
- yield(self)
73
- end
71
+ # @yield [config] Yields this configuration object to a block
72
+ def configure
73
+ yield(self)
74
+ end
74
75
 
75
- # @!attribute preference_store
76
- # Storage method for preferences.
77
- attr_writer :preference_store
76
+ # @!attribute preference_store
77
+ # Storage method for preferences.
78
+ attr_writer :preference_store
78
79
 
79
- def preference_store
80
- @preference_store ||= default_preferences
81
- end
80
+ def preference_store
81
+ @preference_store ||= default_preferences
82
+ end
82
83
 
83
- # Replace the default legacy preference store, which stores preferences in
84
- # the Ibrain_preferences table, with a plain in memory hash. This is faster
85
- # and less error prone.
86
- #
87
- # This will set all preferences to their default values.
88
- #
89
- # These won't be loaded from or persisted to the database, so any desired
90
- # changes must be made each time the application is started, such as in an
91
- # initializer.
92
- def use_static_preferences!
93
- @preference_store = default_preferences
94
- end
84
+ # Replace the default legacy preference store, which stores preferences in
85
+ # the Ibrain_preferences table, with a plain in memory hash. This is faster
86
+ # and less error prone.
87
+ #
88
+ # This will set all preferences to their default values.
89
+ #
90
+ # These won't be loaded from or persisted to the database, so any desired
91
+ # changes must be made each time the application is started, such as in an
92
+ # initializer.
93
+ def use_static_preferences!
94
+ @preference_store = default_preferences
95
+ end
95
96
 
96
- # Replace the new static preference store with the legacy store which
97
- # fetches preferences from the DB.
98
- def use_legacy_db_preferences!
99
- @preference_store = ScopedStore.new(self.class.name.underscore)
100
- end
97
+ # Replace the new static preference store with the legacy store which
98
+ # fetches preferences from the DB.
99
+ def use_legacy_db_preferences!
100
+ @preference_store = ScopedStore.new(self.class.name.underscore)
101
+ end
101
102
 
102
- alias_method :preferences, :preference_store
103
+ alias_method :preferences, :preference_store
103
104
 
104
- # Reset all preferences to their default values.
105
- def reset
106
- set(default_preferences)
107
- end
105
+ # Reset all preferences to their default values.
106
+ def reset
107
+ set(default_preferences)
108
+ end
108
109
 
109
- alias :[] :get_preference
110
- alias :[]= :set_preference
110
+ alias :[] :get_preference
111
+ alias :[]= :set_preference
111
112
 
112
- alias :get :get_preference
113
+ alias :get :get_preference
113
114
 
114
- # @param preferences [Hash] a hash of preferences to set
115
- def set(preferences)
116
- preferences.each do |name, value|
117
- set_preference name, value
115
+ # @param preferences [Hash] a hash of preferences to set
116
+ def set(preferences)
117
+ preferences.each do |name, value|
118
+ set_preference name, value
119
+ end
118
120
  end
119
- end
120
121
 
121
- # Generates a different preference default depending on {#version_defaults}
122
- #
123
- # This method is meant to be used in the `default:` keyword argument for
124
- # {.preference}. For instance, in the example, `foo`'s default was `true`
125
- # until version 3.0.0.alpha, when it became `false`:
126
- #
127
- # @example
128
- # preference :foo, :boolean, default: by_version(true, "3.0.0.alpha" => false)
129
- #
130
- # @see #loaded_defaults
131
- # @see Ibrain::Core::VersionedValue
132
- def self.by_version(*args)
133
- proc do |loaded_defaults|
134
- Ibrain::Core::VersionedValue.new(*args).call(loaded_defaults)
122
+ # Generates a different preference default depending on {#version_defaults}
123
+ #
124
+ # This method is meant to be used in the `default:` keyword argument for
125
+ # {.preference}. For instance, in the example, `foo`'s default was `true`
126
+ # until version 3.0.0.alpha, when it became `false`:
127
+ #
128
+ # @example
129
+ # preference :foo, :boolean, default: by_version(true, "3.0.0.alpha" => false)
130
+ #
131
+ # @see #loaded_defaults
132
+ # @see Ibrain::Core::VersionedValue
133
+ def self.by_version(*args)
134
+ proc do |loaded_defaults|
135
+ Ibrain::Core::VersionedValue.new(*args).call(loaded_defaults)
136
+ end
135
137
  end
136
- end
137
138
 
138
- def self.preference(name, type, options = {})
139
- super
140
- alias_method name.to_s, "preferred_#{name}"
141
- alias_method "#{name}=", "preferred_#{name}="
142
- end
139
+ def self.preference(name, type, options = {})
140
+ super
141
+ alias_method name.to_s, "preferred_#{name}"
142
+ alias_method "#{name}=", "preferred_#{name}="
143
+ end
143
144
 
144
- def self.class_name_attribute(name, default:)
145
- ivar = :"@#{name}"
145
+ def self.class_name_attribute(name, default:)
146
+ ivar = :"@#{name}"
146
147
 
147
- define_method("#{name}=") do |class_name|
148
- # If this is a named class constant, we should store it as a string to
149
- # allow code reloading.
150
- class_name = class_name.name if class_name.is_a?(Class) && class_name.name
148
+ define_method("#{name}=") do |class_name|
149
+ # If this is a named class constant, we should store it as a string to
150
+ # allow code reloading.
151
+ class_name = class_name.name if class_name.is_a?(Class) && class_name.name
151
152
 
152
- instance_variable_set(ivar, class_name)
153
- end
153
+ instance_variable_set(ivar, class_name)
154
+ end
154
155
 
155
- define_method(name) do
156
- class_name = instance_variable_get(ivar)
157
- class_name ||= default
158
- class_name = class_name.constantize if class_name.is_a?(String)
159
- class_name
156
+ define_method(name) do
157
+ class_name = instance_variable_get(ivar)
158
+ class_name ||= default
159
+ class_name = class_name.constantize if class_name.is_a?(String)
160
+ class_name
161
+ end
160
162
  end
161
- end
162
163
 
163
- private
164
+ private
164
165
 
165
- def context_for_default
166
- [loaded_defaults]
166
+ def context_for_default
167
+ [loaded_defaults]
168
+ end
167
169
  end
168
170
  end
@@ -4,176 +4,180 @@ require 'ibrain/preferences/preferable_class_methods'
4
4
  require 'active_support/concern'
5
5
  require 'active_support/core_ext/hash/keys'
6
6
 
7
- # Preferable allows defining preference accessor methods.
8
- #
9
- # A class including Preferable must implement #preferences which should return
10
- # an object responding to .fetch(key), []=(key, val), and .delete(key).
11
- #
12
- # It may also define a `#context_for_default` method. It should return an
13
- # array with the arguments to be provided to a proc used as the `default:`
14
- # keyword for a preference.
15
- #
16
- # The generated writer method performs typecasting before assignment into the
17
- # preferences object.
18
- #
19
- # Examples:
20
- #
21
- # # Ibrain::Base includes Preferable and defines preferences as a serialized
22
- # # column.
23
- # class Settings < Ibrain::Base
24
- # preference :color, :string, default: 'red'
25
- # preference :temperature, :integer, default: 21
26
- # end
27
- #
28
- # s = Settings.new
29
- # s.preferred_color # => 'red'
30
- # s.preferred_temperature # => 21
31
- #
32
- # s.preferred_color = 'blue'
33
- # s.preferred_color # => 'blue'
34
- #
35
- # # Typecasting is performed on assignment
36
- # s.preferred_temperature = '24'
37
- # s.preferred_color # => 24
38
- #
39
- # # Modifications have been made to the .preferences hash
40
- # s.preferences #=> {color: 'blue', temperature: 24}
41
- #
42
- # # Save the changes. All handled by activerecord
43
- # s.save!
44
- #
45
- # Each preference gets rendered as a form field in Ibrain backend.
46
- #
47
- # As not all supported preference types are representable as a form field, only
48
- # some of them get rendered per default. Arrays and Hashes for instance are
49
- # supported preference field types, but do not represent well as a form field.
50
- #
51
- # Overwrite +allowed_admin_form_preference_types+ in your class if you want to
52
- # provide more fields. If you do so, you also need to provide a preference field
53
- # partial that lives in:
54
- #
55
- # +app/views/ibrain/admin/shared/preference_fields/+
56
- #
57
- module Ibrain::Preferences::Preferable
58
- extend ActiveSupport::Concern
59
-
60
- included do
61
- extend Ibrain::Preferences::PreferableClassMethods
62
- end
63
-
64
- # Get a preference
65
- # @param name [#to_sym] name of preference
66
- # @return [Object] The value of preference +name+
67
- def get_preference(name)
68
- has_preference! name
69
- send self.class.preference_getter_method(name)
70
- end
71
-
72
- # Set a preference
73
- # @param name [#to_sym] name of preference
74
- # @param value [Object] new value for preference +name+
75
- def set_preference(name, value)
76
- has_preference! name
77
- send self.class.preference_setter_method(name), value
78
- end
7
+ module Ibrain
8
+ module Preferences
9
+ # Preferable allows defining preference accessor methods.
10
+ #
11
+ # A class including Preferable must implement #preferences which should return
12
+ # an object responding to .fetch(key), []=(key, val), and .delete(key).
13
+ #
14
+ # It may also define a `#context_for_default` method. It should return an
15
+ # array with the arguments to be provided to a proc used as the `default:`
16
+ # keyword for a preference.
17
+ #
18
+ # The generated writer method performs typecasting before assignment into the
19
+ # preferences object.
20
+ #
21
+ # Examples:
22
+ #
23
+ # # Ibrain::Base includes Preferable and defines preferences as a serialized
24
+ # # column.
25
+ # class Settings < Ibrain::Base
26
+ # preference :color, :string, default: 'red'
27
+ # preference :temperature, :integer, default: 21
28
+ # end
29
+ #
30
+ # s = Settings.new
31
+ # s.preferred_color # => 'red'
32
+ # s.preferred_temperature # => 21
33
+ #
34
+ # s.preferred_color = 'blue'
35
+ # s.preferred_color # => 'blue'
36
+ #
37
+ # # Typecasting is performed on assignment
38
+ # s.preferred_temperature = '24'
39
+ # s.preferred_color # => 24
40
+ #
41
+ # # Modifications have been made to the .preferences hash
42
+ # s.preferences #=> {color: 'blue', temperature: 24}
43
+ #
44
+ # # Save the changes. All handled by activerecord
45
+ # s.save!
46
+ #
47
+ # Each preference gets rendered as a form field in Ibrain backend.
48
+ #
49
+ # As not all supported preference types are representable as a form field, only
50
+ # some of them get rendered per default. Arrays and Hashes for instance are
51
+ # supported preference field types, but do not represent well as a form field.
52
+ #
53
+ # Overwrite +allowed_admin_form_preference_types+ in your class if you want to
54
+ # provide more fields. If you do so, you also need to provide a preference field
55
+ # partial that lives in:
56
+ #
57
+ # +app/views/ibrain/admin/shared/preference_fields/+
58
+ #
59
+ module Preferable
60
+ extend ActiveSupport::Concern
61
+
62
+ included do
63
+ extend Ibrain::Preferences::PreferableClassMethods
64
+ end
79
65
 
80
- # @param name [#to_sym] name of preference
81
- # @return [Symbol] The type of preference +name+
82
- def preference_type(name)
83
- has_preference! name
84
- send self.class.preference_type_getter_method(name)
85
- end
66
+ # Get a preference
67
+ # @param name [#to_sym] name of preference
68
+ # @return [Object] The value of preference +name+
69
+ def get_preference(name)
70
+ has_preference! name
71
+ send self.class.preference_getter_method(name)
72
+ end
86
73
 
87
- # @param name [#to_sym] name of preference
88
- # @return [Object] The default for preference +name+
89
- def preference_default(name)
90
- has_preference! name
91
- send self.class.preference_default_getter_method(name)
92
- end
74
+ # Set a preference
75
+ # @param name [#to_sym] name of preference
76
+ # @param value [Object] new value for preference +name+
77
+ def set_preference(name, value)
78
+ has_preference! name
79
+ send self.class.preference_setter_method(name), value
80
+ end
93
81
 
94
- # Raises an exception if the +name+ preference is not defined on this class
95
- # @param name [#to_sym] name of preference
96
- def has_preference!(name)
97
- raise NoMethodError.new "#{name} preference not defined" unless has_preference? name
98
- end
82
+ # @param name [#to_sym] name of preference
83
+ # @return [Symbol] The type of preference +name+
84
+ def preference_type(name)
85
+ has_preference! name
86
+ send self.class.preference_type_getter_method(name)
87
+ end
99
88
 
100
- # @param name [#to_sym] name of preference
101
- # @return [Boolean] if preference exists on this class
102
- def has_preference?(name)
103
- defined_preferences.include?(name.to_sym)
104
- end
89
+ # @param name [#to_sym] name of preference
90
+ # @return [Object] The default for preference +name+
91
+ def preference_default(name)
92
+ has_preference! name
93
+ send self.class.preference_default_getter_method(name)
94
+ end
105
95
 
106
- # @return [Array<Symbol>] All preferences defined on this class
107
- def defined_preferences
108
- self.class.defined_preferences
109
- end
96
+ # Raises an exception if the +name+ preference is not defined on this class
97
+ # @param name [#to_sym] name of preference
98
+ def has_preference!(name)
99
+ raise NoMethodError.new "#{name} preference not defined" unless has_preference? name
100
+ end
110
101
 
111
- # @return [Hash{Symbol => Object}] Default for all preferences defined on this class
112
- def default_preferences
113
- defined_preferences.index_with do |preference|
114
- preference_default(preference)
102
+ # @param name [#to_sym] name of preference
103
+ # @return [Boolean] if preference exists on this class
104
+ def has_preference?(name)
105
+ defined_preferences.include?(name.to_sym)
115
106
  end
116
- end
117
107
 
118
- # Preference names representable as form fields in Ibrain backend
119
- #
120
- # Not all preferences are representable as a form field.
121
- #
122
- # Arrays and Hashes for instance are supported preference field types,
123
- # but do not represent well as a form field.
124
- #
125
- # As these kind of preferences are mostly developer facing
126
- # and not admin facing we should not render them.
127
- #
128
- # Overwrite +allowed_admin_form_preference_types+ in your class that
129
- # includes +Ibrain::Preferable+ if you want to provide more fields.
130
- # If you do so, you also need to provide a preference field partial
131
- # that lives in:
132
- #
133
- # +app/views/ibrain/admin/shared/preference_fields/+
134
- #
135
- # @return [Array]
136
-
137
- private
138
-
139
- def convert_preference_value(value, type, preference_encryptor = nil)
140
- return nil if value.nil?
141
-
142
- case type
143
- when :string, :text, :password
144
- value.to_s
145
- when :encrypted_string
146
- preference_encryptor.encrypt(value.to_s)
147
- when :decimal
148
- begin
149
- value.to_s.to_d
150
- rescue ArgumentError
151
- BigDecimal('0')
108
+ # @return [Array<Symbol>] All preferences defined on this class
109
+ def defined_preferences
110
+ self.class.defined_preferences
152
111
  end
153
- when :integer
154
- value.to_i
155
- when :boolean
156
- if !value ||
157
- value.to_s =~ /\A(f|false|0|^)\Z/i ||
158
- (value.respond_to?(:empty?) && value.empty?)
159
- false
160
- else
161
- true
112
+
113
+ # @return [Hash{Symbol => Object}] Default for all preferences defined on this class
114
+ def default_preferences
115
+ defined_preferences.index_with do |preference|
116
+ preference_default(preference)
117
+ end
162
118
  end
163
- when :array
164
- raise TypeError, "Array expected got #{value.inspect}" unless value.is_a?(Array)
165
119
 
166
- value
167
- when :hash
168
- raise TypeError, "Hash expected got #{value.inspect}" unless value.is_a?(Hash)
120
+ # Preference names representable as form fields in Ibrain backend
121
+ #
122
+ # Not all preferences are representable as a form field.
123
+ #
124
+ # Arrays and Hashes for instance are supported preference field types,
125
+ # but do not represent well as a form field.
126
+ #
127
+ # As these kind of preferences are mostly developer facing
128
+ # and not admin facing we should not render them.
129
+ #
130
+ # Overwrite +allowed_admin_form_preference_types+ in your class that
131
+ # includes +Ibrain::Preferable+ if you want to provide more fields.
132
+ # If you do so, you also need to provide a preference field partial
133
+ # that lives in:
134
+ #
135
+ # +app/views/ibrain/admin/shared/preference_fields/+
136
+ #
137
+ # @return [Array]
138
+
139
+ private
140
+
141
+ def convert_preference_value(value, type, preference_encryptor = nil)
142
+ return nil if value.nil?
143
+
144
+ case type
145
+ when :string, :text, :password
146
+ value.to_s
147
+ when :encrypted_string
148
+ preference_encryptor.encrypt(value.to_s)
149
+ when :decimal
150
+ begin
151
+ value.to_s.to_d
152
+ rescue ArgumentError
153
+ BigDecimal('0')
154
+ end
155
+ when :integer
156
+ value.to_i
157
+ when :boolean
158
+ if !value ||
159
+ value.to_s =~ /\A(f|false|0|^)\Z/i ||
160
+ (value.respond_to?(:empty?) && value.empty?)
161
+ false
162
+ else
163
+ true
164
+ end
165
+ when :array
166
+ raise TypeError, "Array expected got #{value.inspect}" unless value.is_a?(Array)
167
+
168
+ value
169
+ when :hash
170
+ raise TypeError, "Hash expected got #{value.inspect}" unless value.is_a?(Hash)
171
+
172
+ value
173
+ else
174
+ value
175
+ end
176
+ end
169
177
 
170
- value
171
- else
172
- value
178
+ def context_for_default
179
+ [].freeze
180
+ end
173
181
  end
174
182
  end
175
-
176
- def context_for_default
177
- [].freeze
178
- end
179
183
  end