├── .gitignore ├── MIT-LICENSE ├── README.rdoc ├── Rakefile ├── VERSION ├── generators └── settings_migration │ ├── USAGE │ ├── settings_migration_generator.rb │ └── templates │ └── migration.rb ├── init.rb ├── lib └── settings.rb ├── rails-settings.gemspec └── test └── settings_test.rb /.gitignore: -------------------------------------------------------------------------------- 1 | pkg/* 2 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 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. -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | ----- 2 | 3 | = DEPRECATED 4 | 5 | This gem is no longer maintained. However, there is a complete rewrite of this gem maintained here: 6 | 7 | * https://github.com/ledermann/rails-settings 8 | 9 | _Old README below_ 10 | 11 | ----- 12 | 13 | = Settings Plugin 14 | 15 | Settings is a plugin that makes managing a table of global key, value pairs easy. 16 | Think of it like a global Hash stored in you database, that uses simple ActiveRecord 17 | like methods for manipulation. Keep track of any global setting that you dont want 18 | to hard code into your rails app. You can store any kind of object. Strings, numbers, 19 | arrays, or any object. 20 | 21 | 22 | == Installation 23 | 24 | Install the rails-settings gem 25 | 26 | gem install rails-settings 27 | 28 | And include the gem in your apps config 29 | 30 | config.gem 'rails-settings', :lib => 'settings' 31 | 32 | Or install as a plugin if you must. But gems are cooler. 33 | 34 | ./script/plugin install git://github.com/Squeegy/rails-settings.git 35 | 36 | == Setup 37 | 38 | You must create the table used by the Settings model. Simply run this command: 39 | ruby script/generate settings_migration 40 | 41 | Now just put that migration in the database with: 42 | rake db:migrate 43 | 44 | 45 | == Usage 46 | 47 | The syntax is easy. First, lets create some settings to keep track of: 48 | 49 | Settings.admin_password = 'supersecret' 50 | Settings.date_format = '%m %d, %Y' 51 | Settings.cocktails = ['Martini', 'Screwdriver', 'White Russian'] 52 | Settings.foo = 123 53 | 54 | Now lets read them back: 55 | 56 | Settings.foo # returns 123 57 | 58 | Changing an existing setting is the same as creating a new setting: 59 | 60 | Settings.foo = 'super duper bar' 61 | 62 | Decide you dont want to track a particular setting anymore? 63 | 64 | Settings.destroy :foo 65 | Settings.foo # returns nil 66 | 67 | Want a list of all the settings? 68 | 69 | Settings.all # returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'} 70 | 71 | Set defaults for certain settings of your app. This will cause the defined settings to return with the 72 | Specified value even if they are not in the database. Make a new file in config/initializers/settings.rb 73 | with the following: 74 | 75 | Settings.defaults[:some_setting] = 'footastic' 76 | 77 | Now even if the database is completely empty, you app will have some intelligent defaults: 78 | 79 | Settings.some_setting # returns 'footastic' 80 | 81 | 82 | That's all there is to it!. Enjoy! 83 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 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 = <=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 51 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 1.0.0 2 | -------------------------------------------------------------------------------- /generators/settings_migration/USAGE: -------------------------------------------------------------------------------- 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 -------------------------------------------------------------------------------- /generators/settings_migration/settings_migration_generator.rb: -------------------------------------------------------------------------------- 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 13 | -------------------------------------------------------------------------------- /generators/settings_migration/templates/migration.rb: -------------------------------------------------------------------------------- 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 16 | -------------------------------------------------------------------------------- /init.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path(File.join(File.dirname(__FILE__), %w(lib settings))) -------------------------------------------------------------------------------- /lib/settings.rb: -------------------------------------------------------------------------------- 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 93 | -------------------------------------------------------------------------------- /rails-settings.gemspec: -------------------------------------------------------------------------------- 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, [">= 2.0.1"]) 49 | else 50 | s.add_dependency(%q, [">= 2.0.1"]) 51 | end 52 | else 53 | s.add_dependency(%q, [">= 2.0.1"]) 54 | end 55 | end 56 | 57 | -------------------------------------------------------------------------------- /test/settings_test.rb: -------------------------------------------------------------------------------- 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 70 | --------------------------------------------------------------------------------