has_setting 0.55 → 1.0.0

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