ledermann-rails-settings 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.md CHANGED
@@ -1,3 +1,7 @@
1
+ Version 1.2.0 (2012-07-21)
2
+
3
+ - Added model-level settings (thanks to Jim Ryan)
4
+
1
5
  Version 1.1.0 (2012-06-01)
2
6
 
3
7
  - Added caching (thanks to Matthew McEachen)
data/README.md CHANGED
@@ -78,7 +78,7 @@ Decide you dont want to track a particular setting anymore?
78
78
  Settings.foo
79
79
  # => nil
80
80
 
81
- Want a list of all the settings?
81
+ Want a list of all the settings (including defaults)?
82
82
 
83
83
  Settings.all
84
84
  # => { 'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y' }
@@ -91,17 +91,6 @@ You need name spaces and want a list of settings for a give name space? Just cho
91
91
  Settings.all('preferences.')
92
92
  # => { 'preferences.color' => :blue, 'preferences.size' => :large }
93
93
 
94
- Set defaults for certain settings of your app. This will cause the defined settings to return with the
95
- Specified value even if they are not in the database. Make a new file in config/initializers/settings.rb
96
- with the following:
97
-
98
- Settings.defaults[:some_setting] = 'footastic'
99
-
100
- Now even if the database is completely empty, you app will have some intelligent defaults:
101
-
102
- Settings.some_setting
103
- # => 'footastic'
104
-
105
94
  Settings may be bound to any existing ActiveRecord object. Define this association like this:
106
95
 
107
96
  class User < ActiveRecord::Base
@@ -118,6 +107,35 @@ Then you can set/get a setting for a given user instance just by doing this:
118
107
  user.settings.all
119
108
  # => { "color" => :red }
120
109
 
110
+
111
+ Set defaults for certain settings of your app. This will cause the defined settings to return with the
112
+ specified value even if they are not in the database. Make a new file in config/initializers/settings.rb
113
+ with the following:
114
+
115
+ Settings.defaults[:foo] = 'footastic'
116
+
117
+ Now even if the database is completely empty, you app will have some intelligent defaults:
118
+
119
+ Settings.foo
120
+ # => 'footastic'
121
+
122
+ Defaults can be defined on the model level, too:
123
+
124
+ User.settings.foo = 'bar'
125
+ User.find(123).settings.foo
126
+ # => 'bar'
127
+
128
+ If the setting doesn't exist on the object or the model, you'll get the default, as expected:
129
+
130
+ Settings.defaults[:some_default] = 'foo'
131
+
132
+ User.settings.some_default
133
+ # => 'foo'
134
+
135
+ User.find(123).settings.some_default
136
+ # => 'foo'
137
+
138
+
121
139
  I you want to find users having or not having some settings, there are named scopes for this:
122
140
 
123
141
  User.with_settings
@@ -4,7 +4,15 @@ ActiveRecord::Base.class_eval do
4
4
  def settings
5
5
  ScopedSettings.for_target(self)
6
6
  end
7
-
7
+
8
+ def self.settings
9
+ ScopedSettings.for_target(self)
10
+ end
11
+
12
+ def settings=(hash)
13
+ hash.each { |k,v| settings[k] = v }
14
+ end
15
+
8
16
  after_destroy { |user| user.settings.target_scoped.delete_all }
9
17
 
10
18
  scope_method = ActiveRecord::VERSION::MAJOR < 3 ? :named_scope : :scope
@@ -5,10 +5,10 @@ class ScopedSettings < Settings
5
5
  end
6
6
 
7
7
  def self.target_id
8
- @target.id
8
+ @target.is_a?(Class) ? nil : @target.id
9
9
  end
10
10
 
11
11
  def self.target_type
12
- @target.class.base_class.to_s
12
+ @target.is_a?(Class) ? @target.name : @target.class.base_class.to_s
13
13
  end
14
14
  end
@@ -68,16 +68,22 @@ class Settings < ActiveRecord::Base
68
68
  vars.each do |record|
69
69
  result[record.var] = record.value
70
70
  end
71
- result.with_indifferent_access
71
+ defaults = @@defaults.select{ |k, v| k =~ /^#{starting_with}/ }
72
+ defaults = Hash[defaults] if defaults.is_a?(Array)
73
+ defaults.merge(result).with_indifferent_access
72
74
  end
73
75
 
74
76
  #get a setting value by [] notation
75
77
  def self.[](var_name)
76
78
  cache.fetch(cache_key(var_name), cache_options) do
77
- if (var = target(var_name)).present?
79
+ if var = target(var_name)
78
80
  var.value
79
81
  else
80
- defaults[var_name.to_s]
82
+ if target_id.nil?
83
+ @@defaults[var_name.to_s]
84
+ else
85
+ target_type.constantize.settings[var_name.to_s]
86
+ end
81
87
  end
82
88
  end
83
89
  end
@@ -1,3 +1,3 @@
1
1
  module RailsSettings
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -19,7 +19,8 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ['lib']
20
20
 
21
21
  # specify any dependencies here; for example:
22
- # s.add_development_dependency "rspec"
22
+ s.add_development_dependency "rake"
23
+ s.add_development_dependency "sqlite3"
23
24
  # s.add_runtime_dependency "rest-client"
24
25
  s.add_dependency 'activerecord', '>= 2.3'
25
26
  s.add_development_dependency 'rake'
@@ -4,8 +4,11 @@ class SettingsTest < Test::Unit::TestCase
4
4
  setup_db
5
5
 
6
6
  def setup
7
- Settings.create(:var => 'test', :value => 'foo')
8
- Settings.create(:var => 'test2', :value => 'bar')
7
+ Settings.create!(:var => 'test', :value => 'foo')
8
+ Settings.create!(:var => 'test2', :value => 'bar')
9
+
10
+ # Reset defaults
11
+ Settings.defaults = {}.with_indifferent_access
9
12
  end
10
13
 
11
14
  def teardown
@@ -61,8 +64,8 @@ class SettingsTest < Test::Unit::TestCase
61
64
  end
62
65
 
63
66
  def test_target_scope
64
- user1 = User.create :name => 'First user'
65
- user2 = User.create :name => 'Second user'
67
+ user1 = User.create! :name => 'First user'
68
+ user2 = User.create! :name => 'Second user'
66
69
 
67
70
  assert_assign_setting 1, :one, user1
68
71
  assert_assign_setting 2, :two, user2
@@ -83,8 +86,8 @@ class SettingsTest < Test::Unit::TestCase
83
86
  end
84
87
 
85
88
  def test_named_scope
86
- user_without_settings = User.create :name => 'User without settings'
87
- user_with_settings = User.create :name => 'User with settings'
89
+ user_without_settings = User.create! :name => 'User without settings'
90
+ user_with_settings = User.create! :name => 'User with settings'
88
91
  user_with_settings.settings.one = '1'
89
92
  user_with_settings.settings.two = '2'
90
93
 
@@ -100,8 +103,8 @@ class SettingsTest < Test::Unit::TestCase
100
103
  end
101
104
 
102
105
  def test_delete_settings_after_destroying_target
103
- user1 = User.create :name => 'Mr. Foo'
104
- user2 = User.create :name => 'Mr. Bar'
106
+ user1 = User.create! :name => 'Mr. Foo'
107
+ user2 = User.create! :name => 'Mr. Bar'
105
108
  user1.settings.example = 42
106
109
  user2.settings.example = 43
107
110
 
@@ -155,6 +158,42 @@ class SettingsTest < Test::Unit::TestCase
155
158
  assert_setting(nil, 'test3')
156
159
  end
157
160
 
161
+ def test_class_level_settings
162
+ assert_equal User.settings.name, "ScopedSettings"
163
+ end
164
+
165
+ def test_object_inherits_class_settings_before_default
166
+ Settings.defaults[:foo] = 'global default'
167
+ User.settings.foo = 'model default'
168
+
169
+ user = User.create! :name => 'Dwight'
170
+
171
+ assert_equal user.settings.foo, 'model default'
172
+ assert_equal 'global default', Settings.foo
173
+ end
174
+
175
+ def test_class_inherits_default_settings
176
+ Settings.defaults[:foo] = 'bar'
177
+ assert_equal User.settings.foo, 'bar'
178
+ end
179
+
180
+ def test_sets_settings_with_hash
181
+ user = User.create! :name => 'Mr. Foo'
182
+ user.settings[:one] = '1'
183
+ user.settings[:two] = '2'
184
+ user.settings = { :two => '2a', :three => '3' }
185
+
186
+ assert_equal '1', user.settings[:one] # ensure existing settings remain intact
187
+ assert_equal '2a', user.settings[:two] # ensure settings are properly overwritten
188
+ assert_equal '3', user.settings[:three] # ensure new setting are created
189
+ end
190
+
191
+ def test_all_includes_defaults
192
+ Settings.defaults[:foo] = 'bar'
193
+ user = User.create! :name => 'Mr. Foo'
194
+ assert_equal({ 'foo' => 'bar' }, user.settings.all)
195
+ end
196
+
158
197
  private
159
198
  def assert_setting(value, key, scope_target=nil)
160
199
  key = key.to_sym
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ledermann-rails-settings
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 1.1.0
10
+ version: 1.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Georg Ledermann
@@ -15,12 +15,40 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-06-01 00:00:00 Z
18
+ date: 2012-07-21 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: activerecord
21
+ name: rake
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: sqlite3
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: activerecord
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
24
52
  none: false
25
53
  requirements:
26
54
  - - ">="
@@ -31,11 +59,11 @@ dependencies:
31
59
  - 3
32
60
  version: "2.3"
33
61
  type: :runtime
34
- version_requirements: *id001
62
+ version_requirements: *id003
35
63
  - !ruby/object:Gem::Dependency
36
64
  name: rake
37
65
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
66
+ requirement: &id004 !ruby/object:Gem::Requirement
39
67
  none: false
40
68
  requirements:
41
69
  - - ">="
@@ -45,11 +73,11 @@ dependencies:
45
73
  - 0
46
74
  version: "0"
47
75
  type: :development
48
- version_requirements: *id002
76
+ version_requirements: *id004
49
77
  - !ruby/object:Gem::Dependency
50
78
  name: sqlite3
51
79
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
80
+ requirement: &id005 !ruby/object:Gem::Requirement
53
81
  none: false
54
82
  requirements:
55
83
  - - ">="
@@ -59,7 +87,7 @@ dependencies:
59
87
  - 0
60
88
  version: "0"
61
89
  type: :development
62
- version_requirements: *id003
90
+ version_requirements: *id005
63
91
  description: Ruby Gem that makes managing a table of key/value pairs easy. Think of it like a Hash stored in you database, that uses simple ActiveRecord like methods for manipulation.
64
92
  email:
65
93
  - mail@georg-ledermann.de