Permalink
Browse files

Merge pull request #17 from stevenharman/remove_trailing_whitespace

Kill trailing whitespace
  • Loading branch information...
2 parents e8ffb5a + a094133 commit 1cc24ac2e6c8cb031318ea792e3bcbf3cbad42b6 @ckdake committed Jun 28, 2012
@@ -2,13 +2,13 @@
class SetlerGenerator < Rails::Generators::NamedBase
include Rails::Generators::Migration
-
+
argument :name, type: :string, default: "settings"
-
+
source_root File.expand_path('../templates', __FILE__)
-
+
@@migrations = false
-
+
def self.next_migration_number(dirname)
if ActiveRecord::Base.timestamped_migrations
if @@migrations
@@ -21,7 +21,7 @@ def self.next_migration_number(dirname)
"%.3d" % (current_migration_number(dirname) + 1)
end
end
-
+
def generate_model
template "model.rb", File.join("app/models",class_path,"#{file_name}.rb"), force: true
migration_template "migration.rb", "db/migrate/setler_create_#{table_name}.rb"
@@ -7,7 +7,7 @@ def self.up
t.string :thing_type, limit: 30, null: true
t.timestamps
end
-
+
add_index :<%= table_name %>, [ :thing_type, :thing_id, :var ], unique: true
end
@@ -1,11 +1,11 @@
module Setler
module ActiveRecord
-
+
def has_setler(scopename = 'settings')
define_method scopename do
Setler::ScopedSettings.for_thing(self, scopename)
end
end
-
+
end
end
@@ -5,10 +5,10 @@ def self.for_thing(object, scopename)
@object = object
self
end
-
+
def self.thing_scoped
self.base_class.where(thing_type: @object.class.base_class.to_s, thing_id: @object.id)
end
-
+
end
-end
+end
View
@@ -2,11 +2,11 @@ module Setler
class Settings < ActiveRecord::Base
serialize :value
self.abstract_class = true
-
+
def self.defaults
@defaults ||= {}.with_indifferent_access
end
-
+
# Get and Set variables when the calling method is the variable name
def self.method_missing(method, *args, &block)
if respond_to?(method)
@@ -47,15 +47,15 @@ def self.destroy(var_name)
raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
end
end
-
+
def self.all
defaults.merge(Hash[thing_scoped.all.collect{ |s| [s.var, s.value] }])
end
-
+
def self.thing_scoped
self.where(thing_type: nil, thing_id: nil)
end
-
+
end
-
+
end
View
@@ -2,41 +2,41 @@
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
::Preferences.delete_all
end
-
+
def test_defaults
::Settings.defaults[:foo] = 'default foo'
-
+
assert_equal 'default foo', ::Settings.foo
-
+
::Settings.foo = 'bar'
assert_equal 'bar', ::Settings.foo
end
-
+
def tests_defaults_false
::Settings.defaults[:foo] = false
assert_equal false, ::Settings.foo
end
-
+
def test_get
assert_equal 'foo', ::Settings.test
assert_equal 'bar', ::Settings.test2
end
-
+
def test_get_with_array_syntax
assert_equal 'foo', ::Settings["test"]
assert_equal 'bar', ::Settings[:test2]
end
-
+
def test_update
::Settings.test = '321'
assert_equal '321', ::Settings.test
@@ -56,33 +56,33 @@ def test_update_with_nil_and_default_not_nil
def test_update_with_array_syntax
::Settings["test"] = '321'
assert_equal '321', ::Settings.test
-
+
::Settings[:test] = '567'
assert_equal '567', ::Settings.test
end
-
+
def test_create
::Settings.onetwothree = '123'
assert_equal '123', ::Settings.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_all
assert_equal({ "test2" => "bar", "test" => "foo" }, ::Settings.all)
end
-
+
def test_destroy
assert_not_nil ::Settings.test
::Settings.destroy :test
@@ -101,7 +101,7 @@ def test_multiple_settings_classes
::Settings.testing = '123'
assert_nil ::Preferences.testing
end
-
+
def test_user_has_setler
user = User.create name: 'user 1'
assert_nil user.preferences.likes_bacon
@@ -110,7 +110,7 @@ def test_user_has_setler
user.preferences.destroy :likes_bacon
assert_nil user.preferences.likes_bacon
end
-
+
def test_user_settings_all
::Settings.destroy_all
user = User.create name: 'user 1'
@@ -122,7 +122,7 @@ def test_user_settings_all
assert user.preferences.all['really_likes_bacon']
assert !::Settings.all['really_likes_bacon']
end
-
+
def test_user_settings_override_defaults
::Settings.defaults[:foo] = false
user = User.create name: 'user 1'
@@ -132,13 +132,13 @@ def test_user_settings_override_defaults
user.preferences.foo = false
assert !user.preferences.foo
end
-
+
def test_user_preferences_has_defaults
::Preferences.defaults[:foo] = true
user = User.create name: 'user 1'
assert user.preferences.foo
end
-
+
# def test_user_has_settings_for
# user1 = User.create name: 'awesome user'
# user2 = User.create name: 'bad user'
View
@@ -37,7 +37,7 @@ def setup_db
t.timestamps
end
add_index :settings, [ :thing_type, :thing_id, :var ], :unique => true
-
+
create_table :preferences do |t|
t.string :var, :null => false
t.text :value, :null => true
@@ -46,7 +46,7 @@ def setup_db
t.timestamps
end
add_index :preferences, [ :thing_type, :thing_id, :var ], :unique => true
-
+
create_table :users do |t|
t.string :name
end

0 comments on commit 1cc24ac

Please sign in to comment.