setler 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,9 +15,9 @@ module Setler
15
15
  else
16
16
  method_name = method.to_s
17
17
  if method_name.ends_with?("=")
18
- self.find_or_create_by_var(method_name[0..-2]).update_attribute(:value, args.first)
18
+ thing_scoped.find_or_create_by_var(method_name[0..-2]).update_attribute(:value, args.first)
19
19
  else
20
- self.find_by_var(method_name).try(:value) || @@defaults[method_name]
20
+ thing_scoped.find_by_var(method_name).try(:value) || @@defaults[method_name]
21
21
  end
22
22
  end
23
23
  end
@@ -33,7 +33,11 @@ module Setler
33
33
  end
34
34
 
35
35
  def self.all
36
- Hash[super.collect{ |s| [s.var, s.value] }].merge(@@defaults)
36
+ Hash[thing_scoped.all.collect{ |s| [s.var, s.value] }].merge(@@defaults)
37
+ end
38
+
39
+ def self.thing_scoped
40
+ self.where(thing_type: nil, thing_id: nil)
37
41
  end
38
42
  end
39
43
 
@@ -1,3 +1,3 @@
1
1
  module Setler
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -74,12 +74,25 @@ class Setler::SettingsTest < Test::Unit::TestCase
74
74
  end
75
75
 
76
76
  def test_user_settings_all
77
+ Setler::Settings.destroy_all
77
78
  user = User.create name: 'user 1'
78
- assert_equal user.preferences.all, Setler::Settings.all
79
+ assert_equal Setler::Settings.all, user.preferences.all
79
80
  user.preferences.likes_bacon = true
80
81
  user.preferences.really_likes_bacon = true
81
82
  assert user.preferences.all['likes_bacon']
83
+ assert !Setler::Settings.all['likes_bacon']
82
84
  assert user.preferences.all['really_likes_bacon']
85
+ assert !Setler::Settings.all['really_likes_bacon']
86
+ end
87
+
88
+ def test_user_settings_override_defaults
89
+ Setler::Settings.defaults[:foo] = false
90
+ user = User.create name: 'user 1'
91
+ assert !user.preferences.foo
92
+ user.preferences.foo = true
93
+ assert user.preferences.foo
94
+ user.preferences.foo = false
95
+ assert !user.preferences.foo
83
96
  end
84
97
 
85
98
  # def test_user_has_settings_for
data/test/test_helper.rb CHANGED
@@ -26,11 +26,11 @@ def setup_db
26
26
  create_table :settings do |t|
27
27
  t.string :var, :null => false
28
28
  t.text :value, :null => true
29
- t.integer :target_id, :null => true
30
- t.string :target_type, :limit => 30, :null => true
29
+ t.integer :thing_id, :null => true
30
+ t.string :thing_type, :limit => 30, :null => true
31
31
  t.timestamps
32
32
  end
33
- add_index :settings, [ :target_type, :target_id, :var ], :unique => true
33
+ add_index :settings, [ :thing_type, :thing_id, :var ], :unique => true
34
34
 
35
35
  create_table :users do |t|
36
36
  t.string :name
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.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-08-09 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70292790898140 !ruby/object:Gem::Requirement
16
+ requirement: &70271069152000 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70292790898140
24
+ version_requirements: *70271069152000
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rails
27
- requirement: &70292790897640 !ruby/object:Gem::Requirement
27
+ requirement: &70271069151500 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70292790897640
35
+ version_requirements: *70271069151500
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &70292790897260 !ruby/object:Gem::Requirement
38
+ requirement: &70271069151120 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70292790897260
46
+ version_requirements: *70271069151120
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &70292790896800 !ruby/object:Gem::Requirement
49
+ requirement: &70271069150660 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70292790896800
57
+ version_requirements: *70271069150660
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: cover_me
60
- requirement: &70292790896380 !ruby/object:Gem::Requirement
60
+ requirement: &70271069150240 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70292790896380
68
+ version_requirements: *70271069150240
69
69
  description: Setler is a Gem that lets one easily implement the "Feature Flags" pattern,
70
70
  or add settings to individual models. This is a cleanroom implementation of what
71
71
  the 'rails-settings' gem does. It's been forked all over the place, and my favorite