setting_accessors 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0b13cae5880dc3e448d1d26d64ff6bce9149d81
|
4
|
+
data.tar.gz: 7b56e7cec309297d20edfb0a90a346c028c98ad9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 062e4ad9c0fb336a109aae63f9987715dbfce2d9ac5b2b607a8a4a5f339e0e64a020e98606e76cdb2fdcb615de895a42843307dd50d988165d18faf6919430e3
|
7
|
+
data.tar.gz: e140c13822008a93b770ece981ea8be7b75d1b155df28bb5d1fe5f39d6477f7f1cc9fe63f170e081030a79405b3f59cc1bc4917b501bfb40d834c9ba1e2b4c65
|
@@ -13,7 +13,7 @@ class SettingAccessors::Accessor
|
|
13
13
|
# Gets a setting's value
|
14
14
|
#
|
15
15
|
def [](key)
|
16
|
-
@temp_settings[key.to_sym]
|
16
|
+
has_key?(key) ? @temp_settings[key.to_sym] : SettingAccessors.setting_class.get(key, @record)
|
17
17
|
end
|
18
18
|
|
19
19
|
def has_key?(key)
|
@@ -49,6 +49,9 @@ module SettingAccessors::Integration
|
|
49
49
|
#create multi-param fields in forms.
|
50
50
|
self.columns_hash[setting_name.to_s] = OpenStruct.new(type: SettingAccessors::Internal.setting_value_type(setting_name, self.new).to_sym)
|
51
51
|
|
52
|
+
#Add the setting's name to the list of setting_accessors for this class
|
53
|
+
SettingAccessors::Internal.add_setting_accessor_name(self, setting_name)
|
54
|
+
|
52
55
|
#Getter
|
53
56
|
define_method(setting_name) do
|
54
57
|
settings.get_with_fallback(setting_name, fallback)
|
@@ -76,6 +79,14 @@ module SettingAccessors::Integration
|
|
76
79
|
end
|
77
80
|
end
|
78
81
|
|
82
|
+
def as_json(*args)
|
83
|
+
json = super(*args)
|
84
|
+
SettingAccessors::Internal.setting_accessor_names(self.class).each do |setting_name|
|
85
|
+
json[setting_name.to_s] = send(setting_name)
|
86
|
+
end
|
87
|
+
json
|
88
|
+
end
|
89
|
+
|
79
90
|
def settings
|
80
91
|
@settings_accessor ||= SettingAccessors::Accessor.new(self)
|
81
92
|
end
|
@@ -69,7 +69,6 @@ module SettingAccessors
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
-
|
73
72
|
#
|
74
73
|
# @return [Hash] configuration data regarding this setting
|
75
74
|
#
|
@@ -104,5 +103,24 @@ module SettingAccessors
|
|
104
103
|
def self.get_class_setting(klass, setting_name)
|
105
104
|
self.lookup_nested_hash(@@class_settings, klass.to_s, setting_name.to_s)
|
106
105
|
end
|
106
|
+
|
107
|
+
#
|
108
|
+
# Adds the given setting name to the list of used setting accessors
|
109
|
+
# in the given class.
|
110
|
+
# This is mainly to keep track of all accessors defined in the different classes
|
111
|
+
#
|
112
|
+
def self.add_setting_accessor_name(klass, setting_name)
|
113
|
+
@@setting_accessor_names ||= {}
|
114
|
+
@@setting_accessor_names[klass.to_s] ||= []
|
115
|
+
@@setting_accessor_names[klass.to_s] << setting_name.to_s
|
116
|
+
end
|
117
|
+
|
118
|
+
#
|
119
|
+
# @return [Array<String>] all setting accessor names defined in the given +class+
|
120
|
+
#
|
121
|
+
def self.setting_accessor_names(klass)
|
122
|
+
self.lookup_nested_hash(@@setting_accessor_names, klass.to_s)
|
123
|
+
end
|
124
|
+
|
107
125
|
end
|
108
126
|
end
|
@@ -1,5 +1,20 @@
|
|
1
1
|
class UserTest < ActiveSupport::TestCase
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
context 'JSON serialization' do
|
3
|
+
setup do
|
4
|
+
@user = User.new(:a_string => 'test', :a_number => 42, :a_boolean => false)
|
5
|
+
end
|
6
|
+
|
7
|
+
should 'include the setting accessors' do
|
8
|
+
SettingAccessors::Internal.setting_accessor_names(User).each do |setting_name|
|
9
|
+
assert_includes @user.as_json.keys, setting_name
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
should 'contain the correct values' do
|
14
|
+
SettingAccessors::Internal.setting_accessor_names(User).each do |setting_name|
|
15
|
+
assert_equal @user.as_json[setting_name.to_s], @user.send(setting_name)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
5
20
|
end
|