ledermann-rails-settings 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +21 -0
- data/README.md +135 -0
- data/Rakefile +11 -0
- data/ci/Gemfile.rails-2.3.x +5 -0
- data/ci/Gemfile.rails-3.0.x +5 -0
- data/ci/Gemfile.rails-3.1.x +5 -0
- data/init.rb +1 -0
- data/lib/rails-settings/active_record.rb +30 -0
- data/lib/rails-settings/scoped_settings.rb +14 -0
- data/lib/rails-settings/settings.rb +116 -0
- data/lib/rails-settings/version.rb +3 -0
- data/lib/rails-settings.rb +4 -0
- data/rails-settings.gemspec +24 -0
- data/test/settings_test.rb +186 -0
- data/test/test_helper.rb +34 -0
- metadata +86 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2006 Alex Wayne
|
2
|
+
Some additional features added 2009-2011 by Georg Ledermann
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOa AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SaALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
# Settings Gem/Plugin for Rails
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/ledermann/rails-settings.png)](http://travis-ci.org/ledermann/rails-settings)
|
4
|
+
|
5
|
+
Settings is a gem/plugin 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. Keep track of any setting that you don't want to hard code into your rails app. You can store any kind of object: Strings, numbers, arrays, or any object which can be noted as YAML.
|
6
|
+
|
7
|
+
|
8
|
+
## Requirements
|
9
|
+
|
10
|
+
ActiveRecord 2.3.x, 3.0.x or 3.1.x
|
11
|
+
|
12
|
+
Tested with Ruby 1.8.7, 1.9.2, 1.9.3 and RBX2.0
|
13
|
+
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Include the gem in your Gemfile
|
18
|
+
|
19
|
+
gem 'ledermann-rails-settings', :require => 'rails-settings'
|
20
|
+
|
21
|
+
or install as a plugin:
|
22
|
+
|
23
|
+
./script/plugin install git://github.com/ledermann/rails-settings.git
|
24
|
+
|
25
|
+
|
26
|
+
You have to create the table used by the Settings model by using this migration:
|
27
|
+
|
28
|
+
class CreateSettingsTable < ActiveRecord::Migration
|
29
|
+
def self.up
|
30
|
+
create_table :settings, :force => true do |t|
|
31
|
+
t.string :var, :null => false
|
32
|
+
t.text :value
|
33
|
+
t.integer :target_id
|
34
|
+
t.string :target_type, :limit => 30
|
35
|
+
t.timestamps
|
36
|
+
end
|
37
|
+
|
38
|
+
add_index :settings, [ :target_type, :target_id, :var ], :unique => true
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.down
|
42
|
+
drop_table :settings
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
Now update your database with:
|
47
|
+
|
48
|
+
rake db:migrate
|
49
|
+
|
50
|
+
## Usage
|
51
|
+
|
52
|
+
The syntax is easy. First, lets create some settings to keep track of:
|
53
|
+
|
54
|
+
Settings.admin_password = 'supersecret'
|
55
|
+
Settings.date_format = '%m %d, %Y'
|
56
|
+
Settings.cocktails = ['Martini', 'Screwdriver', 'White Russian']
|
57
|
+
Settings.foo = 123
|
58
|
+
Settings.credentials = { :username => 'tom', :password => 'secret' }
|
59
|
+
|
60
|
+
Now lets read them back:
|
61
|
+
|
62
|
+
Settings.foo
|
63
|
+
# => 123
|
64
|
+
|
65
|
+
Changing an existing setting is the same as creating a new setting:
|
66
|
+
|
67
|
+
Settings.foo = 'super duper bar'
|
68
|
+
|
69
|
+
For changing an existing setting which is a Hash, you can merge new values with existing ones:
|
70
|
+
|
71
|
+
Settings.merge! :credentials, :password => 'topsecret'
|
72
|
+
Settings.credentials
|
73
|
+
# => { :username => 'tom', :password => 'topsecret' }
|
74
|
+
|
75
|
+
Decide you dont want to track a particular setting anymore?
|
76
|
+
|
77
|
+
Settings.destroy :foo
|
78
|
+
Settings.foo
|
79
|
+
# => nil
|
80
|
+
|
81
|
+
Want a list of all the settings?
|
82
|
+
|
83
|
+
Settings.all
|
84
|
+
# => { 'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y' }
|
85
|
+
|
86
|
+
You need name spaces and want a list of settings for a give name space? Just choose your prefered named space delimiter and use Settings.all like this:
|
87
|
+
|
88
|
+
Settings['preferences.color'] = :blue
|
89
|
+
Settings['preferences.size'] = :large
|
90
|
+
Settings['license.key'] = 'ABC-DEF'
|
91
|
+
Settings.all('preferences.')
|
92
|
+
# => { 'preferences.color' => :blue, 'preferences.size' => :large }
|
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
|
+
Settings may be bound to any existing ActiveRecord object. Define this association like this:
|
106
|
+
|
107
|
+
class User < ActiveRecord::Base
|
108
|
+
has_settings
|
109
|
+
end
|
110
|
+
|
111
|
+
Then you can set/get a setting for a given user instance just by doing this:
|
112
|
+
|
113
|
+
user = User.find(123)
|
114
|
+
user.settings.color = :red
|
115
|
+
user.settings.color
|
116
|
+
# => :red
|
117
|
+
|
118
|
+
user.settings.all
|
119
|
+
# => { "color" => :red }
|
120
|
+
|
121
|
+
I you want to find users having or not having some settings, there are named scopes for this:
|
122
|
+
|
123
|
+
User.with_settings
|
124
|
+
# returns a scope of users having any setting
|
125
|
+
|
126
|
+
User.with_settings_for('color')
|
127
|
+
# returns a scope of users having a 'color' setting
|
128
|
+
|
129
|
+
User.without_settings
|
130
|
+
# returns a scope of users having no setting at all (means user.settings.all == {})
|
131
|
+
|
132
|
+
User.without_settings('color')
|
133
|
+
# returns a scope of users having no 'color' setting (means user.settings.color == nil)
|
134
|
+
|
135
|
+
That's all there is to it! Enjoy!
|
data/Rakefile
ADDED
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rails-settings'
|
@@ -0,0 +1,30 @@
|
|
1
|
+
ActiveRecord::Base.class_eval do
|
2
|
+
def self.has_settings
|
3
|
+
class_eval do
|
4
|
+
def settings
|
5
|
+
ScopedSettings.for_target(self)
|
6
|
+
end
|
7
|
+
|
8
|
+
after_destroy { |user| user.settings.target_scoped.delete_all }
|
9
|
+
|
10
|
+
scope_method = ActiveRecord::VERSION::MAJOR < 3 ? :named_scope : :scope
|
11
|
+
|
12
|
+
send scope_method, :with_settings, :joins => "JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
|
13
|
+
settings.target_type = '#{self.base_class.name}')",
|
14
|
+
:select => "DISTINCT #{self.table_name}.*"
|
15
|
+
|
16
|
+
send scope_method, :with_settings_for, lambda { |var| { :joins => "JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
|
17
|
+
settings.target_type = '#{self.base_class.name}') AND
|
18
|
+
settings.var = '#{var}'" } }
|
19
|
+
|
20
|
+
send scope_method, :without_settings, :joins => "LEFT JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
|
21
|
+
settings.target_type = '#{self.base_class.name}')",
|
22
|
+
:conditions => 'settings.id IS NULL'
|
23
|
+
|
24
|
+
send scope_method, :without_settings_for, lambda { |var| { :joins => "LEFT JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
|
25
|
+
settings.target_type = '#{self.base_class.name}') AND
|
26
|
+
settings.var = '#{var}'",
|
27
|
+
:conditions => 'settings.id IS NULL' } }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
class Settings < ActiveRecord::Base
|
2
|
+
class SettingNotFound < RuntimeError; end
|
3
|
+
|
4
|
+
cattr_accessor :defaults
|
5
|
+
@@defaults = {}.with_indifferent_access
|
6
|
+
|
7
|
+
# Support old plugin
|
8
|
+
if defined?(SettingsDefaults::DEFAULTS)
|
9
|
+
@@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
|
10
|
+
end
|
11
|
+
|
12
|
+
#get or set a variable with the variable as the called method
|
13
|
+
def self.method_missing(method, *args)
|
14
|
+
if self.respond_to?(method)
|
15
|
+
super
|
16
|
+
else
|
17
|
+
method_name = method.to_s
|
18
|
+
|
19
|
+
#set a value for a variable
|
20
|
+
if method_name =~ /=$/
|
21
|
+
var_name = method_name.gsub('=', '')
|
22
|
+
value = args.first
|
23
|
+
self[var_name] = value
|
24
|
+
|
25
|
+
#retrieve a value
|
26
|
+
else
|
27
|
+
self[method_name]
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
#destroy the specified settings record
|
34
|
+
def self.destroy(var_name)
|
35
|
+
var_name = var_name.to_s
|
36
|
+
begin
|
37
|
+
target(var_name).destroy
|
38
|
+
true
|
39
|
+
rescue NoMethodError
|
40
|
+
raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
#retrieve all settings as a hash (optionally starting with a given namespace)
|
45
|
+
def self.all(starting_with=nil)
|
46
|
+
options = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {}
|
47
|
+
vars = target_scoped.find(:all, {:select => 'var, value'}.merge(options))
|
48
|
+
|
49
|
+
result = {}
|
50
|
+
vars.each do |record|
|
51
|
+
result[record.var] = record.value
|
52
|
+
end
|
53
|
+
result.with_indifferent_access
|
54
|
+
end
|
55
|
+
|
56
|
+
#get a setting value by [] notation
|
57
|
+
def self.[](var_name)
|
58
|
+
if var = target(var_name)
|
59
|
+
var.value
|
60
|
+
else
|
61
|
+
@@defaults[var_name.to_s]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
#set a setting value by [] notation
|
66
|
+
def self.[]=(var_name, value)
|
67
|
+
record = target_scoped.find_or_initialize_by_var(var_name.to_s)
|
68
|
+
record.value = value
|
69
|
+
record.save!
|
70
|
+
|
71
|
+
value
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.merge!(var_name, hash_value)
|
75
|
+
raise ArgumentError unless hash_value.is_a?(Hash)
|
76
|
+
|
77
|
+
old_value = self[var_name] || {}
|
78
|
+
raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
|
79
|
+
|
80
|
+
new_value = old_value.merge(hash_value)
|
81
|
+
self[var_name] = new_value if new_value != old_value
|
82
|
+
|
83
|
+
new_value
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.target(var_name)
|
87
|
+
target_scoped.find_by_var(var_name.to_s)
|
88
|
+
end
|
89
|
+
|
90
|
+
#get the value field, YAML decoded
|
91
|
+
def value
|
92
|
+
YAML::load(self[:value])
|
93
|
+
end
|
94
|
+
|
95
|
+
#set the value field, YAML encoded
|
96
|
+
def value=(new_value)
|
97
|
+
self[:value] = new_value.to_yaml
|
98
|
+
end
|
99
|
+
|
100
|
+
def self.target_scoped
|
101
|
+
Settings.scoped_by_target_type_and_target_id(target_type, target_id)
|
102
|
+
end
|
103
|
+
|
104
|
+
#Deprecated!
|
105
|
+
def self.reload # :nodoc:
|
106
|
+
self
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.target_id
|
110
|
+
nil
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.target_type
|
114
|
+
nil
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'rails-settings/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'ledermann-rails-settings'
|
7
|
+
s.version = RailsSettings::VERSION
|
8
|
+
s.authors = ['Georg Ledermann']
|
9
|
+
s.email = ['mail@georg-ledermann.de']
|
10
|
+
s.homepage = 'https://github.com/ledermann/rails-settings'
|
11
|
+
s.summary = %q{Settings management for ActiveRecord objects}
|
12
|
+
s.description = %q{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.}
|
13
|
+
|
14
|
+
s.rubyforge_project = 'ledermann-rails-settings'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ['lib']
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
@@ -0,0 +1,186 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SettingsTest < Test::Unit::TestCase
|
4
|
+
setup_db
|
5
|
+
|
6
|
+
def setup
|
7
|
+
Settings.create(:var => 'test', :value => 'foo')
|
8
|
+
Settings.create(:var => 'test2', :value => 'bar')
|
9
|
+
end
|
10
|
+
|
11
|
+
def teardown
|
12
|
+
Settings.delete_all
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_defaults
|
16
|
+
Settings.defaults[:foo] = 'default foo'
|
17
|
+
|
18
|
+
assert_nil Settings.target(:foo)
|
19
|
+
assert_equal 'default foo', Settings.foo
|
20
|
+
|
21
|
+
Settings.foo = 'bar'
|
22
|
+
assert_equal 'bar', Settings.foo
|
23
|
+
assert_not_nil Settings.target(:foo)
|
24
|
+
end
|
25
|
+
|
26
|
+
def tests_defaults_false
|
27
|
+
Settings.defaults[:foo] = false
|
28
|
+
assert_equal false, Settings.foo
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_get
|
32
|
+
assert_setting 'foo', :test
|
33
|
+
assert_setting 'bar', :test2
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_update
|
37
|
+
assert_assign_setting '321', :test
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_create
|
41
|
+
assert_assign_setting '123', :onetwothree
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_complex_serialization
|
45
|
+
complex = [1, '2', {:three => true}]
|
46
|
+
Settings.complex = complex
|
47
|
+
assert_equal complex, Settings.complex
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_serialization_of_float
|
51
|
+
Settings.float = 0.01
|
52
|
+
Settings.reload
|
53
|
+
assert_equal 0.01, Settings.float
|
54
|
+
assert_equal 0.02, Settings.float * 2
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_target_scope
|
58
|
+
user1 = User.create :name => 'First user'
|
59
|
+
user2 = User.create :name => 'Second user'
|
60
|
+
|
61
|
+
assert_assign_setting 1, :one, user1
|
62
|
+
assert_assign_setting 2, :two, user2
|
63
|
+
|
64
|
+
assert_setting 1, :one, user1
|
65
|
+
assert_setting 2, :two, user2
|
66
|
+
|
67
|
+
assert_setting nil, :one
|
68
|
+
assert_setting nil, :two
|
69
|
+
|
70
|
+
assert_setting nil, :two, user1
|
71
|
+
assert_setting nil, :one, user2
|
72
|
+
|
73
|
+
assert_equal({ "one" => 1}, user1.settings.all('one'))
|
74
|
+
assert_equal({ "two" => 2}, user2.settings.all('two'))
|
75
|
+
assert_equal({ "one" => 1}, user1.settings.all('o'))
|
76
|
+
assert_equal({}, user1.settings.all('non_existing_var'))
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_named_scope
|
80
|
+
user_without_settings = User.create :name => 'User without settings'
|
81
|
+
user_with_settings = User.create :name => 'User with settings'
|
82
|
+
user_with_settings.settings.one = '1'
|
83
|
+
user_with_settings.settings.two = '2'
|
84
|
+
|
85
|
+
assert_equal [user_with_settings], User.with_settings
|
86
|
+
assert_equal [user_with_settings], User.with_settings_for('one')
|
87
|
+
assert_equal [user_with_settings], User.with_settings_for('two')
|
88
|
+
assert_equal [], User.with_settings_for('foo')
|
89
|
+
|
90
|
+
assert_equal [user_without_settings], User.without_settings
|
91
|
+
assert_equal [user_without_settings], User.without_settings_for('one')
|
92
|
+
assert_equal [user_without_settings], User.without_settings_for('two')
|
93
|
+
assert_equal [user_without_settings, user_with_settings], User.without_settings_for('foo')
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_delete_settings_after_destroying_target
|
97
|
+
user1 = User.create :name => 'Mr. Foo'
|
98
|
+
user2 = User.create :name => 'Mr. Bar'
|
99
|
+
user1.settings.example = 42
|
100
|
+
user2.settings.example = 43
|
101
|
+
|
102
|
+
before_count = Settings.count
|
103
|
+
user1.destroy
|
104
|
+
assert_equal before_count - 1, Settings.count
|
105
|
+
|
106
|
+
before_count = Settings.count
|
107
|
+
user2.destroy
|
108
|
+
assert_equal before_count - 1, Settings.count
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_all
|
112
|
+
assert_equal({ "test2" => "bar", "test" => "foo" }, Settings.all)
|
113
|
+
assert_equal({ "test2" => "bar" }, Settings.all('test2'))
|
114
|
+
assert_equal({ "test2" => "bar", "test" => "foo" }, Settings.all('test'))
|
115
|
+
assert_equal({}, Settings.all('non_existing_var'))
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_merge
|
119
|
+
assert_raise(TypeError) do
|
120
|
+
Settings.merge! :test, { :a => 1 }
|
121
|
+
end
|
122
|
+
|
123
|
+
Settings[:hash] = { :one => 1 }
|
124
|
+
Settings.merge! :hash, { :two => 2 }
|
125
|
+
assert_equal({ :one => 1, :two => 2 }, Settings[:hash])
|
126
|
+
|
127
|
+
assert_raise(ArgumentError) do
|
128
|
+
Settings.merge! :hash, 123
|
129
|
+
end
|
130
|
+
|
131
|
+
Settings.merge! :empty_hash, { :two => 2 }
|
132
|
+
assert_equal({ :two => 2 }, Settings[:empty_hash])
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_destroy
|
136
|
+
Settings.destroy :test
|
137
|
+
assert_equal nil, Settings.test
|
138
|
+
|
139
|
+
assert_raise(Settings::SettingNotFound) do
|
140
|
+
Settings.destroy :unknown
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_false
|
145
|
+
Settings.test3 = false
|
146
|
+
assert_setting(false, 'test3')
|
147
|
+
|
148
|
+
Settings.destroy :test3
|
149
|
+
assert_setting(nil, 'test3')
|
150
|
+
end
|
151
|
+
|
152
|
+
private
|
153
|
+
def assert_setting(value, key, scope_target=nil)
|
154
|
+
key = key.to_sym
|
155
|
+
|
156
|
+
if scope_target
|
157
|
+
assert_equal value, scope_target.instance_eval("settings.#{key}")
|
158
|
+
assert_equal value, scope_target.settings[key.to_sym]
|
159
|
+
assert_equal value, scope_target.settings[key.to_s]
|
160
|
+
else
|
161
|
+
assert_equal value, eval("Settings.#{key}")
|
162
|
+
assert_equal value, Settings[key.to_sym]
|
163
|
+
assert_equal value, Settings[key.to_s]
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def assert_assign_setting(value, key, scope_target=nil)
|
168
|
+
key = key.to_sym
|
169
|
+
|
170
|
+
if scope_target
|
171
|
+
assert_equal value, (scope_target.settings[key] = value)
|
172
|
+
assert_setting value, key, scope_target
|
173
|
+
scope_target.settings[key] = nil
|
174
|
+
|
175
|
+
assert_equal value, (scope_target.settings[key.to_s] = value)
|
176
|
+
assert_setting value, key, scope_target
|
177
|
+
else
|
178
|
+
assert_equal value, (Settings[key] = value)
|
179
|
+
assert_setting value, key
|
180
|
+
Settings[key] = nil
|
181
|
+
|
182
|
+
assert_equal value, (Settings[key.to_s] = value)
|
183
|
+
assert_setting value, key
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/test_case'
|
5
|
+
require 'active_record'
|
6
|
+
require 'test/unit'
|
7
|
+
|
8
|
+
require "#{File.dirname(__FILE__)}/../init"
|
9
|
+
|
10
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
11
|
+
ActiveRecord::Migration.verbose = false
|
12
|
+
|
13
|
+
class User < ActiveRecord::Base
|
14
|
+
has_settings
|
15
|
+
end
|
16
|
+
|
17
|
+
def setup_db
|
18
|
+
ActiveRecord::Schema.define(:version => 1) do
|
19
|
+
create_table :settings do |t|
|
20
|
+
t.string :var, :null => false
|
21
|
+
t.text :value, :null => true
|
22
|
+
t.integer :target_id, :null => true
|
23
|
+
t.string :target_type, :limit => 30, :null => true
|
24
|
+
t.timestamps
|
25
|
+
end
|
26
|
+
add_index :settings, [ :target_type, :target_id, :var ], :unique => true
|
27
|
+
|
28
|
+
create_table :users do |t|
|
29
|
+
t.string :name
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
puts "Testing with ActiveRecord #{ActiveRecord::VERSION::STRING}"
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ledermann-rails-settings
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Georg Ledermann
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-11-05 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
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.
|
23
|
+
email:
|
24
|
+
- mail@georg-ledermann.de
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- .travis.yml
|
34
|
+
- Gemfile
|
35
|
+
- MIT-LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- ci/Gemfile.rails-2.3.x
|
39
|
+
- ci/Gemfile.rails-3.0.x
|
40
|
+
- ci/Gemfile.rails-3.1.x
|
41
|
+
- init.rb
|
42
|
+
- lib/rails-settings.rb
|
43
|
+
- lib/rails-settings/active_record.rb
|
44
|
+
- lib/rails-settings/scoped_settings.rb
|
45
|
+
- lib/rails-settings/settings.rb
|
46
|
+
- lib/rails-settings/version.rb
|
47
|
+
- rails-settings.gemspec
|
48
|
+
- test/settings_test.rb
|
49
|
+
- test/test_helper.rb
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: https://github.com/ledermann/rails-settings
|
52
|
+
licenses: []
|
53
|
+
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
requirements: []
|
78
|
+
|
79
|
+
rubyforge_project: ledermann-rails-settings
|
80
|
+
rubygems_version: 1.6.2
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: Settings management for ActiveRecord objects
|
84
|
+
test_files:
|
85
|
+
- test/settings_test.rb
|
86
|
+
- test/test_helper.rb
|