rails-settings 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +70 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/generators/settings_migration/USAGE +14 -0
- data/generators/settings_migration/settings_migration_generator.rb +12 -0
- data/generators/settings_migration/templates/migration.rb +15 -0
- data/init.rb +1 -0
- data/lib/settings.rb +92 -0
- data/rails-settings.gemspec +56 -0
- data/test/settings_test.rb +69 -0
- metadata +80 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg/*
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2006 Alex Wayne
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOa AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SaALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,70 @@
|
|
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
|
+
== Installation
|
11
|
+
|
12
|
+
Install the rails-settings gem
|
13
|
+
|
14
|
+
gem install rails-settings
|
15
|
+
|
16
|
+
And include the gem in your apps config
|
17
|
+
|
18
|
+
config.gem 'rails-settings', :lib => 'settings'
|
19
|
+
|
20
|
+
Or install as a plugin if you must. But gems are cooler.
|
21
|
+
|
22
|
+
./script/plugin install git://github.com/Squeegy/rails-settings.git
|
23
|
+
|
24
|
+
== Setup
|
25
|
+
|
26
|
+
You must create the table used by the Settings model. Simply run this command:
|
27
|
+
ruby script/generate settings_migration
|
28
|
+
|
29
|
+
Now just put that migration in the database with:
|
30
|
+
rake db:migrate
|
31
|
+
|
32
|
+
|
33
|
+
== Usage
|
34
|
+
|
35
|
+
The syntax is easy. First, lets create some settings to keep track of:
|
36
|
+
|
37
|
+
Settings.admin_password = 'supersecret'
|
38
|
+
Settings.date_format = '%m %d, %Y'
|
39
|
+
Settings.cocktails = ['Martini', 'Screwdriver', 'White Russian']
|
40
|
+
Settings.foo = 123
|
41
|
+
|
42
|
+
Now lets read them back:
|
43
|
+
|
44
|
+
Settings.foo # returns 123
|
45
|
+
|
46
|
+
Changing an existing setting is the same as creating a new setting:
|
47
|
+
|
48
|
+
Settings.foo = 'super duper bar'
|
49
|
+
|
50
|
+
Decide you dont want to track a particular setting anymore?
|
51
|
+
|
52
|
+
Settings.destroy :foo
|
53
|
+
Settings.foo # returns nil
|
54
|
+
|
55
|
+
Want a list of all the settings?
|
56
|
+
|
57
|
+
Settings.all # returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}
|
58
|
+
|
59
|
+
Set defaults for certain settings of your app. This will cause the defined settings to return with the
|
60
|
+
Specified value even if they are not in the database. Make a new file in config/initializers/settings.rb
|
61
|
+
with the following:
|
62
|
+
|
63
|
+
Settings.defaults[:some_setting] = 'footastic'
|
64
|
+
|
65
|
+
Now even if the database is completely empty, you app will have some intelligent defaults:
|
66
|
+
|
67
|
+
Settings.some_setting # returns 'footastic'
|
68
|
+
|
69
|
+
|
70
|
+
That's all there is to it!. Enjoy!
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rubygems'
|
5
|
+
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
desc 'Test the fleximage plugin.'
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.pattern = 'test/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
desc 'Generate documentation for the rails-settings plugin.'
|
18
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = 'Rails Settings'
|
21
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
22
|
+
rdoc.rdoc_files.include('README.rdoc')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'jeweler'
|
28
|
+
Jeweler::Tasks.new do |gem|
|
29
|
+
gem.name = "rails-settings"
|
30
|
+
gem.summary = <<EOF
|
31
|
+
Settings is a plugin that makes managing a table of global key, value pairs
|
32
|
+
easy. Think of it like a global Hash stored in your database, that uses simple
|
33
|
+
ActiveRecord like methods for manipulation. Keep track of any global setting
|
34
|
+
that you don't want to hard code into your rails app. You can store any kind
|
35
|
+
of object. Strings, numbers, arrays, or any serializable object.
|
36
|
+
EOF
|
37
|
+
gem.description = <<EOF
|
38
|
+
Settings is a plugin that makes managing a table of global key, value pairs
|
39
|
+
easy.
|
40
|
+
EOF
|
41
|
+
gem.email = "ruby@beautifulpixel.com"
|
42
|
+
gem.homepage = "http://github.com/Squeegy/rails-settings"
|
43
|
+
gem.authors = `git log --pretty=format:"%an"`.split("\n").uniq.sort
|
44
|
+
gem.add_development_dependency "rails", ">=2.0.1"
|
45
|
+
end
|
46
|
+
Jeweler::GemcutterTasks.new
|
47
|
+
rescue LoadError
|
48
|
+
puts "Jeweler (or a dependency) not available."
|
49
|
+
puts "Install it with: gem install jeweler"
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -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. 'add_settings_table' 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
|
+
./script/generate settings_migration
|
12
|
+
|
13
|
+
With 4 existing migrations, this will create an AddSettingsTable migration in the
|
14
|
+
file db/migrate/005_add_settings_table.rb
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class SettingsMigrationGenerator < Rails::Generator::NamedBase
|
2
|
+
def initialize(runtime_args, runtime_options = {})
|
3
|
+
runtime_args << 'add_settings_table' if runtime_args.empty?
|
4
|
+
super
|
5
|
+
end
|
6
|
+
|
7
|
+
def manifest
|
8
|
+
record do |m|
|
9
|
+
m.migration_template 'migration.rb', 'db/migrate'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class <%= class_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.timestamps
|
7
|
+
end
|
8
|
+
|
9
|
+
add_index :settings, :var, :uniq => true
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.down
|
13
|
+
drop_table :settings
|
14
|
+
end
|
15
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), %w(lib settings)))
|
data/lib/settings.rb
ADDED
@@ -0,0 +1,92 @@
|
|
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
|
+
object(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
|
43
|
+
def self.all
|
44
|
+
vars = find(:all, :select => 'var, value')
|
45
|
+
|
46
|
+
result = {}
|
47
|
+
vars.each do |record|
|
48
|
+
result[record.var] = record.value
|
49
|
+
end
|
50
|
+
result.with_indifferent_access
|
51
|
+
end
|
52
|
+
|
53
|
+
#retrieve a setting value by [] notation
|
54
|
+
def self.[](var_name)
|
55
|
+
if var = object(var_name)
|
56
|
+
var.value
|
57
|
+
elsif @@defaults[var_name.to_s]
|
58
|
+
@@defaults[var_name.to_s]
|
59
|
+
else
|
60
|
+
nil
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
#set a setting value by [] notation
|
65
|
+
def self.[]=(var_name, value)
|
66
|
+
var_name = var_name.to_s
|
67
|
+
|
68
|
+
record = object(var_name) || Settings.new(:var => var_name)
|
69
|
+
record.value = value
|
70
|
+
record.save
|
71
|
+
end
|
72
|
+
|
73
|
+
#retrieve the actual Setting record
|
74
|
+
def self.object(var_name)
|
75
|
+
Settings.find_by_var(var_name.to_s)
|
76
|
+
end
|
77
|
+
|
78
|
+
#get the value field, YAML decoded
|
79
|
+
def value
|
80
|
+
YAML::load(self[:value])
|
81
|
+
end
|
82
|
+
|
83
|
+
#set the value field, YAML encoded
|
84
|
+
def value=(new_value)
|
85
|
+
self[:value] = new_value.to_yaml
|
86
|
+
end
|
87
|
+
|
88
|
+
#Deprecated!
|
89
|
+
def self.reload # :nodoc:
|
90
|
+
self
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rails-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 = ["Alex Wayne", "Joshua Clayton", "squeegy"]
|
12
|
+
s.date = %q{2009-12-24}
|
13
|
+
s.description = %q{Settings is a plugin that makes managing a table of global key, value pairs
|
14
|
+
easy.
|
15
|
+
}
|
16
|
+
s.email = %q{ruby@beautifulpixel.com}
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"README.rdoc"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".gitignore",
|
22
|
+
"MIT-LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"generators/settings_migration/USAGE",
|
27
|
+
"generators/settings_migration/settings_migration_generator.rb",
|
28
|
+
"generators/settings_migration/templates/migration.rb",
|
29
|
+
"init.rb",
|
30
|
+
"lib/settings.rb",
|
31
|
+
"rails-settings.gemspec",
|
32
|
+
"test/settings_test.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/Squeegy/rails-settings}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.5}
|
38
|
+
s.summary = %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 your database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global 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 serializable object.}
|
39
|
+
s.test_files = [
|
40
|
+
"test/settings_test.rb"
|
41
|
+
]
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_development_dependency(%q<rails>, [">= 2.0.1"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<rails>, [">= 2.0.1"])
|
51
|
+
end
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rails>, [">= 2.0.1"])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -0,0 +1,69 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require File.dirname(__FILE__) + '/../../../../test/test_helper'
|
3
|
+
|
4
|
+
class SettingsTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
Settings.create(:var => 'test', :value => 'foo')
|
8
|
+
Settings.create(:var => 'test2', :value => 'bar')
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_defaults
|
12
|
+
Settings.defaults[:foo] = 'default foo'
|
13
|
+
|
14
|
+
assert_nil Settings.object(:foo)
|
15
|
+
assert_equal 'default foo', Settings.foo
|
16
|
+
|
17
|
+
Settings.foo = 'bar'
|
18
|
+
assert_equal 'bar', Settings.foo
|
19
|
+
assert_not_nil Settings.object(:foo)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_get
|
23
|
+
assert_setting 'foo', :test
|
24
|
+
assert_setting 'bar', :test2
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_update
|
28
|
+
assert_assign_setting '321', :test
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_create
|
32
|
+
assert_assign_setting '123', :onetwothree
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_complex_serialization
|
36
|
+
complex = [1, '2', {:three => true}]
|
37
|
+
Settings.complex = complex
|
38
|
+
assert_equal complex, Settings.complex
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_serialization_of_float
|
42
|
+
Settings.float = 0.01
|
43
|
+
Settings.reload
|
44
|
+
assert_equal 0.01, Settings.float
|
45
|
+
assert_equal 0.02, Settings.float * 2
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
def assert_setting(value, key)
|
50
|
+
key = key.to_sym
|
51
|
+
assert_equal value, eval("Settings.#{key}")
|
52
|
+
assert_equal value, Settings[key.to_sym]
|
53
|
+
assert_equal value, Settings[key.to_s]
|
54
|
+
end
|
55
|
+
|
56
|
+
def assert_assign_setting(value, key)
|
57
|
+
key = key.to_sym
|
58
|
+
assert_equal value, eval("Settings.#{key} = value")
|
59
|
+
assert_setting value, key
|
60
|
+
eval("Settings.#{key} = nil")
|
61
|
+
|
62
|
+
assert_equal value, (Settings[key] = value)
|
63
|
+
assert_setting value, key
|
64
|
+
Settings[key] = nil
|
65
|
+
|
66
|
+
assert_equal value, (Settings[key.to_s] = value)
|
67
|
+
assert_setting value, key
|
68
|
+
end
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-settings
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Wayne
|
8
|
+
- Joshua Clayton
|
9
|
+
- squeegy
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2009-12-24 00:00:00 -08:00
|
15
|
+
default_executable:
|
16
|
+
dependencies:
|
17
|
+
- !ruby/object:Gem::Dependency
|
18
|
+
name: rails
|
19
|
+
type: :development
|
20
|
+
version_requirement:
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 2.0.1
|
26
|
+
version:
|
27
|
+
description: |
|
28
|
+
Settings is a plugin that makes managing a table of global key, value pairs
|
29
|
+
easy.
|
30
|
+
|
31
|
+
email: ruby@beautifulpixel.com
|
32
|
+
executables: []
|
33
|
+
|
34
|
+
extensions: []
|
35
|
+
|
36
|
+
extra_rdoc_files:
|
37
|
+
- README.rdoc
|
38
|
+
files:
|
39
|
+
- .gitignore
|
40
|
+
- MIT-LICENSE
|
41
|
+
- README.rdoc
|
42
|
+
- Rakefile
|
43
|
+
- VERSION
|
44
|
+
- generators/settings_migration/USAGE
|
45
|
+
- generators/settings_migration/settings_migration_generator.rb
|
46
|
+
- generators/settings_migration/templates/migration.rb
|
47
|
+
- init.rb
|
48
|
+
- lib/settings.rb
|
49
|
+
- rails-settings.gemspec
|
50
|
+
- test/settings_test.rb
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/Squeegy/rails-settings
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options:
|
57
|
+
- --charset=UTF-8
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.3.5
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Settings is a plugin that makes managing a table of global key, value pairs easy. Think of it like a global Hash stored in your database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global 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 serializable object.
|
79
|
+
test_files:
|
80
|
+
- test/settings_test.rb
|