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: pluginaweek/preferences
base: master
...
head fork: moomerman/preferences
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 10 commits
  • 12 files changed
  • 0 commit comments
  • 5 contributors
View
4 CHANGELOG.rdoc
@@ -1,5 +1,9 @@
== master
+== 0.5.0 / 2011-08-23
+
+* Rails 3 support [Jack Dempsey]
+
== 0.4.2 / 2010-04-17
* Fix #preferences lookup not typecasting values
View
2  README.rdoc
@@ -221,4 +221,4 @@ To run against a specific version of Rails:
== Dependencies
-* Rails 2.3 or later
+* 0.5.0 for Rails 3 (0.4.x and below for Rails 2)
View
7 generators/preferences/preferences_generator.rb
@@ -1,7 +0,0 @@
-class PreferencesGenerator < Rails::Generator::Base
- def manifest
- record do |m|
- m.migration_template '001_create_preferences.rb', 'db/migrate', :migration_file_name => 'create_preferences'
- end
- end
-end
View
1  init.rb
@@ -1 +0,0 @@
-require 'preferences'
View
2  generators/preferences/USAGE → lib/generators/USAGE
@@ -1,5 +1,5 @@
Usage:
- script/generate preferences
+ rails generate preferences
This will create a migration that will add the proper table to store preferences.
View
17 lib/generators/preferences_generator.rb
@@ -0,0 +1,17 @@
+class PreferencesGenerator < Rails::Generators::Base
+ include Rails::Generators::Migration
+
+ source_root File.expand_path("../templates", __FILE__)
+
+ def self.next_migration_number(dirname)
+ if ActiveRecord::Base.timestamped_migrations
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
+ else
+ "%.3d" % (current_migration_number(dirname) + 1)
+ end
+ end
+
+ def create_migration_file
+ migration_template 'create_preferences.rb', "db/migrate/create_preferences.rb"
+ end
+end
View
6 ...ences/templates/001_create_preferences.rb → ...enerators/templates/create_preferences.rb
@@ -1,5 +1,5 @@
class CreatePreferences < ActiveRecord::Migration
- def self.up
+ def change
create_table :preferences do |t|
t.string :name, :null => false
t.references :owner, :polymorphic => true, :null => false
@@ -9,8 +9,4 @@ def self.up
end
add_index :preferences, [:owner_id, :owner_type, :name, :group_id, :group_type], :unique => true, :name => 'index_preferences_on_owner_and_name_and_preference'
end
-
- def self.down
- drop_table :preferences
- end
end
View
11 lib/preferences.rb
@@ -1,3 +1,4 @@
+require 'preferences/engine'
require 'preferences/preference_definition'
# Adds support for defining preferences on ActiveRecord models.
@@ -152,16 +153,16 @@ module MacroMethods
# user.save! # => true
def preference(name, *args)
unless included_modules.include?(InstanceMethods)
- class_inheritable_hash :preference_definitions
+ class_attribute :preference_definitions
self.preference_definitions = {}
- has_many :stored_preferences, :as => :owner, :class_name => 'Preference'
+ has_many :stored_preferences, :as => :owner, :class_name => 'Preference', :dependent => :destroy
after_save :update_preferences
# Named scopes
- named_scope :with_preferences, lambda {|preferences| build_preference_scope(preferences)}
- named_scope :without_preferences, lambda {|preferences| build_preference_scope(preferences, true)}
+ scope :with_preferences, lambda {|preferences| build_preference_scope(preferences)}
+ scope :without_preferences, lambda {|preferences| build_preference_scope(preferences, true)}
extend Preferences::ClassMethods
include Preferences::InstanceMethods
@@ -417,7 +418,7 @@ def write_preference(name, value, group = nil)
end
value = convert_number_column_value(value) if preference_definitions[name].number?
- preferences_group(group)[name] = value
+ preferences_group(group)[name] = preference_definitions[name].type_cast(value)
value
end
View
4 lib/preferences/engine.rb
@@ -0,0 +1,4 @@
+module Preferences
+ class Engine < Rails::Engine
+ end
+end
View
2  lib/preferences/version.rb
@@ -1,3 +1,3 @@
module Preferences
- VERSION = '0.4.2'
+ VERSION = '0.5.0'
end
View
25 test/functional/preferences_test.rb
@@ -129,6 +129,26 @@ def test_should_include_new_definitions_in_preference_definitions
end
end
+class PreferencesTypeCasted < ModelPreferenceTest
+ def setup
+ @definition = User.preference :rate, :float, :default => 1.0
+ @user = new_user
+ end
+
+ def test_should_have_float_type
+ assert_equal :float, @definition.type
+ end
+
+ def test_should_only_have_default_preferences
+ assert_equal e = {'rate' => 1.0}, @user.preferences
+ end
+
+ def test_should_type_cast_changed_values
+ @user.write_preference(:rate, "1.1")
+ assert_equal e = {'rate' => 1.1}, @user.preferences
+ end
+end
+
class PreferencesByDefaultTest < ModelPreferenceTest
def setup
@definition = User.preference :notifications
@@ -178,7 +198,7 @@ def setup
@user = new_user
end
- def test_should_have_boolean_type
+ def test_should_have_integer_type
assert_equal :integer, @definition.type
end
@@ -236,6 +256,7 @@ def test_should_not_have_any_stored_preferences
class PreferencesReaderTest < ModelPreferenceTest
def setup
User.preference :notifications, :default => true
+ User.preference :rate, :float, :default => 1.0
@user = create_user
end
@@ -261,6 +282,8 @@ def test_should_use_stored_value_if_stored
def test_should_type_cast_based_on_preference_definition
@user.write_preference(:notifications, 'false')
assert_equal false, @user.preferred(:notifications)
+ @user.write_preference(:rate, "1.2")
+ assert_equal 1.2, @user.preferred(:rate)
end
def test_should_cache_stored_values
View
23 test/unit/preference_test.rb
@@ -212,6 +212,29 @@ def teardown
end
end
+class PreferenceWithFloatTypeTest < ActiveSupport::TestCase
+ def setup
+ User.preference :rate, :float, :default => 10.0
+ end
+
+ def test_should_type_cast_nil_values
+ preference = new_preference(:name => 'rate', :value => nil)
+ assert_nil preference.value
+ end
+
+ def test_should_type_cast_numeric_values
+ preference = new_preference(:name => 'rate', :value => 1.0)
+ assert_equal 1.0, preference.value
+
+ preference.value = "1.1"
+ assert_equal 1.1, preference.value
+ end
+
+ def teardown
+ User.preference_definitions.delete('rate')
+ end
+end
+
class PreferenceWithSTIOwnerTest < ActiveSupport::TestCase
def setup
@manager = create_manager

No commit comments for this range

Something went wrong with that request. Please try again.