setler 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/generators/setler/setler_generator.rb +5 -5
- data/lib/generators/setler/templates/migration.rb +1 -1
- data/lib/setler/active_record.rb +2 -2
- data/lib/setler/scoped_settings.rb +3 -3
- data/lib/setler/settings.rb +8 -9
- data/lib/setler/version.rb +1 -1
- data/test/settings_test.rb +27 -21
- data/test/test_helper.rb +2 -2
- metadata +2 -2
@@ -2,13 +2,13 @@ require 'rails/generators/migration'
|
|
2
2
|
|
3
3
|
class SetlerGenerator < Rails::Generators::NamedBase
|
4
4
|
include Rails::Generators::Migration
|
5
|
-
|
5
|
+
|
6
6
|
argument :name, type: :string, default: "settings"
|
7
|
-
|
7
|
+
|
8
8
|
source_root File.expand_path('../templates', __FILE__)
|
9
|
-
|
9
|
+
|
10
10
|
@@migrations = false
|
11
|
-
|
11
|
+
|
12
12
|
def self.next_migration_number(dirname)
|
13
13
|
if ActiveRecord::Base.timestamped_migrations
|
14
14
|
if @@migrations
|
@@ -21,7 +21,7 @@ class SetlerGenerator < Rails::Generators::NamedBase
|
|
21
21
|
"%.3d" % (current_migration_number(dirname) + 1)
|
22
22
|
end
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def generate_model
|
26
26
|
template "model.rb", File.join("app/models",class_path,"#{file_name}.rb"), force: true
|
27
27
|
migration_template "migration.rb", "db/migrate/setler_create_#{table_name}.rb"
|
data/lib/setler/active_record.rb
CHANGED
data/lib/setler/settings.rb
CHANGED
@@ -3,9 +3,8 @@ module Setler
|
|
3
3
|
serialize :value
|
4
4
|
self.abstract_class = true
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
end
|
6
|
+
cattr_accessor :defaults
|
7
|
+
@@defaults = {}.with_indifferent_access
|
9
8
|
|
10
9
|
# Get and Set variables when the calling method is the variable name
|
11
10
|
def self.method_missing(method, *args, &block)
|
@@ -23,7 +22,7 @@ module Setler
|
|
23
22
|
|
24
23
|
def self.[](var)
|
25
24
|
the_setting = thing_scoped.find_by_var(var.to_s)
|
26
|
-
the_setting.present? ? the_setting.value : defaults[var]
|
25
|
+
the_setting.present? ? the_setting.value : @@defaults[var]
|
27
26
|
end
|
28
27
|
|
29
28
|
def self.[]=(var, value)
|
@@ -47,15 +46,15 @@ module Setler
|
|
47
46
|
raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
|
48
47
|
end
|
49
48
|
end
|
50
|
-
|
49
|
+
|
51
50
|
def self.all
|
52
|
-
defaults.merge(Hash[thing_scoped.all.collect{ |s| [s.var, s.value] }])
|
51
|
+
@@defaults.merge(Hash[thing_scoped.all.collect{ |s| [s.var, s.value] }])
|
53
52
|
end
|
54
|
-
|
53
|
+
|
55
54
|
def self.thing_scoped
|
56
55
|
self.where(thing_type: nil, thing_id: nil)
|
57
56
|
end
|
58
|
-
|
57
|
+
|
59
58
|
end
|
60
|
-
|
59
|
+
|
61
60
|
end
|
data/lib/setler/version.rb
CHANGED
data/test/settings_test.rb
CHANGED
@@ -2,41 +2,41 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class ::SettingsTest < Test::Unit::TestCase
|
4
4
|
setup_db
|
5
|
-
|
5
|
+
|
6
6
|
def setup
|
7
7
|
::Settings.create(:var => 'test', :value => 'foo')
|
8
8
|
::Settings.create(:var => 'test2', :value => 'bar')
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def teardown
|
12
12
|
::Settings.delete_all
|
13
13
|
::Preferences.delete_all
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
def test_defaults
|
17
17
|
::Settings.defaults[:foo] = 'default foo'
|
18
|
-
|
18
|
+
|
19
19
|
assert_equal 'default foo', ::Settings.foo
|
20
|
-
|
20
|
+
|
21
21
|
::Settings.foo = 'bar'
|
22
22
|
assert_equal 'bar', ::Settings.foo
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def tests_defaults_false
|
26
26
|
::Settings.defaults[:foo] = false
|
27
27
|
assert_equal false, ::Settings.foo
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
def test_get
|
31
31
|
assert_equal 'foo', ::Settings.test
|
32
32
|
assert_equal 'bar', ::Settings.test2
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
def test_get_with_array_syntax
|
36
36
|
assert_equal 'foo', ::Settings["test"]
|
37
37
|
assert_equal 'bar', ::Settings[:test2]
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
def test_update
|
41
41
|
::Settings.test = '321'
|
42
42
|
assert_equal '321', ::Settings.test
|
@@ -56,33 +56,33 @@ class ::SettingsTest < Test::Unit::TestCase
|
|
56
56
|
def test_update_with_array_syntax
|
57
57
|
::Settings["test"] = '321'
|
58
58
|
assert_equal '321', ::Settings.test
|
59
|
-
|
59
|
+
|
60
60
|
::Settings[:test] = '567'
|
61
61
|
assert_equal '567', ::Settings.test
|
62
62
|
end
|
63
|
-
|
63
|
+
|
64
64
|
def test_create
|
65
65
|
::Settings.onetwothree = '123'
|
66
66
|
assert_equal '123', ::Settings.onetwothree
|
67
67
|
end
|
68
|
-
|
68
|
+
|
69
69
|
def test_complex_serialization
|
70
70
|
complex = [1, '2', {:three => true}]
|
71
71
|
::Settings.complex = complex
|
72
72
|
assert_equal complex, ::Settings.complex
|
73
73
|
end
|
74
|
-
|
74
|
+
|
75
75
|
def test_serialization_of_float
|
76
76
|
::Settings.float = 0.01
|
77
77
|
::Settings.reload
|
78
78
|
assert_equal 0.01, ::Settings.float
|
79
79
|
assert_equal 0.02, ::Settings.float * 2
|
80
80
|
end
|
81
|
-
|
81
|
+
|
82
82
|
def test_all
|
83
83
|
assert_equal({ "test2" => "bar", "test" => "foo" }, ::Settings.all)
|
84
84
|
end
|
85
|
-
|
85
|
+
|
86
86
|
def test_destroy
|
87
87
|
assert_not_nil ::Settings.test
|
88
88
|
::Settings.destroy :test
|
@@ -101,7 +101,7 @@ class ::SettingsTest < Test::Unit::TestCase
|
|
101
101
|
::Settings.testing = '123'
|
102
102
|
assert_nil ::Preferences.testing
|
103
103
|
end
|
104
|
-
|
104
|
+
|
105
105
|
def test_user_has_setler
|
106
106
|
user = User.create name: 'user 1'
|
107
107
|
assert_nil user.preferences.likes_bacon
|
@@ -110,7 +110,7 @@ class ::SettingsTest < Test::Unit::TestCase
|
|
110
110
|
user.preferences.destroy :likes_bacon
|
111
111
|
assert_nil user.preferences.likes_bacon
|
112
112
|
end
|
113
|
-
|
113
|
+
|
114
114
|
def test_user_settings_all
|
115
115
|
::Settings.destroy_all
|
116
116
|
user = User.create name: 'user 1'
|
@@ -122,7 +122,7 @@ class ::SettingsTest < Test::Unit::TestCase
|
|
122
122
|
assert user.preferences.all['really_likes_bacon']
|
123
123
|
assert !::Settings.all['really_likes_bacon']
|
124
124
|
end
|
125
|
-
|
125
|
+
|
126
126
|
def test_user_settings_override_defaults
|
127
127
|
::Settings.defaults[:foo] = false
|
128
128
|
user = User.create name: 'user 1'
|
@@ -132,7 +132,13 @@ class ::SettingsTest < Test::Unit::TestCase
|
|
132
132
|
user.preferences.foo = false
|
133
133
|
assert !user.preferences.foo
|
134
134
|
end
|
135
|
-
|
135
|
+
|
136
|
+
def test_user_preferences_has_defaults
|
137
|
+
::Preferences.defaults[:foo] = true
|
138
|
+
user = User.create name: 'user 1'
|
139
|
+
assert user.preferences.foo
|
140
|
+
end
|
141
|
+
|
136
142
|
# def test_user_has_settings_for
|
137
143
|
# user1 = User.create name: 'awesome user'
|
138
144
|
# user2 = User.create name: 'bad user'
|
@@ -148,8 +154,8 @@ class ::SettingsTest < Test::Unit::TestCase
|
|
148
154
|
end
|
149
155
|
|
150
156
|
def test_implementations_are_independent
|
151
|
-
::Preferences.create
|
152
|
-
::Preferences.create
|
157
|
+
::Preferences.create var: 'test', value: 'preferences foo'
|
158
|
+
::Preferences.create var: 'test2', value: 'preferences bar'
|
153
159
|
|
154
160
|
assert_not_equal ::Settings.defaults, ::Preferences.defaults
|
155
161
|
|
data/test/test_helper.rb
CHANGED
@@ -37,7 +37,7 @@ def setup_db
|
|
37
37
|
t.timestamps
|
38
38
|
end
|
39
39
|
add_index :settings, [ :thing_type, :thing_id, :var ], :unique => true
|
40
|
-
|
40
|
+
|
41
41
|
create_table :preferences do |t|
|
42
42
|
t.string :var, :null => false
|
43
43
|
t.text :value, :null => true
|
@@ -46,7 +46,7 @@ def setup_db
|
|
46
46
|
t.timestamps
|
47
47
|
end
|
48
48
|
add_index :preferences, [ :thing_type, :thing_id, :var ], :unique => true
|
49
|
-
|
49
|
+
|
50
50
|
create_table :users do |t|
|
51
51
|
t.string :name
|
52
52
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: setler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|