has_setting 0.7.4 → 0.55
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/has_setting/ar_extensions.rb +6 -27
- data/lib/has_setting/version.rb +1 -1
- data/test/unit/has_setting_test.rb +29 -23
- metadata +12 -10
- checksums.yaml +0 -7
- data/lib/.DS_Store +0 -0
@@ -11,7 +11,7 @@ module HasSetting
|
|
11
11
|
self.class_eval do
|
12
12
|
unless @has_setting_options # define only once
|
13
13
|
# AR association to settings
|
14
|
-
has_many( :settings, :as => :owner, :class_name => 'HasSetting::Setting',
|
14
|
+
has_many( :settings, :as => :owner, :class_name => 'HasSetting::Setting',
|
15
15
|
:foreign_key => :owner_id, :dependent => :destroy)
|
16
16
|
after_save(:save_has_setting_association)
|
17
17
|
@has_setting_options = {}
|
@@ -34,7 +34,6 @@ module HasSetting
|
|
34
34
|
# default options
|
35
35
|
type = options[:type] || :string # treat as string
|
36
36
|
options[:localize] ||= false
|
37
|
-
options[:no_fallback] ||= false
|
38
37
|
self.has_setting_options[name] = options
|
39
38
|
|
40
39
|
# setter
|
@@ -46,7 +45,7 @@ module HasSetting
|
|
46
45
|
# getter
|
47
46
|
define_method(name) do |*args|
|
48
47
|
setting = read_setting(name)
|
49
|
-
options = args.first ||
|
48
|
+
options = args.first || self.class.has_setting_options[name]
|
50
49
|
return options[:default] if setting.nil?
|
51
50
|
formatter = Formatters.for_type(options[:type] || type)
|
52
51
|
formatter.to_type(setting.value)
|
@@ -60,40 +59,20 @@ module HasSetting
|
|
60
59
|
setting = self.settings.detect() {|item| item.name == name and item.locale.to_s == locale.to_s }
|
61
60
|
setting = self.settings.build(:name => name, locale: locale) if setting.blank?
|
62
61
|
setting.value = value
|
63
|
-
# mark model as changed to make sure model is saved so that after_save is triggered even if only settings change
|
64
|
-
# maybe there is a better way of doing this.
|
65
|
-
self.updated_at = Time.now if self.respond_to?(:updated_at)
|
66
62
|
end
|
67
63
|
|
68
64
|
def read_setting(name)
|
69
|
-
# use detect instead of SQL find. like this the 'cached' has_many-collection is inited
|
65
|
+
# use detect instead of SQL find. like this the 'cached' has_many-collection is inited
|
70
66
|
# only once
|
71
|
-
locale = localize?(name) ? I18n.locale.to_s :
|
67
|
+
locale = localize?(name) ? I18n.locale.to_s : nil
|
72
68
|
s = self.settings.detect() {|item| item.name == name and item.locale.to_s == locale} # first see if there is a setting with current locale
|
73
|
-
s ||= self.settings.detect() {|item| item.name == name}
|
69
|
+
s ||= self.settings.detect() {|item| item.name == name} # then if not found, take the first setting with matching name (TODO: add locale fallbacks)
|
74
70
|
s
|
75
71
|
end
|
76
72
|
|
77
73
|
def localize? name
|
78
|
-
options =
|
74
|
+
options = self.class.has_setting_options[name]
|
79
75
|
options ? options[:localize] : false
|
80
76
|
end
|
81
77
|
|
82
|
-
def no_fallback? name
|
83
|
-
options = has_setting_option name
|
84
|
-
options ? options[:no_fallback] : false
|
85
|
-
end
|
86
|
-
|
87
|
-
def has_setting_option name
|
88
|
-
klass = self.class
|
89
|
-
option = klass.has_setting_options ? klass.has_setting_options[name] : nil
|
90
|
-
while option.nil? and
|
91
|
-
klass.superclass != ActiveRecord::Base and
|
92
|
-
klass.superclass.respond_to?(:has_setting_options)
|
93
|
-
klass = klass.superclass
|
94
|
-
option = klass.has_setting_options[name]
|
95
|
-
end
|
96
|
-
option
|
97
|
-
end
|
98
|
-
|
99
78
|
end
|
data/lib/has_setting/version.rb
CHANGED
@@ -6,13 +6,19 @@ class HasSettingTest < Test::Unit::TestCase
|
|
6
6
|
@bar = Bar.create!
|
7
7
|
@baz = Baz.create!
|
8
8
|
end
|
9
|
-
|
10
|
-
|
9
|
+
|
10
|
+
|
11
11
|
def test_setting_has_accessors
|
12
12
|
assert @foo.respond_to?(:setting_1)
|
13
13
|
assert @foo.respond_to?(:setting_1=)
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
|
+
|
17
|
+
def test_has_many
|
18
|
+
assert @foo.respond_to?(:settings)
|
19
|
+
assert @foo.settings.is_a?(Array)
|
20
|
+
end
|
21
|
+
|
16
22
|
def test_settings_are_destroyed
|
17
23
|
count_before = HasSetting::Setting.count
|
18
24
|
@foo.setting_1 = 10
|
@@ -20,9 +26,9 @@ class HasSettingTest < Test::Unit::TestCase
|
|
20
26
|
assert_equal(count_before + 1, HasSetting::Setting.count)
|
21
27
|
@foo.destroy
|
22
28
|
assert_equal(count_before, HasSetting::Setting.count)
|
23
|
-
|
29
|
+
|
24
30
|
end
|
25
|
-
|
31
|
+
|
26
32
|
def test_write_setting
|
27
33
|
count_before = HasSetting::Setting.count
|
28
34
|
@foo.write_setting('name', 'value1')
|
@@ -31,14 +37,14 @@ class HasSettingTest < Test::Unit::TestCase
|
|
31
37
|
setting = @foo.read_setting('name')
|
32
38
|
assert setting
|
33
39
|
assert_equal('value1', setting.value)
|
34
|
-
|
40
|
+
|
35
41
|
@foo.write_setting('name', 'value2')
|
36
|
-
assert_equal(count_before + 1, HasSetting::Setting.count)
|
42
|
+
assert_equal(count_before + 1, HasSetting::Setting.count)
|
37
43
|
setting = @foo.read_setting('name')
|
38
44
|
assert setting
|
39
45
|
assert_equal('value2', setting.value)
|
40
46
|
end
|
41
|
-
|
47
|
+
|
42
48
|
def test_setting_accessors
|
43
49
|
count_before = HasSetting::Setting.count
|
44
50
|
assert(!@foo.setting_1)
|
@@ -49,7 +55,7 @@ class HasSettingTest < Test::Unit::TestCase
|
|
49
55
|
@foo.setting_1 = 'bla'
|
50
56
|
assert_equal('bla', @foo.setting_1)
|
51
57
|
end
|
52
|
-
|
58
|
+
|
53
59
|
def test_different_classes_do_not_share_setting
|
54
60
|
count_before = HasSetting::Setting.count
|
55
61
|
@foo.setting_1 = 'foo'
|
@@ -57,43 +63,43 @@ class HasSettingTest < Test::Unit::TestCase
|
|
57
63
|
@bar.setting_1 = 'bar'
|
58
64
|
@bar.save!
|
59
65
|
assert_equal(count_before + 2, HasSetting::Setting.count)
|
60
|
-
|
66
|
+
|
61
67
|
assert_equal('foo', @foo.setting_1)
|
62
68
|
assert_equal('bar', @bar.setting_1)
|
63
69
|
end
|
64
70
|
|
65
|
-
|
71
|
+
|
66
72
|
def test_has_nil_setting
|
67
73
|
@foo.setting_1 = nil
|
68
74
|
assert(@foo.read_setting('setting_1'))
|
69
75
|
assert(!@foo.setting_1)
|
70
76
|
end
|
71
|
-
|
77
|
+
|
72
78
|
def test_options_on_getter
|
73
79
|
@foo.setting_1 = '12.3'
|
74
80
|
assert_equal('12.3', @foo.setting_1)
|
75
81
|
assert_equal(12, @foo.setting_1(:type => :int))
|
76
82
|
assert_equal(12.3, @foo.setting_1(:type => :float))
|
77
|
-
|
83
|
+
|
78
84
|
# Foo.setting_2 is a float setting. Override and get as string
|
79
85
|
@foo.setting_2 = 12.3
|
80
86
|
assert_equal('12.3', @foo.setting_2(:type => :string))
|
81
87
|
end
|
82
|
-
|
88
|
+
|
83
89
|
def test_different_classes_do_not_share_options()
|
84
90
|
@foo.setting_2 = 12.3
|
85
91
|
assert_equal(12.3, @foo.setting_2)
|
86
92
|
@bar.setting_2 = 12.3
|
87
93
|
assert_equal(12, @bar.setting_2)
|
88
94
|
end
|
89
|
-
|
95
|
+
|
90
96
|
def test_default_values()
|
91
97
|
assert_equal('def', @foo.with_default)
|
92
98
|
assert_equal('override def', @foo.with_default(:default => 'override def'))
|
93
99
|
@foo.with_default = 'not def'
|
94
100
|
assert_equal('not def', @foo.with_default)
|
95
101
|
end
|
96
|
-
|
102
|
+
|
97
103
|
def test_write_settings_without_saved_parent
|
98
104
|
my_foo = Foo.new
|
99
105
|
count_before = HasSetting::Setting.count
|
@@ -104,19 +110,19 @@ class HasSettingTest < Test::Unit::TestCase
|
|
104
110
|
assert_equal(count_before + 1, HasSetting::Setting.count)
|
105
111
|
assert_equal('radabumm', my_foo.with_default)
|
106
112
|
end
|
107
|
-
|
113
|
+
|
108
114
|
def test_not_everyone_has_settings_association
|
109
115
|
assert_equal(true, @foo.respond_to?(:settings))
|
110
116
|
assert_equal(true, @bar.respond_to?(:settings))
|
111
117
|
assert_equal(false, @baz.respond_to?(:settings))
|
112
118
|
end
|
113
|
-
|
114
|
-
|
119
|
+
|
120
|
+
|
115
121
|
def test_boolean_setting_without_default
|
116
122
|
assert_equal nil, @foo.setting_3
|
117
123
|
@foo.setting_3 = true
|
118
124
|
@foo.save!
|
119
|
-
|
125
|
+
|
120
126
|
@foo = Foo.find @foo.id
|
121
127
|
assert_equal true, @foo.setting_3
|
122
128
|
end
|
@@ -127,7 +133,7 @@ class HasSettingTest < Test::Unit::TestCase
|
|
127
133
|
@foo = Foo.find @foo.id
|
128
134
|
assert_equal true, @foo.setting_4
|
129
135
|
end
|
130
|
-
|
136
|
+
|
131
137
|
def test_boolean_setting_with_default_and_no_saving
|
132
138
|
assert_equal false, @foo.setting_4
|
133
139
|
@foo.setting_4 = true
|
@@ -147,6 +153,6 @@ class HasSettingTest < Test::Unit::TestCase
|
|
147
153
|
I18n.locale = :de
|
148
154
|
assert_equal 'setting de', @bar.setting_1
|
149
155
|
end
|
150
|
-
|
151
|
-
|
156
|
+
|
157
|
+
|
152
158
|
end
|
metadata
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_setting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.55'
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Simplificator GmbH
|
@@ -9,7 +10,7 @@ authors:
|
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date:
|
13
|
+
date: 2013-06-05 00:00:00.000000000 Z
|
13
14
|
dependencies: []
|
14
15
|
description: Stores settings as key/value pairs in a settings table and provides accessors
|
15
16
|
for them on the owning object
|
@@ -20,13 +21,12 @@ executables: []
|
|
20
21
|
extensions: []
|
21
22
|
extra_rdoc_files: []
|
22
23
|
files:
|
23
|
-
-
|
24
|
+
- .gitignore
|
24
25
|
- README.md
|
25
26
|
- Rakefile
|
26
27
|
- VERSION.yml
|
27
28
|
- has_setting.gemspec
|
28
29
|
- help/001_create_settings.rb
|
29
|
-
- lib/.DS_Store
|
30
30
|
- lib/has_setting.rb
|
31
31
|
- lib/has_setting/ar_extensions.rb
|
32
32
|
- lib/has_setting/formatters.rb
|
@@ -40,27 +40,28 @@ files:
|
|
40
40
|
- test/unit/has_setting_test.rb
|
41
41
|
homepage: http://github.com/ncri/has_setting
|
42
42
|
licenses: []
|
43
|
-
metadata: {}
|
44
43
|
post_install_message:
|
45
44
|
rdoc_options:
|
46
|
-
-
|
45
|
+
- --charset=UTF-8
|
47
46
|
require_paths:
|
48
47
|
- lib
|
49
48
|
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
50
|
requirements:
|
51
|
-
- -
|
51
|
+
- - ! '>='
|
52
52
|
- !ruby/object:Gem::Version
|
53
53
|
version: '0'
|
54
54
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
55
56
|
requirements:
|
56
|
-
- -
|
57
|
+
- - ! '>='
|
57
58
|
- !ruby/object:Gem::Version
|
58
59
|
version: '0'
|
59
60
|
requirements: []
|
60
61
|
rubyforge_project:
|
61
|
-
rubygems_version:
|
62
|
+
rubygems_version: 1.8.24
|
62
63
|
signing_key:
|
63
|
-
specification_version:
|
64
|
+
specification_version: 3
|
64
65
|
summary: Simple setting extension to AR
|
65
66
|
test_files:
|
66
67
|
- test/bar.rb
|
@@ -69,3 +70,4 @@ test_files:
|
|
69
70
|
- test/test_helper.rb
|
70
71
|
- test/unit/formatters_test.rb
|
71
72
|
- test/unit/has_setting_test.rb
|
73
|
+
has_rdoc: true
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: 628241cce8245e1e0d2d76bb35fb4bc33e6e00bc
|
4
|
-
data.tar.gz: 38e4e899954422e7a23a6ec786f877c7a1702e6d
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: 85f62ddf97adb6282b0e3347965c5411d7489534ccd026d4317e6d3a54c210b4bb77a7d159c706b0da7f9ae663808c7bbd7512a179aa63bb79990d0d4a463125
|
7
|
-
data.tar.gz: 1e2a0f35a9e60f32c0db6052447a6f7f17ab467623855d6fba20359c055be4aeee3ef368b1aadd3ad4b5c6a177cb6d5d0bcfa71fb85a4074025975f6889ebffe
|
data/lib/.DS_Store
DELETED
Binary file
|