Permalink
Browse files

Ported to Rails 3

  • Loading branch information...
1 parent 85ee45e commit 08e9b2725e8912826f3ddead8098937cca49e1f1 100hz committed Oct 7, 2010
View
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>rails-settings</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ </buildSpec>
+ <natures>
+ <nature>org.radrails.rails.core.railsnature</nature>
+ </natures>
+</projectDescription>
View
@@ -1,16 +1,19 @@
-= Settings Plugin
+= Settings Gem
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.
+arrays, or any object. Ported to Rails 3!
== Setup
-You must create the table used by the Settings model. Simply run this command:
- ruby script/generate settings_migration
+Edit your Gemfile:
+ gem "rails-settings", :git => "git://github.com/100hz/rails-settings.git"
+
+Generate your settings:
+ rails g settings <settings_name>
Now just put that migration in the database with:
rake db:migrate
@@ -20,49 +23,49 @@ Now just put that migration in the database with:
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' }
+ MySettings.admin_password = 'supersecret'
+ MySettings.date_format = '%m %d, %Y'
+ MySettings.cocktails = ['Martini', 'Screwdriver', 'White Russian']
+ MySettings.foo = 123
+ MySettings.credentials = { :username => 'tom', :password => 'secret' }
Now lets read them back:
- Settings.foo # returns 123
+ MySettings.foo # returns 123
Changing an existing setting is the same as creating a new setting:
- Settings.foo = 'super duper bar'
+ MySettings.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' }
+ MySettings.merge! :credentials, :password => 'topsecret'
+ MySettings.credentials # returns { :username => 'tom', :password => 'topsecret' }
Decide you dont want to track a particular setting anymore?
- Settings.destroy :foo
- Settings.foo # returns nil
+ MySettings.destroy :foo
+ MySettings.foo # returns nil
Want a list of all the settings?
- Settings.all # returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}
+ MySettings.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 }
+ MySettings['preferences.color'] = :blue
+ MySettings['preferences.size'] = :large
+ MySettings['license.key'] = 'ABC-DEF'
+ MySettings.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'
+ MySettings.defaults[:some_setting] = 'footastic'
Now even if the database is completely empty, you app will have some intelligent defaults:
- Settings.some_setting # returns 'footastic'
+ MySettings.some_setting # returns 'footastic'
Settings may be bound to any existing ActiveRecord object. Define this association like this:
View
@@ -1,9 +1,20 @@
-require 'rake/testtask'
+require 'rubygems'
+require 'rake'
-Rake::TestTask.new do |t|
- t.libs << 'lib' << 'test'
- t.pattern = 'test/**/*_test.rb'
- t.verbose = true
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new() do |gem|
+ gem.name = "rails-settings"
+ gem.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 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. Ported to Rails 3!"
+ gem.email = "rails-settings@theblackestbox.net"
+ gem.homepage = "http://theblackestbox.net"
+ gem.authors = ["Squeegy","Georg Ledermann","100hz"]
+ gem.add_dependency "rails", ">= 3.0.0"
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end
-task :default => :test
+
+task :default => :release
View
@@ -0,0 +1 @@
+0.1.0
@@ -1,14 +0,0 @@
-Description:
- The settings migration generator creates a migration for the settings plugin.
-
- The generator takes a migration name as its argument. The migration name may be
- given in CamelCase or under_score. 'add_settings_table' is the default.
-
- The generator creates a migration class in db/migrate prefixed by its number
- in the queue.
-
-Example:
- ./script/generate settings_migration
-
- With 4 existing migrations, this will create an AddSettingsTable migration in the
- file db/migrate/005_add_settings_table.rb
@@ -1,12 +0,0 @@
-class SettingsMigrationGenerator < Rails::Generator::NamedBase
- def initialize(runtime_args, runtime_options = {})
- runtime_args << 'add_settings_table' if runtime_args.empty?
- super
- end
-
- def manifest
- record do |m|
- m.migration_template 'migration.rb', 'db/migrate'
- end
- end
-end
@@ -1,17 +0,0 @@
-class <%= class_name %> < ActiveRecord::Migration
- def self.up
- create_table :settings, :force => true do |t|
- t.string :var, :null => false
- t.text :value, :null => true
- t.integer :object_id, :null => true
- t.string :object_type, :limit => 30, :null => true
- t.timestamps
- end
-
- add_index :settings, [ :object_type, :object_id, :var ], :unique => true
- end
-
- def self.down
- drop_table :settings
- end
-end
View
28 init.rb
@@ -1,28 +0,0 @@
-require 'settings'
-
-ActiveRecord::Base.class_eval do
- def self.has_settings
- class_eval do
- def settings
- ScopedSettings.for_object(self)
- end
-
- named_scope :with_settings, :joins => "JOIN settings ON (settings.object_id = #{self.table_name}.#{self.primary_key} AND
- settings.object_type = '#{self.base_class.name}')",
- :select => "DISTINCT #{self.table_name}.*"
-
- named_scope :with_settings_for, lambda { |var| { :joins => "JOIN settings ON (settings.object_id = #{self.table_name}.#{self.primary_key} AND
- settings.object_type = '#{self.base_class.name}') AND
- settings.var = '#{var}'" } }
-
- named_scope :without_settings, :joins => "LEFT JOIN settings ON (settings.object_id = #{self.table_name}.#{self.primary_key} AND
- settings.object_type = '#{self.base_class.name}')",
- :conditions => 'settings.id IS NULL'
-
- named_scope :without_settings_for, lambda { |var| { :joins => "LEFT JOIN settings ON (settings.object_id = #{self.table_name}.#{self.primary_key} AND
- settings.object_type = '#{self.base_class.name}') AND
- settings.var = '#{var}'",
- :conditions => 'settings.id IS NULL' } }
- end
- end
-end
@@ -0,0 +1,30 @@
+require 'rails/generators/migration'
+
+ class SettingsGenerator < Rails::Generators::NamedBase
+ include Rails::Generators::Migration
+
+ argument :name, :type => :string, :default => "my_settings"
+
+ source_root File.expand_path('../templates', __FILE__)
+
+ @@migrations = false
+
+ def self.next_migration_number(dirname) #:nodoc:
+ if ActiveRecord::Base.timestamped_migrations
+ if @@migrations
+ (current_migration_number(dirname) + 1)
+ else
+ @@migrations = true
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
+ end
+ else
+ "%.3d" % (current_migration_number(dirname) + 1)
+ end
+ end
+
+ def settings
+ #generate(:model, name, "--skip-migration")
+ template "model.rb", File.join("app/models",class_path,"#{file_name}.rb"), :force => true
+ migration_template "migration.rb", "db/migrate/create_settings.rb"
+ end
+ end
@@ -0,0 +1,17 @@
+class CreateSettings < ActiveRecord::Migration
+ def self.up
+ create_table :settings do |t|
+ t.string :var, :null => false
+ t.text :value, :null => true
+ t.integer :thing_id, :null => true
+ t.string :thing_type, :limit => 30, :null => true
+ t.timestamps
+ end
+
+ add_index :settings, [ :thing_type, :thing_id, :var ], :unique => true
+ end
+
+ def self.down
+ drop_table :settings
+ end
+end
@@ -0,0 +1,3 @@
+class <%= class_name %> < RailsSettings::Settings
+
+end
View
@@ -0,0 +1,5 @@
+require "rails-settings/settings"
+require "rails-settings/scoped_settings"
+
+
+require "rails-settings/railtie" if defined?(Rails) && Rails.version >= "3"
@@ -0,0 +1,43 @@
+module RailsSettings
+ class Railtie < Rails::Railtie
+
+ initializer 'rails_settings.initialize', :after => :after_initialize do
+ Railtie.extend_active_record
+ end
+
+ end
+
+ class Railtie
+ def self.extend_active_record
+ ActiveRecord::Base.class_eval do
+ def self.has_settings
+ class_eval do
+ def settings
+ RailsSettings::ScopedSettings.for_thing(self)
+ end
+
+ scope :with_settings, :joins => "JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
+ settings.thing_type = '#{self.base_class.name}')",
+ :select => "DISTINCT #{self.table_name}.*"
+
+ scope :with_settings_for, lambda { |var| { :joins => "JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
+ settings.thing_type = '#{self.base_class.name}') AND
+ settings.var = '#{var}'" } }
+
+ scope :without_settings, :joins => "LEFT JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
+ settings.thing_type = '#{self.base_class.name}')",
+ :conditions => 'settings.id IS NULL'
+
+ scope :without_settings_for, lambda { |var| { :joins => "LEFT JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
+ settings.thing_type = '#{self.base_class.name}') AND
+ settings.var = '#{var}'",
+ :conditions => 'settings.id IS NULL' } }
+ end
+ end
+ end
+ end
+
+ end
+end
+
+
@@ -0,0 +1,13 @@
+module RailsSettings
+ class ScopedSettings < Settings
+ def self.for_object(object)
+ @object = object
+ self
+ end
+
+ def self.thing_scoped
+ Settings.scoped_by_thing_type_and_thing_id(@object.class.base_class.to_s, @object.id)
+ end
+
+ end
+end
Oops, something went wrong.

0 comments on commit 08e9b27

Please sign in to comment.