rails3-settings 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +13 -0
- data/Gemfile.lock +36 -0
- data/MIT-LICENSE +22 -0
- data/README.rdoc +112 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/init.rb +28 -0
- data/lib/generators/settings_migration/USAGE +14 -0
- data/lib/generators/settings_migration/settings_migration_generator.rb +24 -0
- data/lib/generators/settings_migration/templates/migration.rb +17 -0
- data/lib/settings.rb +131 -0
- data/rails3-settings.gemspec +77 -0
- data/test/helper.rb +30 -0
- data/test/settings_test.rb +159 -0
- metadata +210 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.0.5)
|
5
|
+
activesupport (= 3.0.5)
|
6
|
+
builder (~> 2.1.2)
|
7
|
+
i18n (~> 0.4)
|
8
|
+
activerecord (3.0.5)
|
9
|
+
activemodel (= 3.0.5)
|
10
|
+
activesupport (= 3.0.5)
|
11
|
+
arel (~> 2.0.2)
|
12
|
+
tzinfo (~> 0.3.23)
|
13
|
+
activesupport (3.0.5)
|
14
|
+
arel (2.0.9)
|
15
|
+
builder (2.1.2)
|
16
|
+
git (1.2.5)
|
17
|
+
i18n (0.5.0)
|
18
|
+
jeweler (1.5.2)
|
19
|
+
bundler (~> 1.0.0)
|
20
|
+
git (>= 1.2.5)
|
21
|
+
rake
|
22
|
+
rake (0.8.7)
|
23
|
+
sqlite3 (1.3.3)
|
24
|
+
sqlite3-ruby (1.3.3)
|
25
|
+
sqlite3 (>= 1.3.3)
|
26
|
+
tzinfo (0.3.25)
|
27
|
+
|
28
|
+
PLATFORMS
|
29
|
+
ruby
|
30
|
+
|
31
|
+
DEPENDENCIES
|
32
|
+
activerecord (~> 3.0.5)
|
33
|
+
activesupport (~> 3.0.5)
|
34
|
+
bundler (~> 1.0.10)
|
35
|
+
jeweler (~> 1.5.2)
|
36
|
+
sqlite3-ruby
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2006 Alex Wayne
|
2
|
+
Some additional features added 2009 by Georg Ledermann
|
3
|
+
Rails 3 adaptation added 2011 by Mikhail Khomutetskiy
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOa AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SaALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
= Settings Plugin
|
2
|
+
|
3
|
+
Settings is a plugin that makes managing a table of global key, value pairs easy.
|
4
|
+
Think of it like a global Hash stored in you database, that uses simple ActiveRecord
|
5
|
+
like methods for manipulation. Keep track of any global setting that you dont want
|
6
|
+
to hard code into your rails app. You can store any kind of object. Strings, numbers,
|
7
|
+
arrays, or any object.
|
8
|
+
|
9
|
+
|
10
|
+
== Setup
|
11
|
+
|
12
|
+
Requirements:
|
13
|
+
Rails 3.0.x
|
14
|
+
|
15
|
+
You can add this gem in your Gemfile
|
16
|
+
gem 'rails3-settings', :require => 'settings', :git => 'git://github.com/Magicdream/rails-settings.git'
|
17
|
+
|
18
|
+
You must create the table used by the Settings model. Simply run this command:
|
19
|
+
rails generate settings_migration
|
20
|
+
|
21
|
+
Now just put that migration in the database with:
|
22
|
+
rake db:migrate
|
23
|
+
|
24
|
+
If you use some older version of this fork, beware that the database schema needs to
|
25
|
+
be changed ("object_id" renamed to "target_id", "object_type" renamed to "target_type").
|
26
|
+
You can do this with this migration:
|
27
|
+
|
28
|
+
class RenameSettingsFields < ActiveRecord::Migration
|
29
|
+
def self.up
|
30
|
+
remove_index :settings, [ :object_type, :object_id, :var ]
|
31
|
+
|
32
|
+
rename_column :settings, :object_id, :target_id
|
33
|
+
rename_column :settings, :object_type, :target_type
|
34
|
+
|
35
|
+
add_index :settings, [ :target_type, :target_id, :var ], :unique => true
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.down
|
39
|
+
# ...
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
== Usage
|
44
|
+
|
45
|
+
The syntax is easy. First, lets create some settings to keep track of:
|
46
|
+
|
47
|
+
Settings.admin_password = 'supersecret'
|
48
|
+
Settings.date_format = '%m %d, %Y'
|
49
|
+
Settings.cocktails = ['Martini', 'Screwdriver', 'White Russian']
|
50
|
+
Settings.foo = 123
|
51
|
+
Settings.credentials = { :username => 'tom', :password => 'secret' }
|
52
|
+
|
53
|
+
Now lets read them back:
|
54
|
+
|
55
|
+
Settings.foo # returns 123
|
56
|
+
|
57
|
+
Changing an existing setting is the same as creating a new setting:
|
58
|
+
|
59
|
+
Settings.foo = 'super duper bar'
|
60
|
+
|
61
|
+
For changing an existing setting which is a Hash, you can merge new values with existing ones:
|
62
|
+
Settings.merge! :credentials, :password => 'topsecret'
|
63
|
+
Settings.credentials # returns { :username => 'tom', :password => 'topsecret' }
|
64
|
+
|
65
|
+
Decide you dont want to track a particular setting anymore?
|
66
|
+
|
67
|
+
Settings.destroy :foo
|
68
|
+
Settings.foo # returns nil
|
69
|
+
|
70
|
+
Want a list of all the settings?
|
71
|
+
|
72
|
+
Settings.all # returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}
|
73
|
+
|
74
|
+
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:
|
75
|
+
|
76
|
+
Settings['preferences.color'] = :blue
|
77
|
+
Settings['preferences.size'] = :large
|
78
|
+
Settings['license.key'] = 'ABC-DEF'
|
79
|
+
Settings.all('preferences.') # returns { 'preferences.color' => :blue, 'preferences.size' => :large }
|
80
|
+
|
81
|
+
Set defaults for certain settings of your app. This will cause the defined settings to return with the
|
82
|
+
Specified value even if they are not in the database. Make a new file in config/initializers/settings.rb
|
83
|
+
with the following:
|
84
|
+
|
85
|
+
Settings.defaults[:some_setting] = 'footastic'
|
86
|
+
|
87
|
+
Now even if the database is completely empty, you app will have some intelligent defaults:
|
88
|
+
|
89
|
+
Settings.some_setting # returns 'footastic'
|
90
|
+
|
91
|
+
Settings may be bound to any existing ActiveRecord object. Define this association like this:
|
92
|
+
|
93
|
+
class User < ActiveRecord::Base
|
94
|
+
has_settings
|
95
|
+
end
|
96
|
+
|
97
|
+
Then you can set/get a setting for a given user instance just by doing this:
|
98
|
+
|
99
|
+
user = User.find(123)
|
100
|
+
user.settings.color = :red
|
101
|
+
user.settings.color # returns :red
|
102
|
+
user.settings.all # { "color" => :red }
|
103
|
+
|
104
|
+
I you want to find users having or not having some settings, there are named scopes for this:
|
105
|
+
|
106
|
+
User.with_settings # => returns a scope of users having any setting
|
107
|
+
User.with_settings_for('color') # => returns a scope of users having a 'color' setting
|
108
|
+
|
109
|
+
User.without_settings # returns a scope of users having no setting at all (means user.settings.all == {})
|
110
|
+
User.without_settings('color') # returns a scope of users having no 'color' setting (means user.settings.color == nil)
|
111
|
+
|
112
|
+
That's all there is to it! Enjoy!
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
gem.name = "rails3-settings"
|
15
|
+
gem.version = IO.read(File.join(File.dirname(__FILE__), 'VERSION'))
|
16
|
+
gem.homepage = "http://github.com/Magicdream/rails-settings"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Store application settings or user-settings in DB like key-value pairs}
|
19
|
+
gem.description = %Q{Settings is a plugin that makes managing a table of global key, value pairs easy. Think of it like a global Hash stored in you database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global setting that you dont want to hard code into your rails app. You can store any kind of object. Strings, numbers, arrays, or any object.}
|
20
|
+
gem.email = "khomutetskiy@gmail.com"
|
21
|
+
gem.authors = ["Squeegy", "Georg Ledermann ", "Mikhail Khomutetskiy"]
|
22
|
+
|
23
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
24
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
25
|
+
gem.add_runtime_dependency 'activerecord', '~> 3.0.5'
|
26
|
+
gem.add_runtime_dependency 'activesupport', '~> 3.0.5'
|
27
|
+
|
28
|
+
gem.add_development_dependency 'bundler', '~> 1.0.10'
|
29
|
+
gem.add_development_dependency 'jeweler', '~> 1.5.2'
|
30
|
+
end
|
31
|
+
Jeweler::RubygemsDotOrgTasks.new
|
32
|
+
|
33
|
+
require 'rake/testtask'
|
34
|
+
Rake::TestTask.new(:test) do |test|
|
35
|
+
test.libs << 'lib' << 'test'
|
36
|
+
test.pattern = 'test/**/*_test.rb'
|
37
|
+
test.verbose = true
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :test
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "rails-settings #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/init.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'settings'
|
2
|
+
|
3
|
+
ActiveRecord::Base.class_eval do
|
4
|
+
def self.has_settings
|
5
|
+
class_eval do
|
6
|
+
def settings
|
7
|
+
ScopedSettings.for_target(self)
|
8
|
+
end
|
9
|
+
|
10
|
+
scope :with_settings, :joins => "JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
|
11
|
+
settings.target_type = '#{self.base_class.name}')",
|
12
|
+
:select => "DISTINCT #{self.table_name}.*"
|
13
|
+
|
14
|
+
scope :with_settings_for, lambda { |var| { :joins => "JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
|
15
|
+
settings.target_type = '#{self.base_class.name}') AND
|
16
|
+
settings.var = '#{var}'" } }
|
17
|
+
|
18
|
+
scope :without_settings, :joins => "LEFT JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
|
19
|
+
settings.target_type = '#{self.base_class.name}')",
|
20
|
+
:conditions => 'settings.id IS NULL'
|
21
|
+
|
22
|
+
scope :without_settings_for, lambda { |var| { :joins => "LEFT JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
|
23
|
+
settings.target_type = '#{self.base_class.name}') AND
|
24
|
+
settings.var = '#{var}'",
|
25
|
+
:conditions => 'settings.id IS NULL' } }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Description:
|
2
|
+
The settings migration generator creates a migration for the settings plugin.
|
3
|
+
|
4
|
+
The generator takes a migration name as its argument. The migration name may be
|
5
|
+
given in CamelCase or under_score. 'create_settings' is the default.
|
6
|
+
|
7
|
+
The generator creates a migration class in db/migrate prefixed by its number
|
8
|
+
in the queue.
|
9
|
+
|
10
|
+
Example:
|
11
|
+
rails generate settings_migration
|
12
|
+
|
13
|
+
With 4 existing migrations, this will create an CreateSettings migration in the
|
14
|
+
file db/migrate/005_create_settings.rb
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
class SettingsMigrationGenerator < Rails::Generators::Base
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
|
7
|
+
source_root File.join(File.dirname(__FILE__), 'templates')
|
8
|
+
|
9
|
+
argument :migration_name, :type => :string, :default => "CreateSettings"
|
10
|
+
|
11
|
+
def self.next_migration_number(dirname)
|
12
|
+
next_migration_number = current_migration_number(dirname) + 1
|
13
|
+
if ActiveRecord::Base.timestamped_migrations
|
14
|
+
[Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
|
15
|
+
else
|
16
|
+
"%.3d" % next_migration_number
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def copy_migration
|
21
|
+
migration_template "migration.rb", "db/migrate/#{migration_name.underscore}.rb"
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class <%= migration_name %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :settings, :force => true do |t|
|
4
|
+
t.string :var, :null => false
|
5
|
+
t.text :value, :null => true
|
6
|
+
t.integer :target_id, :null => true
|
7
|
+
t.string :target_type, :limit => 30, :null => true
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
|
11
|
+
add_index :settings, [ :target_type, :target_id, :var ], :unique => true
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.down
|
15
|
+
drop_table :settings
|
16
|
+
end
|
17
|
+
end
|
data/lib/settings.rb
ADDED
@@ -0,0 +1,131 @@
|
|
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
|
+
method_name = method.to_s
|
15
|
+
super(method, *args)
|
16
|
+
|
17
|
+
rescue NoMethodError
|
18
|
+
#set a value for a variable
|
19
|
+
if method_name =~ /=$/
|
20
|
+
var_name = method_name.gsub('=', '')
|
21
|
+
value = args.first
|
22
|
+
self[var_name] = value
|
23
|
+
|
24
|
+
#retrieve a value
|
25
|
+
else
|
26
|
+
self[method_name]
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
#destroy the specified settings record
|
32
|
+
def self.destroy(var_name)
|
33
|
+
var_name = var_name.to_s
|
34
|
+
if self[var_name]
|
35
|
+
target(var_name).destroy
|
36
|
+
true
|
37
|
+
else
|
38
|
+
raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
#retrieve all settings as a hash (optionally starting with a given namespace)
|
43
|
+
def self.all(starting_with=nil)
|
44
|
+
options = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {}
|
45
|
+
vars = target_scoped.find(:all, {:select => 'var, value'}.merge(options))
|
46
|
+
|
47
|
+
result = {}
|
48
|
+
vars.each do |record|
|
49
|
+
result[record.var] = record.value
|
50
|
+
end
|
51
|
+
result.with_indifferent_access
|
52
|
+
end
|
53
|
+
|
54
|
+
#get a setting value by [] notation
|
55
|
+
def self.[](var_name)
|
56
|
+
if var = target(var_name)
|
57
|
+
var.value
|
58
|
+
else
|
59
|
+
@@defaults[var_name.to_s]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
#set a setting value by [] notation
|
64
|
+
def self.[]=(var_name, value)
|
65
|
+
var_name = var_name.to_s
|
66
|
+
|
67
|
+
record = target(var_name) || target_scoped.new(:var => var_name)
|
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
|
117
|
+
|
118
|
+
class ScopedSettings < Settings
|
119
|
+
def self.for_target(target)
|
120
|
+
@target = target
|
121
|
+
self
|
122
|
+
end
|
123
|
+
|
124
|
+
def self.target_id
|
125
|
+
@target.id
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.target_type
|
129
|
+
@target.class.base_class.to_s
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rails3-settings}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Squeegy", "Georg Ledermann ", "Mikhail Khomutetskiy"]
|
12
|
+
s.date = %q{2011-04-02}
|
13
|
+
s.description = %q{Settings is a plugin that makes managing a table of global key, value pairs easy. Think of it like a global Hash stored in you database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global setting that you dont want to hard code into your rails app. You can store any kind of object. Strings, numbers, arrays, or any object.}
|
14
|
+
s.email = %q{khomutetskiy@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"Gemfile",
|
20
|
+
"Gemfile.lock",
|
21
|
+
"MIT-LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"init.rb",
|
26
|
+
"lib/generators/settings_migration/USAGE",
|
27
|
+
"lib/generators/settings_migration/settings_migration_generator.rb",
|
28
|
+
"lib/generators/settings_migration/templates/migration.rb",
|
29
|
+
"lib/settings.rb",
|
30
|
+
"rails3-settings.gemspec",
|
31
|
+
"test/helper.rb",
|
32
|
+
"test/settings_test.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/Magicdream/rails-settings}
|
35
|
+
s.licenses = ["MIT"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.6.2}
|
38
|
+
s.summary = %q{Store application settings or user-settings in DB like key-value pairs}
|
39
|
+
s.test_files = [
|
40
|
+
"test/helper.rb",
|
41
|
+
"test/settings_test.rb"
|
42
|
+
]
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_runtime_dependency(%q<activerecord>, ["~> 3.0.5"])
|
49
|
+
s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.5"])
|
50
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.10"])
|
51
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
52
|
+
s.add_runtime_dependency(%q<activerecord>, ["~> 3.0.5"])
|
53
|
+
s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.5"])
|
54
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.10"])
|
55
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<activerecord>, ["~> 3.0.5"])
|
58
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0.5"])
|
59
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.10"])
|
60
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
61
|
+
s.add_dependency(%q<activerecord>, ["~> 3.0.5"])
|
62
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0.5"])
|
63
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.10"])
|
64
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
65
|
+
end
|
66
|
+
else
|
67
|
+
s.add_dependency(%q<activerecord>, ["~> 3.0.5"])
|
68
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0.5"])
|
69
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.10"])
|
70
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
71
|
+
s.add_dependency(%q<activerecord>, ["~> 3.0.5"])
|
72
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0.5"])
|
73
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.10"])
|
74
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_support/test_case'
|
4
|
+
require 'active_record'
|
5
|
+
require 'test/unit'
|
6
|
+
|
7
|
+
require "#{File.dirname(__FILE__)}/../init"
|
8
|
+
|
9
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
10
|
+
|
11
|
+
class User < ActiveRecord::Base
|
12
|
+
has_settings
|
13
|
+
end
|
14
|
+
|
15
|
+
def setup_db
|
16
|
+
ActiveRecord::Schema.define(:version => 1) do
|
17
|
+
create_table :settings do |t|
|
18
|
+
t.string :var, :null => false
|
19
|
+
t.text :value, :null => true
|
20
|
+
t.integer :target_id, :null => true
|
21
|
+
t.string :target_type, :limit => 30, :null => true
|
22
|
+
t.timestamps
|
23
|
+
end
|
24
|
+
add_index :settings, [ :target_type, :target_id, :var ], :unique => true
|
25
|
+
|
26
|
+
create_table :users do |t|
|
27
|
+
t.string :name
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,159 @@
|
|
1
|
+
require '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_all
|
97
|
+
assert_equal({ "test2" => "bar", "test" => "foo" }, Settings.all)
|
98
|
+
assert_equal({ "test2" => "bar" }, Settings.all('test2'))
|
99
|
+
assert_equal({ "test2" => "bar", "test" => "foo" }, Settings.all('test'))
|
100
|
+
assert_equal({}, Settings.all('non_existing_var'))
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_merge
|
104
|
+
assert_raise(TypeError) do
|
105
|
+
Settings.merge! :test, { :a => 1 }
|
106
|
+
end
|
107
|
+
|
108
|
+
Settings[:hash] = { :one => 1 }
|
109
|
+
Settings.merge! :hash, { :two => 2 }
|
110
|
+
assert_equal({ :one => 1, :two => 2 }, Settings[:hash])
|
111
|
+
|
112
|
+
assert_raise(ArgumentError) do
|
113
|
+
Settings.merge! :hash, 123
|
114
|
+
end
|
115
|
+
|
116
|
+
Settings.merge! :empty_hash, { :two => 2 }
|
117
|
+
assert_equal({ :two => 2 }, Settings[:empty_hash])
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_destroy
|
121
|
+
Settings.destroy :test
|
122
|
+
assert_equal nil, Settings.test
|
123
|
+
end
|
124
|
+
|
125
|
+
private
|
126
|
+
def assert_setting(value, key, scope_target=nil)
|
127
|
+
key = key.to_sym
|
128
|
+
|
129
|
+
if scope_target
|
130
|
+
assert_equal value, scope_target.instance_eval("settings.#{key}")
|
131
|
+
assert_equal value, scope_target.settings[key.to_sym]
|
132
|
+
assert_equal value, scope_target.settings[key.to_s]
|
133
|
+
else
|
134
|
+
assert_equal value, eval("Settings.#{key}")
|
135
|
+
assert_equal value, Settings[key.to_sym]
|
136
|
+
assert_equal value, Settings[key.to_s]
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def assert_assign_setting(value, key, scope_target=nil)
|
141
|
+
key = key.to_sym
|
142
|
+
|
143
|
+
if scope_target
|
144
|
+
assert_equal value, (scope_target.settings[key] = value)
|
145
|
+
assert_setting value, key, scope_target
|
146
|
+
scope_target.settings[key] = nil
|
147
|
+
|
148
|
+
assert_equal value, (scope_target.settings[key.to_s] = value)
|
149
|
+
assert_setting value, key, scope_target
|
150
|
+
else
|
151
|
+
assert_equal value, (Settings[key] = value)
|
152
|
+
assert_setting value, key
|
153
|
+
Settings[key] = nil
|
154
|
+
|
155
|
+
assert_equal value, (Settings[key.to_s] = value)
|
156
|
+
assert_setting value, key
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
metadata
ADDED
@@ -0,0 +1,210 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails3-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
|
+
- Squeegy
|
14
|
+
- "Georg Ledermann "
|
15
|
+
- Mikhail Khomutetskiy
|
16
|
+
autorequire:
|
17
|
+
bindir: bin
|
18
|
+
cert_chain: []
|
19
|
+
|
20
|
+
date: 2011-04-02 00:00:00 +06:00
|
21
|
+
default_executable:
|
22
|
+
dependencies:
|
23
|
+
- !ruby/object:Gem::Dependency
|
24
|
+
type: :runtime
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 13
|
31
|
+
segments:
|
32
|
+
- 3
|
33
|
+
- 0
|
34
|
+
- 5
|
35
|
+
version: 3.0.5
|
36
|
+
name: activerecord
|
37
|
+
version_requirements: *id001
|
38
|
+
prerelease: false
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
type: :runtime
|
41
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 13
|
47
|
+
segments:
|
48
|
+
- 3
|
49
|
+
- 0
|
50
|
+
- 5
|
51
|
+
version: 3.0.5
|
52
|
+
name: activesupport
|
53
|
+
version_requirements: *id002
|
54
|
+
prerelease: false
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
type: :development
|
57
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 1
|
65
|
+
- 0
|
66
|
+
- 10
|
67
|
+
version: 1.0.10
|
68
|
+
name: bundler
|
69
|
+
version_requirements: *id003
|
70
|
+
prerelease: false
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
type: :development
|
73
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ~>
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 7
|
79
|
+
segments:
|
80
|
+
- 1
|
81
|
+
- 5
|
82
|
+
- 2
|
83
|
+
version: 1.5.2
|
84
|
+
name: jeweler
|
85
|
+
version_requirements: *id004
|
86
|
+
prerelease: false
|
87
|
+
- !ruby/object:Gem::Dependency
|
88
|
+
type: :runtime
|
89
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ~>
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 13
|
95
|
+
segments:
|
96
|
+
- 3
|
97
|
+
- 0
|
98
|
+
- 5
|
99
|
+
version: 3.0.5
|
100
|
+
name: activerecord
|
101
|
+
version_requirements: *id005
|
102
|
+
prerelease: false
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
type: :runtime
|
105
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ~>
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
hash: 13
|
111
|
+
segments:
|
112
|
+
- 3
|
113
|
+
- 0
|
114
|
+
- 5
|
115
|
+
version: 3.0.5
|
116
|
+
name: activesupport
|
117
|
+
version_requirements: *id006
|
118
|
+
prerelease: false
|
119
|
+
- !ruby/object:Gem::Dependency
|
120
|
+
type: :development
|
121
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ~>
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
hash: 3
|
127
|
+
segments:
|
128
|
+
- 1
|
129
|
+
- 0
|
130
|
+
- 10
|
131
|
+
version: 1.0.10
|
132
|
+
name: bundler
|
133
|
+
version_requirements: *id007
|
134
|
+
prerelease: false
|
135
|
+
- !ruby/object:Gem::Dependency
|
136
|
+
type: :development
|
137
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - ~>
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
hash: 7
|
143
|
+
segments:
|
144
|
+
- 1
|
145
|
+
- 5
|
146
|
+
- 2
|
147
|
+
version: 1.5.2
|
148
|
+
name: jeweler
|
149
|
+
version_requirements: *id008
|
150
|
+
prerelease: false
|
151
|
+
description: Settings is a plugin that makes managing a table of global key, value pairs easy. Think of it like a global Hash stored in you database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global setting that you dont want to hard code into your rails app. You can store any kind of object. Strings, numbers, arrays, or any object.
|
152
|
+
email: khomutetskiy@gmail.com
|
153
|
+
executables: []
|
154
|
+
|
155
|
+
extensions: []
|
156
|
+
|
157
|
+
extra_rdoc_files:
|
158
|
+
- README.rdoc
|
159
|
+
files:
|
160
|
+
- Gemfile
|
161
|
+
- Gemfile.lock
|
162
|
+
- MIT-LICENSE
|
163
|
+
- README.rdoc
|
164
|
+
- Rakefile
|
165
|
+
- VERSION
|
166
|
+
- init.rb
|
167
|
+
- lib/generators/settings_migration/USAGE
|
168
|
+
- lib/generators/settings_migration/settings_migration_generator.rb
|
169
|
+
- lib/generators/settings_migration/templates/migration.rb
|
170
|
+
- lib/settings.rb
|
171
|
+
- rails3-settings.gemspec
|
172
|
+
- test/helper.rb
|
173
|
+
- test/settings_test.rb
|
174
|
+
has_rdoc: true
|
175
|
+
homepage: http://github.com/Magicdream/rails-settings
|
176
|
+
licenses:
|
177
|
+
- MIT
|
178
|
+
post_install_message:
|
179
|
+
rdoc_options: []
|
180
|
+
|
181
|
+
require_paths:
|
182
|
+
- lib
|
183
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
184
|
+
none: false
|
185
|
+
requirements:
|
186
|
+
- - ">="
|
187
|
+
- !ruby/object:Gem::Version
|
188
|
+
hash: 3
|
189
|
+
segments:
|
190
|
+
- 0
|
191
|
+
version: "0"
|
192
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
193
|
+
none: false
|
194
|
+
requirements:
|
195
|
+
- - ">="
|
196
|
+
- !ruby/object:Gem::Version
|
197
|
+
hash: 3
|
198
|
+
segments:
|
199
|
+
- 0
|
200
|
+
version: "0"
|
201
|
+
requirements: []
|
202
|
+
|
203
|
+
rubyforge_project:
|
204
|
+
rubygems_version: 1.6.2
|
205
|
+
signing_key:
|
206
|
+
specification_version: 3
|
207
|
+
summary: Store application settings or user-settings in DB like key-value pairs
|
208
|
+
test_files:
|
209
|
+
- test/helper.rb
|
210
|
+
- test/settings_test.rb
|