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,168 +3,166 @@
3
3
  require 'ibrain/core/versioned_value'
4
4
  require 'ibrain/preferences/preferable'
5
5
 
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
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
47
46
 
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
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
56
55
 
57
- def check_load_defaults_called(instance_constant_name = nil)
58
- return if load_defaults_called || !Ibrain::Core.has_install_generator_been_run?
56
+ def check_load_defaults_called(instance_constant_name = nil)
57
+ return if load_defaults_called || !Ibrain::Core.has_install_generator_been_run?
59
58
 
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:
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:
65
64
 
66
- config.load_defaults('#{Ibrain.ibrain_version}')
65
+ config.load_defaults('#{Ibrain.ibrain_version}')
67
66
 
68
- MSG
69
- end
67
+ MSG
68
+ end
70
69
 
71
- # @yield [config] Yields this configuration object to a block
72
- def configure
73
- yield(self)
74
- end
70
+ # @yield [config] Yields this configuration object to a block
71
+ def configure
72
+ yield(self)
73
+ end
75
74
 
76
- # @!attribute preference_store
77
- # Storage method for preferences.
78
- attr_writer :preference_store
75
+ # @!attribute preference_store
76
+ # Storage method for preferences.
77
+ attr_writer :preference_store
79
78
 
80
- def preference_store
81
- @preference_store ||= default_preferences
82
- end
79
+ def preference_store
80
+ @preference_store ||= default_preferences
81
+ end
83
82
 
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
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
96
95
 
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
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
102
101
 
103
- alias_method :preferences, :preference_store
102
+ alias_method :preferences, :preference_store
104
103
 
105
- # Reset all preferences to their default values.
106
- def reset
107
- set(default_preferences)
108
- end
104
+ # Reset all preferences to their default values.
105
+ def reset
106
+ set(default_preferences)
107
+ end
109
108
 
110
- alias :[] :get_preference
111
- alias :[]= :set_preference
109
+ alias :[] :get_preference
110
+ alias :[]= :set_preference
112
111
 
113
- alias :get :get_preference
112
+ alias :get :get_preference
114
113
 
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
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
120
118
  end
119
+ end
121
120
 
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
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)
137
135
  end
136
+ end
138
137
 
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
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
144
143
 
145
- def self.class_name_attribute(name, default:)
146
- ivar = :"@#{name}"
144
+ def self.class_name_attribute(name, default:)
145
+ ivar = :"@#{name}"
147
146
 
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
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
152
151
 
153
- instance_variable_set(ivar, class_name)
154
- end
152
+ instance_variable_set(ivar, class_name)
153
+ end
155
154
 
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
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
162
160
  end
161
+ end
163
162
 
164
- private
163
+ private
165
164
 
166
- def context_for_default
167
- [loaded_defaults]
168
- end
165
+ def context_for_default
166
+ [loaded_defaults]
169
167
  end
170
168
  end
@@ -4,180 +4,176 @@ require 'ibrain/preferences/preferable_class_methods'
4
4
  require 'active_support/concern'
5
5
  require 'active_support/core_ext/hash/keys'
6
6
 
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
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
65
63
 
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
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
73
71
 
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
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
81
79
 
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
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
88
86
 
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
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
95
93
 
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
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
101
99
 
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)
106
- end
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
107
105
 
108
- # @return [Array<Symbol>] All preferences defined on this class
109
- def defined_preferences
110
- self.class.defined_preferences
111
- end
106
+ # @return [Array<Symbol>] All preferences defined on this class
107
+ def defined_preferences
108
+ self.class.defined_preferences
109
+ end
112
110
 
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
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)
118
115
  end
116
+ end
119
117
 
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
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')
176
152
  end
177
-
178
- def context_for_default
179
- [].freeze
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
180
162
  end
163
+ when :array
164
+ raise TypeError, "Array expected got #{value.inspect}" unless value.is_a?(Array)
165
+
166
+ value
167
+ when :hash
168
+ raise TypeError, "Hash expected got #{value.inspect}" unless value.is_a?(Hash)
169
+
170
+ value
171
+ else
172
+ value
181
173
  end
182
174
  end
175
+
176
+ def context_for_default
177
+ [].freeze
178
+ end
183
179
  end