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.
- 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,166 +3,168 @@
|
|
3
3
|
require 'ibrain/core/versioned_value'
|
4
4
|
require 'ibrain/preferences/preferable'
|
5
5
|
|
6
|
-
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
# a
|
19
|
-
# a
|
20
|
-
# a.
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
# a
|
26
|
-
# a
|
27
|
-
# a.
|
28
|
-
#
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
-
|
57
|
-
|
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
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|
-
|
66
|
+
config.load_defaults('#{Ibrain.ibrain_version}')
|
66
67
|
|
67
|
-
|
68
|
-
|
68
|
+
MSG
|
69
|
+
end
|
69
70
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
71
|
+
# @yield [config] Yields this configuration object to a block
|
72
|
+
def configure
|
73
|
+
yield(self)
|
74
|
+
end
|
74
75
|
|
75
|
-
|
76
|
-
|
77
|
-
|
76
|
+
# @!attribute preference_store
|
77
|
+
# Storage method for preferences.
|
78
|
+
attr_writer :preference_store
|
78
79
|
|
79
|
-
|
80
|
-
|
81
|
-
|
80
|
+
def preference_store
|
81
|
+
@preference_store ||= default_preferences
|
82
|
+
end
|
82
83
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
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
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
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
|
-
|
103
|
+
alias_method :preferences, :preference_store
|
103
104
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
105
|
+
# Reset all preferences to their default values.
|
106
|
+
def reset
|
107
|
+
set(default_preferences)
|
108
|
+
end
|
108
109
|
|
109
|
-
|
110
|
-
|
110
|
+
alias :[] :get_preference
|
111
|
+
alias :[]= :set_preference
|
111
112
|
|
112
|
-
|
113
|
+
alias :get :get_preference
|
113
114
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
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
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
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
|
-
|
145
|
-
|
145
|
+
def self.class_name_attribute(name, default:)
|
146
|
+
ivar = :"@#{name}"
|
146
147
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
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
|
-
|
153
|
-
|
153
|
+
instance_variable_set(ivar, class_name)
|
154
|
+
end
|
154
155
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
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
|
-
|
164
|
+
private
|
164
165
|
|
165
|
-
|
166
|
-
|
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
|
-
|
8
|
-
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
# s
|
31
|
-
#
|
32
|
-
# s.
|
33
|
-
#
|
34
|
-
#
|
35
|
-
# #
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
# #
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
# supported preference
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
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
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
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
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
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
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
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
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
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
|
-
|
119
|
-
|
120
|
-
|
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
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
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
|
-
|
167
|
-
|
168
|
-
|
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
|
-
|
171
|
-
|
172
|
-
|
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
|