Permalink
Browse files

Fixed naming conflict: Renamed "object_id" and "object_type" to "targ…

…et_id" and "target_type". ATTENTION: Needs migration!
  • Loading branch information...
1 parent 85ee45e commit 21f4789f42528bbb9dc850572e7eff4c15bb66af @ledermann ledermann committed Oct 15, 2010
Showing with 208 additions and 184 deletions.
  1. +21 −0 README.rdoc
  2. +3 −3 generators/settings_migration/templates/migration.rb
  3. +9 −9 init.rb
  4. +16 −16 lib/settings.rb
  5. +153 −153 test/settings_test.rb
  6. +6 −3 test/test_helper.rb
View
21 README.rdoc
@@ -9,12 +9,33 @@ arrays, or any object.
== Setup
+Requirements:
+ Rails 2.3.x
+
You must create the table used by the Settings model. Simply run this command:
ruby script/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
View
6 generators/settings_migration/templates/migration.rb
@@ -3,12 +3,12 @@ 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.integer :target_id, :null => true
+ t.string :target_type, :limit => 30, :null => true
t.timestamps
end
- add_index :settings, [ :object_type, :object_id, :var ], :unique => true
+ add_index :settings, [ :target_type, :target_id, :var ], :unique => true
end
def self.down
View
18 init.rb
@@ -4,23 +4,23 @@
def self.has_settings
class_eval do
def settings
- ScopedSettings.for_object(self)
+ ScopedSettings.for_target(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}')",
+ named_scope :with_settings, :joins => "JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
+ settings.target_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
+ named_scope :with_settings_for, lambda { |var| { :joins => "JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
+ settings.target_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}')",
+ named_scope :without_settings, :joins => "LEFT JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
+ settings.target_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
+ named_scope :without_settings_for, lambda { |var| { :joins => "LEFT JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
+ settings.target_type = '#{self.base_class.name}') AND
settings.var = '#{var}'",
:conditions => 'settings.id IS NULL' } }
end
View
32 lib/settings.rb
@@ -32,7 +32,7 @@ def self.method_missing(method, *args)
def self.destroy(var_name)
var_name = var_name.to_s
if self[var_name]
- object(var_name).destroy
+ target(var_name).destroy
true
else
raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
@@ -42,7 +42,7 @@ def self.destroy(var_name)
#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 = object_scoped.find(:all, {:select => 'var, value'}.merge(options))
+ vars = target_scoped.find(:all, {:select => 'var, value'}.merge(options))
result = {}
vars.each do |record|
@@ -53,7 +53,7 @@ def self.all(starting_with=nil)
#get a setting value by [] notation
def self.[](var_name)
- if var = object(var_name)
+ if var = target(var_name)
var.value
elsif @@defaults[var_name.to_s]
@@defaults[var_name.to_s]
@@ -66,7 +66,7 @@ def self.[](var_name)
def self.[]=(var_name, value)
var_name = var_name.to_s
- record = object(var_name) || object_scoped.new(:var => var_name)
+ record = target(var_name) || target_scoped.new(:var => var_name)
record.value = value
record.save!
@@ -85,8 +85,8 @@ def self.merge!(var_name, hash_value)
new_value
end
- def self.object(var_name)
- object_scoped.find_by_var(var_name.to_s)
+ def self.target(var_name)
+ target_scoped.find_by_var(var_name.to_s)
end
#get the value field, YAML decoded
@@ -99,35 +99,35 @@ def value=(new_value)
self[:value] = new_value.to_yaml
end
- def self.object_scoped
- Settings.scoped_by_object_type_and_object_id(object_type, object_id)
+ 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.object_id
+ def self.target_id
nil
end
- def self.object_type
+ def self.target_type
nil
end
end
class ScopedSettings < Settings
- def self.for_object(object)
- @object = object
+ def self.for_target(target)
+ @target = target
self
end
- def self.object_id
- @object.id
+ def self.target_id
+ @target.id
end
- def self.object_type
- @object.class.base_class.to_s
+ def self.target_type
+ @target.class.base_class.to_s
end
end
View
306 test/settings_test.rb
@@ -1,154 +1,154 @@
-require 'test_helper'
-
-class SettingsTest < Test::Unit::TestCase
- setup_db
-
- def setup
- Settings.create(:var => 'test', :value => 'foo')
- Settings.create(:var => 'test2', :value => 'bar')
- end
-
- def teardown
- Settings.delete_all
- end
-
- def test_defaults
- Settings.defaults[:foo] = 'default foo'
-
- assert_nil Settings.object(:foo)
- assert_equal 'default foo', Settings.foo
-
- Settings.foo = 'bar'
- assert_equal 'bar', Settings.foo
- assert_not_nil Settings.object(:foo)
- end
-
- def test_get
- assert_setting 'foo', :test
- assert_setting 'bar', :test2
- end
-
- def test_update
- assert_assign_setting '321', :test
- end
-
- def test_create
- assert_assign_setting '123', :onetwothree
- end
-
- def test_complex_serialization
- complex = [1, '2', {:three => true}]
- Settings.complex = complex
- assert_equal complex, Settings.complex
- end
-
- def test_serialization_of_float
- Settings.float = 0.01
- Settings.reload
- assert_equal 0.01, Settings.float
- assert_equal 0.02, Settings.float * 2
- end
-
- def test_object_scope
- user1 = User.create :name => 'First user'
- user2 = User.create :name => 'Second user'
-
- assert_assign_setting 1, :one, user1
- assert_assign_setting 2, :two, user2
-
- assert_setting 1, :one, user1
- assert_setting 2, :two, user2
-
- assert_setting nil, :one
- assert_setting nil, :two
-
- assert_setting nil, :two, user1
- assert_setting nil, :one, user2
-
- assert_equal({ "one" => 1}, user1.settings.all('one'))
- assert_equal({ "two" => 2}, user2.settings.all('two'))
- assert_equal({ "one" => 1}, user1.settings.all('o'))
- assert_equal({}, user1.settings.all('non_existing_var'))
- end
-
- def test_named_scope
- user_without_settings = User.create :name => 'User without settings'
- user_with_settings = User.create :name => 'User with settings'
- user_with_settings.settings.one = '1'
- user_with_settings.settings.two = '2'
-
- assert_equal [user_with_settings], User.with_settings
- assert_equal [user_with_settings], User.with_settings_for('one')
- assert_equal [user_with_settings], User.with_settings_for('two')
- assert_equal [], User.with_settings_for('foo')
-
- assert_equal [user_without_settings], User.without_settings
- assert_equal [user_without_settings], User.without_settings_for('one')
- assert_equal [user_without_settings], User.without_settings_for('two')
- assert_equal [user_without_settings, user_with_settings], User.without_settings_for('foo')
- end
-
- def test_all
- assert_equal({ "test2" => "bar", "test" => "foo" }, Settings.all)
- assert_equal({ "test2" => "bar" }, Settings.all('test2'))
- assert_equal({ "test2" => "bar", "test" => "foo" }, Settings.all('test'))
- assert_equal({}, Settings.all('non_existing_var'))
- end
-
- def test_merge
- assert_raise(TypeError) do
- Settings.merge! :test, { :a => 1 }
- end
-
- Settings[:hash] = { :one => 1 }
- Settings.merge! :hash, { :two => 2 }
- assert_equal({ :one => 1, :two => 2 }, Settings[:hash])
-
- assert_raise(ArgumentError) do
- Settings.merge! :hash, 123
- end
-
- Settings.merge! :empty_hash, { :two => 2 }
- assert_equal({ :two => 2 }, Settings[:empty_hash])
- end
-
- def test_destroy
- Settings.destroy :test
- assert_equal nil, Settings.test
- end
-
- private
- def assert_setting(value, key, scope_object=nil)
- key = key.to_sym
-
- if scope_object
- assert_equal value, scope_object.instance_eval("settings.#{key}")
- assert_equal value, scope_object.settings[key.to_sym]
- assert_equal value, scope_object.settings[key.to_s]
- else
- assert_equal value, eval("Settings.#{key}")
- assert_equal value, Settings[key.to_sym]
- assert_equal value, Settings[key.to_s]
- end
- end
-
- def assert_assign_setting(value, key, scope_object=nil)
- key = key.to_sym
-
- if scope_object
- assert_equal value, (scope_object.settings[key] = value)
- assert_setting value, key, scope_object
- scope_object.settings[key] = nil
-
- assert_equal value, (scope_object.settings[key.to_s] = value)
- assert_setting value, key, scope_object
- else
- assert_equal value, (Settings[key] = value)
- assert_setting value, key
- Settings[key] = nil
-
- assert_equal value, (Settings[key.to_s] = value)
- assert_setting value, key
- end
- end
+require 'test_helper'
+
+class SettingsTest < Test::Unit::TestCase
+ setup_db
+
+ def setup
+ Settings.create(:var => 'test', :value => 'foo')
+ Settings.create(:var => 'test2', :value => 'bar')
+ end
+
+ def teardown
+ Settings.delete_all
+ end
+
+ def test_defaults
+ Settings.defaults[:foo] = 'default foo'
+
+ assert_nil Settings.target(:foo)
+ assert_equal 'default foo', Settings.foo
+
+ Settings.foo = 'bar'
+ assert_equal 'bar', Settings.foo
+ assert_not_nil Settings.target(:foo)
+ end
+
+ def test_get
+ assert_setting 'foo', :test
+ assert_setting 'bar', :test2
+ end
+
+ def test_update
+ assert_assign_setting '321', :test
+ end
+
+ def test_create
+ assert_assign_setting '123', :onetwothree
+ end
+
+ def test_complex_serialization
+ complex = [1, '2', {:three => true}]
+ Settings.complex = complex
+ assert_equal complex, Settings.complex
+ end
+
+ def test_serialization_of_float
+ Settings.float = 0.01
+ Settings.reload
+ assert_equal 0.01, Settings.float
+ assert_equal 0.02, Settings.float * 2
+ end
+
+ def test_target_scope
+ user1 = User.create :name => 'First user'
+ user2 = User.create :name => 'Second user'
+
+ assert_assign_setting 1, :one, user1
+ assert_assign_setting 2, :two, user2
+
+ assert_setting 1, :one, user1
+ assert_setting 2, :two, user2
+
+ assert_setting nil, :one
+ assert_setting nil, :two
+
+ assert_setting nil, :two, user1
+ assert_setting nil, :one, user2
+
+ assert_equal({ "one" => 1}, user1.settings.all('one'))
+ assert_equal({ "two" => 2}, user2.settings.all('two'))
+ assert_equal({ "one" => 1}, user1.settings.all('o'))
+ assert_equal({}, user1.settings.all('non_existing_var'))
+ end
+
+ def test_named_scope
+ user_without_settings = User.create :name => 'User without settings'
+ user_with_settings = User.create :name => 'User with settings'
+ user_with_settings.settings.one = '1'
+ user_with_settings.settings.two = '2'
+
+ assert_equal [user_with_settings], User.with_settings
+ assert_equal [user_with_settings], User.with_settings_for('one')
+ assert_equal [user_with_settings], User.with_settings_for('two')
+ assert_equal [], User.with_settings_for('foo')
+
+ assert_equal [user_without_settings], User.without_settings
+ assert_equal [user_without_settings], User.without_settings_for('one')
+ assert_equal [user_without_settings], User.without_settings_for('two')
+ assert_equal [user_without_settings, user_with_settings], User.without_settings_for('foo')
+ end
+
+ def test_all
+ assert_equal({ "test2" => "bar", "test" => "foo" }, Settings.all)
+ assert_equal({ "test2" => "bar" }, Settings.all('test2'))
+ assert_equal({ "test2" => "bar", "test" => "foo" }, Settings.all('test'))
+ assert_equal({}, Settings.all('non_existing_var'))
+ end
+
+ def test_merge
+ assert_raise(TypeError) do
+ Settings.merge! :test, { :a => 1 }
+ end
+
+ Settings[:hash] = { :one => 1 }
+ Settings.merge! :hash, { :two => 2 }
+ assert_equal({ :one => 1, :two => 2 }, Settings[:hash])
+
+ assert_raise(ArgumentError) do
+ Settings.merge! :hash, 123
+ end
+
+ Settings.merge! :empty_hash, { :two => 2 }
+ assert_equal({ :two => 2 }, Settings[:empty_hash])
+ end
+
+ def test_destroy
+ Settings.destroy :test
+ assert_equal nil, Settings.test
+ end
+
+ private
+ def assert_setting(value, key, scope_target=nil)
+ key = key.to_sym
+
+ if scope_target
+ assert_equal value, scope_target.instance_eval("settings.#{key}")
+ assert_equal value, scope_target.settings[key.to_sym]
+ assert_equal value, scope_target.settings[key.to_s]
+ else
+ assert_equal value, eval("Settings.#{key}")
+ assert_equal value, Settings[key.to_sym]
+ assert_equal value, Settings[key.to_s]
+ end
+ end
+
+ def assert_assign_setting(value, key, scope_target=nil)
+ key = key.to_sym
+
+ if scope_target
+ assert_equal value, (scope_target.settings[key] = value)
+ assert_setting value, key, scope_target
+ scope_target.settings[key] = nil
+
+ assert_equal value, (scope_target.settings[key.to_s] = value)
+ assert_setting value, key, scope_target
+ else
+ assert_equal value, (Settings[key] = value)
+ assert_setting value, key
+ Settings[key] = nil
+
+ assert_equal value, (Settings[key.to_s] = value)
+ assert_setting value, key
+ end
+ end
end
View
9 test/test_helper.rb
@@ -1,3 +1,6 @@
+gem 'activesupport', '< 3'
+gem 'activerecord', '< 3'
+
require 'rubygems'
require 'active_support'
require 'active_support/test_case'
@@ -17,11 +20,11 @@ def setup_db
create_table :settings 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.integer :target_id, :null => true
+ t.string :target_type, :limit => 30, :null => true
t.timestamps
end
- add_index :settings, [ :object_type, :object_id, :var ], :unique => true
+ add_index :settings, [ :target_type, :target_id, :var ], :unique => true
create_table :users do |t|
t.string :name

0 comments on commit 21f4789

Please sign in to comment.