Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: Magicdream/rails-settings
base: master
...
head fork: emerencia/rails-settings
compare: master
Checking mergeability… Don’t worry, you can still create the pull request.
  • 4 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
Showing with 324 additions and 323 deletions.
  1. +2 −2 Gemfile
  2. +112 −112 README.rdoc
  3. +2 −2 Rakefile
  4. +131 −130 lib/settings.rb
  5. +77 −77 rails3-settings.gemspec
View
4 Gemfile
@@ -1,7 +1,7 @@
source :rubygems
-gem 'activerecord', '~> 3.0.5'
-gem 'activesupport', '~> 3.0.5'
+gem 'activerecord', '~> 3.1.0'
+gem 'activesupport', '~> 3.1.0'
group :test do
gem "sqlite3-ruby"
View
224 README.rdoc
@@ -1,112 +1,112 @@
-= Settings Plugin
-
-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.
-
-
-== Setup
-
-Requirements:
- Rails 3.0.x
-
-You can add this gem in your Gemfile
- gem 'rails3-settings', '~> 1.0.0', :require => 'settings'
-
-You must create the table used by the Settings model. Simply run this command:
- rails generate settings_migration
-
-Now just put that migration in the database with:
- rake db:migrate
-
-If you use some older version of this fork, beware that the database schema needs to
-be changed ("object_id" renamed to "target_id", "object_type" renamed to "target_type").
-You can do this with this migration:
-
- class RenameSettingsFields < ActiveRecord::Migration
- def self.up
- remove_index :settings, [ :object_type, :object_id, :var ]
-
- rename_column :settings, :object_id, :target_id
- rename_column :settings, :object_type, :target_type
-
- add_index :settings, [ :target_type, :target_id, :var ], :unique => true
- end
-
- def self.down
- # ...
- end
- end
-
-== Usage
-
-The syntax is easy. First, lets create some settings to keep track of:
-
- Settings.admin_password = 'supersecret'
- Settings.date_format = '%m %d, %Y'
- Settings.cocktails = ['Martini', 'Screwdriver', 'White Russian']
- Settings.foo = 123
- Settings.credentials = { :username => 'tom', :password => 'secret' }
-
-Now lets read them back:
-
- Settings.foo # returns 123
-
-Changing an existing setting is the same as creating a new setting:
-
- Settings.foo = 'super duper bar'
-
-For changing an existing setting which is a Hash, you can merge new values with existing ones:
- Settings.merge! :credentials, :password => 'topsecret'
- Settings.credentials # returns { :username => 'tom', :password => 'topsecret' }
-
-Decide you dont want to track a particular setting anymore?
-
- Settings.destroy :foo
- Settings.foo # returns nil
-
-Want a list of all the settings?
-
- Settings.all # returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}
-
-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:
-
- Settings['preferences.color'] = :blue
- Settings['preferences.size'] = :large
- Settings['license.key'] = 'ABC-DEF'
- Settings.all('preferences.') # returns { 'preferences.color' => :blue, 'preferences.size' => :large }
-
-Set defaults for certain settings of your app. This will cause the defined settings to return with the
-Specified value even if they are not in the database. Make a new file in config/initializers/settings.rb
-with the following:
-
- Settings.defaults[:some_setting] = 'footastic'
-
-Now even if the database is completely empty, you app will have some intelligent defaults:
-
- Settings.some_setting # returns 'footastic'
-
-Settings may be bound to any existing ActiveRecord object. Define this association like this:
-
- class User < ActiveRecord::Base
- has_settings
- end
-
-Then you can set/get a setting for a given user instance just by doing this:
-
- user = User.find(123)
- user.settings.color = :red
- user.settings.color # returns :red
- user.settings.all # { "color" => :red }
-
-I you want to find users having or not having some settings, there are named scopes for this:
-
- User.with_settings # => returns a scope of users having any setting
- User.with_settings_for('color') # => returns a scope of users having a 'color' setting
-
- User.without_settings # returns a scope of users having no setting at all (means user.settings.all == {})
- User.without_settings('color') # returns a scope of users having no 'color' setting (means user.settings.color == nil)
-
-That's all there is to it! Enjoy!
+= Settings Plugin
+
+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.
+
+
+== Setup
+
+Requirements:
+ Rails 3.0.x
+
+You can add this gem in your Gemfile
+ gem 'rails3-settings', '~> 1.0.0', :require => 'settings'
+
+You must create the table used by the Settings model. Simply run this command:
+ rails generate settings_migration
+
+Now just put that migration in the database with:
+ rake db:migrate
+
+If you use some older version of this fork, beware that the database schema needs to
+be changed ("object_id" renamed to "target_id", "object_type" renamed to "target_type").
+You can do this with this migration:
+
+ class RenameSettingsFields < ActiveRecord::Migration
+ def self.up
+ remove_index :settings, [ :object_type, :object_id, :var ]
+
+ rename_column :settings, :object_id, :target_id
+ rename_column :settings, :object_type, :target_type
+
+ add_index :settings, [ :target_type, :target_id, :var ], :unique => true
+ end
+
+ def self.down
+ # ...
+ end
+ end
+
+== Usage
+
+The syntax is easy. First, lets create some settings to keep track of:
+
+ Settings.admin_password = 'supersecret'
+ Settings.date_format = '%m %d, %Y'
+ Settings.cocktails = ['Martini', 'Screwdriver', 'White Russian']
+ Settings.foo = 123
+ Settings.credentials = { :username => 'tom', :password => 'secret' }
+
+Now lets read them back:
+
+ Settings.foo # returns 123
+
+Changing an existing setting is the same as creating a new setting:
+
+ Settings.foo = 'super duper bar'
+
+For changing an existing setting which is a Hash, you can merge new values with existing ones:
+ Settings.merge! :credentials, :password => 'topsecret'
+ Settings.credentials # returns { :username => 'tom', :password => 'topsecret' }
+
+Decide you dont want to track a particular setting anymore?
+
+ Settings.destroy :foo
+ Settings.foo # returns nil
+
+Want a list of all the settings?
+
+ Settings.all # returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}
+
+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:
+
+ Settings['preferences.color'] = :blue
+ Settings['preferences.size'] = :large
+ Settings['license.key'] = 'ABC-DEF'
+ Settings.all('preferences.') # returns { 'preferences.color' => :blue, 'preferences.size' => :large }
+
+Set defaults for certain settings of your app. This will cause the defined settings to return with the
+Specified value even if they are not in the database. Make a new file in config/initializers/settings.rb
+with the following:
+
+ Settings.defaults[:some_setting] = 'footastic'
+
+Now even if the database is completely empty, you app will have some intelligent defaults:
+
+ Settings.some_setting # returns 'footastic'
+
+Settings may be bound to any existing ActiveRecord object. Define this association like this:
+
+ class User < ActiveRecord::Base
+ has_settings
+ end
+
+Then you can set/get a setting for a given user instance just by doing this:
+
+ user = User.find(123)
+ user.settings.color = :red
+ user.settings.color # returns :red
+ user.settings.all # { "color" => :red }
+
+I you want to find users having or not having some settings, there are named scopes for this:
+
+ User.with_settings # => returns a scope of users having any setting
+ User.with_settings_for('color') # => returns a scope of users having a 'color' setting
+
+ User.without_settings # returns a scope of users having no setting at all (means user.settings.all == {})
+ User.without_settings('color') # returns a scope of users having no 'color' setting (means user.settings.color == nil)
+
+That's all there is to it! Enjoy!
View
4 Rakefile
@@ -22,8 +22,8 @@ Jeweler::Tasks.new do |gem|
# Include your dependencies below. Runtime dependencies are required when using your gem,
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
- gem.add_runtime_dependency 'activerecord', '~> 3.0.5'
- gem.add_runtime_dependency 'activesupport', '~> 3.0.5'
+ gem.add_runtime_dependency 'activerecord', '~> 3.1.0'
+ gem.add_runtime_dependency 'activesupport', '~> 3.1.0'
gem.add_development_dependency 'bundler', '~> 1.0.10'
gem.add_development_dependency 'jeweler', '~> 1.5.2'
View
261 lib/settings.rb
@@ -1,131 +1,132 @@
-class Settings < ActiveRecord::Base
- class SettingNotFound < RuntimeError; end
-
- cattr_accessor :defaults
- @@defaults = {}.with_indifferent_access
-
- # Support old plugin
- if defined?(SettingsDefaults::DEFAULTS)
- @@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
- end
-
- #get or set a variable with the variable as the called method
- def self.method_missing(method, *args)
- method_name = method.to_s
- super(method, *args)
-
- rescue NoMethodError
- #set a value for a variable
- if method_name =~ /=$/
- var_name = method_name.gsub('=', '')
- value = args.first
- self[var_name] = value
-
- #retrieve a value
- else
- self[method_name]
-
- end
- end
-
- #destroy the specified settings record
- def self.destroy(var_name)
- var_name = var_name.to_s
- if self[var_name]
- target(var_name).destroy
- true
- else
- raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
- end
- end
-
- #retrieve all settings as a hash (optionally starting with a given namespace)
- def self.all(starting_with=nil)
- options = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {}
- vars = target_scoped.find(:all, {:select => 'var, value'}.merge(options))
-
- result = {}
- vars.each do |record|
- result[record.var] = record.value
- end
- result.with_indifferent_access
- end
-
- #get a setting value by [] notation
- def self.[](var_name)
- if var = target(var_name)
- var.value
- else
- @@defaults[var_name.to_s]
- end
- end
-
- #set a setting value by [] notation
- def self.[]=(var_name, value)
- var_name = var_name.to_s
-
- record = target(var_name) || target_scoped.new(:var => var_name)
- record.value = value
- record.save!
-
- value
- end
-
- def self.merge!(var_name, hash_value)
- raise ArgumentError unless hash_value.is_a?(Hash)
-
- old_value = self[var_name] || {}
- raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
-
- new_value = old_value.merge(hash_value)
- self[var_name] = new_value if new_value != old_value
-
- new_value
- end
-
- def self.target(var_name)
- target_scoped.find_by_var(var_name.to_s)
- end
-
- #get the value field, YAML decoded
- def value
- YAML::load(self[:value])
- end
-
- #set the value field, YAML encoded
- def value=(new_value)
- self[:value] = new_value.to_yaml
- end
-
- def self.target_scoped
- Settings.scoped_by_target_type_and_target_id(target_type, target_id)
- end
-
- #Deprecated!
- def self.reload # :nodoc:
- self
- end
-
- def self.target_id
- nil
- end
-
- def self.target_type
- nil
- end
-end
-
-class ScopedSettings < Settings
- def self.for_target(target)
- @target = target
- self
- end
-
- def self.target_id
- @target.id
- end
-
- def self.target_type
- @target.class.base_class.to_s
- end
+class Settings < ActiveRecord::Base
+ class SettingNotFound < RuntimeError; end
+
+ cattr_accessor :defaults
+ @@defaults = {}.with_indifferent_access
+
+ # Support old plugin
+ if defined?(SettingsDefaults::DEFAULTS)
+ @@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
+ end
+
+ #get or set a variable with the variable as the called method
+ def self.method_missing(method, *args)
+ method_name = method.to_s
+ super(method, *args)
+
+ rescue NoMethodError
+ #set a value for a variable
+ if method_name =~ /=$/
+ var_name = method_name.gsub('=', '')
+ value = args.first
+ self[var_name] = value
+
+ #retrieve a value
+ else
+ self[method_name]
+
+ end
+ end
+
+ #destroy the specified settings record
+ def self.destroy(var_name)
+ var_name = var_name.to_s
+ if self[var_name]
+ target(var_name).destroy
+ true
+ else
+ raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
+ end
+ end
+
+ #retrieve all settings as a hash (optionally starting with a given namespace)
+ def self.all(starting_with=nil)
+ options = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {}
+ vars = target_scoped.find(:all, {:select => 'var, value'}.merge(options))
+
+ result = {}
+ vars.each do |record|
+ result[record.var] = record.value
+ end
+ result.with_indifferent_access
+ end
+
+ #get a setting value by [] notation
+ def self.[](var_name)
+ var = target(var_name)
+ if var != nil
+ var.value
+ else
+ @@defaults[var_name.to_s]
+ end
+ end
+
+ #set a setting value by [] notation
+ def self.[]=(var_name, value)
+ var_name = var_name.to_s
+
+ record = target(var_name) || target_scoped.new(:var => var_name)
+ record.value = value
+ record.save!
+
+ value
+ end
+
+ def self.merge!(var_name, hash_value)
+ raise ArgumentError unless hash_value.is_a?(Hash)
+
+ old_value = self[var_name] || {}
+ raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
+
+ new_value = old_value.merge(hash_value)
+ self[var_name] = new_value if new_value != old_value
+
+ new_value
+ end
+
+ def self.target(var_name)
+ target_scoped.find_by_var(var_name.to_s)
+ end
+
+ #get the value field, YAML decoded
+ def value
+ YAML::load(self[:value])
+ end
+
+ #set the value field, YAML encoded
+ def value=(new_value)
+ self[:value] = new_value.to_yaml
+ end
+
+ def self.target_scoped
+ Settings.scoped_by_target_type_and_target_id(target_type, target_id)
+ end
+
+ #Deprecated!
+ def self.reload # :nodoc:
+ self
+ end
+
+ def self.target_id
+ nil
+ end
+
+ def self.target_type
+ nil
+ end
+end
+
+class ScopedSettings < Settings
+ def self.for_target(target)
+ @target = target
+ self
+ end
+
+ def self.target_id
+ @target.id
+ end
+
+ def self.target_type
+ @target.class.base_class.to_s
+ end
end
View
154 rails3-settings.gemspec
@@ -1,77 +1,77 @@
-# Generated by jeweler
-# DO NOT EDIT THIS FILE DIRECTLY
-# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
-# -*- encoding: utf-8 -*-
-
-Gem::Specification.new do |s|
- s.name = %q{rails3-settings}
- s.version = "1.0.0"
-
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
- s.authors = ["Squeegy", "Georg Ledermann ", "Mikhail Khomutetskiy"]
- s.date = %q{2011-04-02}
- 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.}
- s.email = %q{khomutetskiy@gmail.com}
- s.extra_rdoc_files = [
- "README.rdoc"
- ]
- s.files = [
- "Gemfile",
- "Gemfile.lock",
- "MIT-LICENSE",
- "README.rdoc",
- "Rakefile",
- "VERSION",
- "init.rb",
- "lib/generators/settings_migration/USAGE",
- "lib/generators/settings_migration/settings_migration_generator.rb",
- "lib/generators/settings_migration/templates/migration.rb",
- "lib/settings.rb",
- "rails3-settings.gemspec",
- "test/helper.rb",
- "test/settings_test.rb"
- ]
- s.homepage = %q{http://github.com/Magicdream/rails-settings}
- s.licenses = ["MIT"]
- s.require_paths = ["lib"]
- s.rubygems_version = %q{1.6.2}
- s.summary = %q{Store application settings or user-settings in DB like key-value pairs}
- s.test_files = [
- "test/helper.rb",
- "test/settings_test.rb"
- ]
-
- if s.respond_to? :specification_version then
- s.specification_version = 3
-
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- s.add_runtime_dependency(%q<activerecord>, ["~> 3.0.5"])
- s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.5"])
- s.add_development_dependency(%q<bundler>, ["~> 1.0.10"])
- s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
- s.add_runtime_dependency(%q<activerecord>, ["~> 3.0.5"])
- s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.5"])
- s.add_development_dependency(%q<bundler>, ["~> 1.0.10"])
- s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
- else
- s.add_dependency(%q<activerecord>, ["~> 3.0.5"])
- s.add_dependency(%q<activesupport>, ["~> 3.0.5"])
- s.add_dependency(%q<bundler>, ["~> 1.0.10"])
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
- s.add_dependency(%q<activerecord>, ["~> 3.0.5"])
- s.add_dependency(%q<activesupport>, ["~> 3.0.5"])
- s.add_dependency(%q<bundler>, ["~> 1.0.10"])
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
- end
- else
- s.add_dependency(%q<activerecord>, ["~> 3.0.5"])
- s.add_dependency(%q<activesupport>, ["~> 3.0.5"])
- s.add_dependency(%q<bundler>, ["~> 1.0.10"])
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
- s.add_dependency(%q<activerecord>, ["~> 3.0.5"])
- s.add_dependency(%q<activesupport>, ["~> 3.0.5"])
- s.add_dependency(%q<bundler>, ["~> 1.0.10"])
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
- end
-end
-
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{rails3-settings}
+ s.version = "1.0.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Squeegy", "Georg Ledermann ", "Mikhail Khomutetskiy"]
+ s.date = %q{2011-04-02}
+ 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.}
+ s.email = %q{khomutetskiy@gmail.com}
+ s.extra_rdoc_files = [
+ "README.rdoc"
+ ]
+ s.files = [
+ "Gemfile",
+ "Gemfile.lock",
+ "MIT-LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION",
+ "init.rb",
+ "lib/generators/settings_migration/USAGE",
+ "lib/generators/settings_migration/settings_migration_generator.rb",
+ "lib/generators/settings_migration/templates/migration.rb",
+ "lib/settings.rb",
+ "rails3-settings.gemspec",
+ "test/helper.rb",
+ "test/settings_test.rb"
+ ]
+ s.homepage = %q{http://github.com/Magicdream/rails-settings}
+ s.licenses = ["MIT"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.6.2}
+ s.summary = %q{Store application settings or user-settings in DB like key-value pairs}
+ s.test_files = [
+ "test/helper.rb",
+ "test/settings_test.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<activerecord>, ["~> 3.1"])
+ s.add_runtime_dependency(%q<activesupport>, ["~> 3.1"])
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.10"])
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
+ s.add_runtime_dependency(%q<activerecord>, ["~> 3.1"])
+ s.add_runtime_dependency(%q<activesupport>, ["~> 3.1"])
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.10"])
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
+ else
+ s.add_dependency(%q<activerecord>, ["~> 3.1"])
+ s.add_dependency(%q<activesupport>, ["~> 3.1"])
+ s.add_dependency(%q<bundler>, ["~> 1.0.10"])
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
+ s.add_dependency(%q<activerecord>, ["~> 3.1"])
+ s.add_dependency(%q<activesupport>, ["~> 3.1"])
+ s.add_dependency(%q<bundler>, ["~> 1.0.10"])
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
+ end
+ else
+ s.add_dependency(%q<activerecord>, ["~> 3.1"])
+ s.add_dependency(%q<activesupport>, ["~> 3.1"])
+ s.add_dependency(%q<bundler>, ["~> 1.0.10"])
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
+ s.add_dependency(%q<activerecord>, ["~> 3.1"])
+ s.add_dependency(%q<activesupport>, ["~> 3.1"])
+ s.add_dependency(%q<bundler>, ["~> 1.0.10"])
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
+ end
+end
+

No commit comments for this range

Something went wrong with that request. Please try again.