ledermann-rails-settings 2.4.3 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,103 +5,116 @@ module RailsSettings
5
5
  end
6
6
 
7
7
  describe Configuration, 'successful' do
8
- it "should define single key" do
8
+ it 'should define single key' do
9
9
  Configuration.new(Dummy, :dashboard)
10
10
 
11
- expect(Dummy.default_settings).to eq({ :dashboard => {} })
12
- expect(Dummy.setting_object_class_name).to eq('RailsSettings::SettingObject')
11
+ expect(Dummy.default_settings).to eq({ dashboard: {} })
12
+ expect(Dummy.setting_object_class_name).to eq(
13
+ 'RailsSettings::SettingObject',
14
+ )
13
15
  end
14
16
 
15
- it "should define multiple keys" do
17
+ it 'should define multiple keys' do
16
18
  Configuration.new(Dummy, :dashboard, :calendar)
17
19
 
18
- expect(Dummy.default_settings).to eq({ :dashboard => {}, :calendar => {} })
19
- expect(Dummy.setting_object_class_name).to eq('RailsSettings::SettingObject')
20
+ expect(Dummy.default_settings).to eq({ dashboard: {}, calendar: {} })
21
+ expect(Dummy.setting_object_class_name).to eq(
22
+ 'RailsSettings::SettingObject',
23
+ )
20
24
  end
21
25
 
22
- it "should define single key with class_name" do
23
- Configuration.new(Dummy, :dashboard, :class_name => 'MyClass')
24
- expect(Dummy.default_settings).to eq({ :dashboard => {} })
26
+ it 'should define single key with class_name' do
27
+ Configuration.new(Dummy, :dashboard, class_name: 'MyClass')
28
+ expect(Dummy.default_settings).to eq({ dashboard: {} })
25
29
  expect(Dummy.setting_object_class_name).to eq('MyClass')
26
30
  end
27
31
 
28
- it "should define multiple keys with class_name" do
29
- Configuration.new(Dummy, :dashboard, :calendar, :class_name => 'MyClass')
32
+ it 'should define multiple keys with class_name' do
33
+ Configuration.new(Dummy, :dashboard, :calendar, class_name: 'MyClass')
30
34
 
31
- expect(Dummy.default_settings).to eq({ :dashboard => {}, :calendar => {} })
35
+ expect(Dummy.default_settings).to eq({ dashboard: {}, calendar: {} })
32
36
  expect(Dummy.setting_object_class_name).to eq('MyClass')
33
37
  end
34
38
 
35
- it "should define using block" do
39
+ it 'should define using block' do
36
40
  Configuration.new(Dummy) do |c|
37
41
  c.key :dashboard
38
42
  c.key :calendar
39
43
  end
40
44
 
41
- expect(Dummy.default_settings).to eq({ :dashboard => {}, :calendar => {} })
42
- expect(Dummy.setting_object_class_name).to eq('RailsSettings::SettingObject')
45
+ expect(Dummy.default_settings).to eq({ dashboard: {}, calendar: {} })
46
+ expect(Dummy.setting_object_class_name).to eq(
47
+ 'RailsSettings::SettingObject',
48
+ )
43
49
  end
44
50
 
45
- it "should define using block with defaults" do
51
+ it 'should define using block with defaults' do
46
52
  Configuration.new(Dummy) do |c|
47
- c.key :dashboard, :defaults => { :theme => 'red' }
48
- c.key :calendar, :defaults => { :scope => 'all' }
53
+ c.key :dashboard, defaults: { theme: 'red' }
54
+ c.key :calendar, defaults: { scope: 'all' }
49
55
  end
50
56
 
51
- expect(Dummy.default_settings).to eq({ :dashboard => { 'theme' => 'red' }, :calendar => { 'scope' => 'all'} })
52
- expect(Dummy.setting_object_class_name).to eq('RailsSettings::SettingObject')
57
+ expect(Dummy.default_settings).to eq(
58
+ { dashboard: { 'theme' => 'red' }, calendar: { 'scope' => 'all' } },
59
+ )
60
+ expect(Dummy.setting_object_class_name).to eq(
61
+ 'RailsSettings::SettingObject',
62
+ )
53
63
  end
54
64
 
55
- it "should define using block and class_name" do
56
- Configuration.new(Dummy, :class_name => 'MyClass') do |c|
65
+ it 'should define using block and class_name' do
66
+ Configuration.new(Dummy, class_name: 'MyClass') do |c|
57
67
  c.key :dashboard
58
68
  c.key :calendar
59
69
  end
60
70
 
61
- expect(Dummy.default_settings).to eq({ :dashboard => {}, :calendar => {} })
71
+ expect(Dummy.default_settings).to eq({ dashboard: {}, calendar: {} })
62
72
  expect(Dummy.setting_object_class_name).to eq('MyClass')
63
73
  end
74
+
75
+ context 'persistent' do
76
+ it 'should keep settings between multiple configurations initialization' do
77
+ Configuration.new(Dummy, persistent: true) do |c|
78
+ c.key :dashboard, defaults: { theme: 'red' }
79
+ end
80
+
81
+ Configuration.new(Dummy, :calendar, persistent: true)
82
+
83
+ expect(Dummy.default_settings).to eq(
84
+ { dashboard: { 'theme' => 'red' }, calendar: {} },
85
+ )
86
+ end
87
+ end
64
88
  end
65
89
 
66
90
  describe Configuration, 'failure' do
67
- it "should fail without args" do
68
- expect {
69
- Configuration.new
70
- }.to raise_error(ArgumentError)
91
+ it 'should fail without args' do
92
+ expect { Configuration.new }.to raise_error(ArgumentError)
71
93
  end
72
94
 
73
- it "should fail without keys" do
74
- expect {
75
- Configuration.new(Dummy)
76
- }.to raise_error(ArgumentError)
95
+ it 'should fail without keys' do
96
+ expect { Configuration.new(Dummy) }.to raise_error(ArgumentError)
77
97
  end
78
98
 
79
- it "should fail without keys in block" do
80
- expect {
81
- Configuration.new(Dummy) do |c|
82
- end
83
- }.to raise_error(ArgumentError)
99
+ it 'should fail without keys in block' do
100
+ expect { Configuration.new(Dummy) { |c| } }.to raise_error(ArgumentError)
84
101
  end
85
102
 
86
- it "should fail with keys not being symbols" do
87
- expect {
88
- Configuration.new(Dummy, 42, "string")
89
- }.to raise_error(ArgumentError)
103
+ it 'should fail with keys not being symbols' do
104
+ expect { Configuration.new(Dummy, 42, 'string') }.to raise_error(
105
+ ArgumentError,
106
+ )
90
107
  end
91
108
 
92
- it "should fail with keys not being symbols" do
109
+ it 'should fail with keys not being symbols' do
93
110
  expect {
94
- Configuration.new(Dummy) do |c|
95
- c.key 42, "string"
96
- end
111
+ Configuration.new(Dummy) { |c| c.key 42, 'string' }
97
112
  }.to raise_error(ArgumentError)
98
113
  end
99
114
 
100
- it "should fail with unknown option" do
115
+ it 'should fail with unknown option' do
101
116
  expect {
102
- Configuration.new(Dummy) do |c|
103
- c.key :dashboard, :foo => {}
104
- end
117
+ Configuration.new(Dummy) { |c| c.key :dashboard, foo: {} }
105
118
  }.to raise_error(ArgumentError)
106
119
  end
107
120
  end
data/spec/database.yml CHANGED
@@ -1,3 +1,3 @@
1
1
  sqlite:
2
2
  adapter: sqlite3
3
- database: ":memory:"
3
+ database: ':memory:'
data/spec/queries_spec.rb CHANGED
@@ -2,12 +2,10 @@ require 'spec_helper'
2
2
 
3
3
  describe 'Queries performed' do
4
4
  context 'New record' do
5
- let!(:user) { User.new :name => 'Mr. Pink' }
5
+ let!(:user) { User.new name: 'Mr. Pink' }
6
6
 
7
7
  it 'should be saved by one SQL query' do
8
- expect {
9
- user.save!
10
- }.to perform_queries(1)
8
+ expect { user.save! }.to perform_queries(1)
11
9
  end
12
10
 
13
11
  it 'should be saved with settings for one key by two SQL queries' do
@@ -29,12 +27,10 @@ describe 'Queries performed' do
29
27
  end
30
28
 
31
29
  context 'Existing record without settings' do
32
- let!(:user) { User.create! :name => 'Mr. Pink' }
30
+ let!(:user) { User.create! name: 'Mr. Pink' }
33
31
 
34
32
  it 'should be saved without SQL queries' do
35
- expect {
36
- user.save!
37
- }.to perform_queries(0)
33
+ expect { user.save! }.to perform_queries(0)
38
34
  end
39
35
 
40
36
  it 'should be saved with settings for one key by two SQL queries' do
@@ -57,16 +53,14 @@ describe 'Queries performed' do
57
53
 
58
54
  context 'Existing record with settings' do
59
55
  let!(:user) do
60
- User.create! :name => 'Mr. Pink' do |user|
56
+ User.create! name: 'Mr. Pink' do |user|
61
57
  user.settings(:dashboard).theme = 'pink'
62
58
  user.settings(:calendar).scope = 'all'
63
59
  end
64
60
  end
65
61
 
66
62
  it 'should be saved without SQL queries' do
67
- expect {
68
- user.save!
69
- }.to perform_queries(0)
63
+ expect { user.save! }.to perform_queries(0)
70
64
  end
71
65
 
72
66
  it 'should be saved with settings for one key by one SQL queries' do
@@ -87,14 +81,12 @@ describe 'Queries performed' do
87
81
  end
88
82
 
89
83
  it 'should be destroyed by two SQL queries' do
90
- expect {
91
- user.destroy
92
- }.to perform_queries(2)
84
+ expect { user.destroy }.to perform_queries(2)
93
85
  end
94
86
 
95
- it "should update settings by one SQL query" do
87
+ it 'should update settings by one SQL query' do
96
88
  expect {
97
- user.settings(:dashboard).update_attributes! :foo => 'bar'
89
+ user.settings(:dashboard).update! foo: 'bar'
98
90
  }.to perform_queries(1)
99
91
  end
100
92
  end
data/spec/scopes_spec.rb CHANGED
@@ -1,31 +1,39 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'scopes' do
4
- let!(:user1) { User.create! :name => 'Mr. White' do |user| user.settings(:dashboard).theme = 'white' end }
5
- let!(:user2) { User.create! :name => 'Mr. Blue' }
4
+ let!(:user1) do
5
+ User.create! name: 'Mr. White' do |user|
6
+ user.settings(:dashboard).theme = 'white'
7
+ end
8
+ end
9
+ let!(:user2) { User.create! name: 'Mr. Blue' }
6
10
 
7
- it "should find objects with existing settings" do
11
+ it 'should find objects with existing settings' do
8
12
  expect(User.with_settings).to eq([user1])
9
13
  end
10
14
 
11
- it "should find objects with settings for key" do
15
+ it 'should find objects with settings for key' do
12
16
  expect(User.with_settings_for(:dashboard)).to eq([user1])
13
17
  expect(User.with_settings_for(:foo)).to eq([])
14
18
  end
15
19
 
16
- it "should records without settings" do
20
+ it 'should records without settings' do
17
21
  expect(User.without_settings).to eq([user2])
18
22
  end
19
23
 
20
- it "should records without settings for key" do
24
+ it 'should records without settings for key' do
21
25
  expect(User.without_settings_for(:foo)).to eq([user1, user2])
22
26
  expect(User.without_settings_for(:dashboard)).to eq([user2])
23
27
  end
24
28
 
25
- it "should require symbol as key" do
26
- [ nil, "string", 42 ].each do |invalid_key|
27
- expect { User.without_settings_for(invalid_key) }.to raise_error(ArgumentError)
28
- expect { User.with_settings_for(invalid_key) }.to raise_error(ArgumentError)
29
+ it 'should require symbol as key' do
30
+ [nil, 'string', 42].each do |invalid_key|
31
+ expect { User.without_settings_for(invalid_key) }.to raise_error(
32
+ ArgumentError,
33
+ )
34
+ expect { User.with_settings_for(invalid_key) }.to raise_error(
35
+ ArgumentError,
36
+ )
29
37
  end
30
38
  end
31
39
  end
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "Serialization" do
3
+ describe 'Serialization' do
4
4
  let!(:user) do
5
- User.create! :name => 'Mr. White' do |user|
5
+ User.create! name: 'Mr. White' do |user|
6
6
  user.settings(:dashboard).theme = 'white'
7
7
  user.settings(:calendar).scope = 'all'
8
8
  end
@@ -12,29 +12,31 @@ describe "Serialization" do
12
12
  it 'should be serialized' do
13
13
  user.reload
14
14
 
15
- dashboard_settings = user.setting_objects.where(:var => 'dashboard').first
16
- calendar_settings = user.setting_objects.where(:var => 'calendar').first
15
+ dashboard_settings = user.setting_objects.where(var: 'dashboard').first
16
+ calendar_settings = user.setting_objects.where(var: 'calendar').first
17
17
 
18
18
  expect(dashboard_settings.var).to eq('dashboard')
19
- expect(dashboard_settings.value).to eq({'theme' => 'white'})
19
+ expect(dashboard_settings.value).to eq({ 'theme' => 'white' })
20
20
 
21
21
  expect(calendar_settings.var).to eq('calendar')
22
- expect(calendar_settings.value).to eq({'scope' => 'all'})
22
+ expect(calendar_settings.value).to eq({ 'scope' => 'all' })
23
23
  end
24
24
  end
25
25
 
26
26
  describe 'updated settings' do
27
27
  it 'should be serialized' do
28
- user.settings(:dashboard).update_attributes! :smart => true
28
+ user.settings(:dashboard).update! smart: true
29
29
 
30
- dashboard_settings = user.setting_objects.where(:var => 'dashboard').first
31
- calendar_settings = user.setting_objects.where(:var => 'calendar').first
30
+ dashboard_settings = user.setting_objects.where(var: 'dashboard').first
31
+ calendar_settings = user.setting_objects.where(var: 'calendar').first
32
32
 
33
33
  expect(dashboard_settings.var).to eq('dashboard')
34
- expect(dashboard_settings.value).to eq({'theme' => 'white', 'smart' => true})
34
+ expect(dashboard_settings.value).to eq(
35
+ { 'theme' => 'white', 'smart' => true },
36
+ )
35
37
 
36
38
  expect(calendar_settings.var).to eq('calendar')
37
- expect(calendar_settings.value).to eq({'scope' => 'all'})
39
+ expect(calendar_settings.value).to eq({ 'scope' => 'all' })
38
40
  end
39
41
  end
40
42
  end
@@ -1,78 +1,97 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe RailsSettings::SettingObject do
4
- let(:user) { User.create! :name => 'Mr. Pink' }
4
+ let(:user) { User.create! name: 'Mr. Pink' }
5
5
 
6
6
  if RailsSettings.can_protect_attributes?
7
- let(:new_setting_object) { user.setting_objects.build({ :var => 'dashboard'}, :without_protection => true) }
8
- let(:saved_setting_object) { user.setting_objects.create!({ :var => 'dashboard', :value => { 'theme' => 'pink', 'filter' => false}}, :without_protection => true) }
7
+ let(:new_setting_object) do
8
+ user.setting_objects.build({ var: 'dashboard' }, without_protection: true)
9
+ end
10
+ let(:saved_setting_object) do
11
+ user.setting_objects.create!(
12
+ { var: 'dashboard', value: { 'theme' => 'pink', 'filter' => false } },
13
+ without_protection: true,
14
+ )
15
+ end
9
16
  else
10
- let(:new_setting_object) { user.setting_objects.build({ :var => 'dashboard'}) }
11
- let(:saved_setting_object) { user.setting_objects.create!({ :var => 'dashboard', :value => { 'theme' => 'pink', 'filter' => false}}) }
17
+ let(:new_setting_object) do
18
+ user.setting_objects.build({ var: 'dashboard' })
19
+ end
20
+ let(:saved_setting_object) do
21
+ user.setting_objects.create!(
22
+ { var: 'dashboard', value: { 'theme' => 'pink', 'filter' => false } },
23
+ )
24
+ end
12
25
  end
13
26
 
14
- describe "serialization" do
15
- it "should have a hash default" do
27
+ describe 'serialization' do
28
+ it 'should have a hash default' do
16
29
  expect(RailsSettings::SettingObject.new.value).to eq({})
17
30
  end
18
31
  end
19
32
 
20
- describe "Getter and Setter" do
21
- context "on unsaved settings" do
22
- it "should respond to setters" do
33
+ describe 'Getter and Setter' do
34
+ context 'on unsaved settings' do
35
+ it 'should respond to setters' do
23
36
  expect(new_setting_object).to respond_to(:foo=)
24
37
  expect(new_setting_object).to respond_to(:bar=)
38
+ expect(new_setting_object).to respond_to(:x=)
25
39
  end
26
40
 
27
- it "should not respond to some getters" do
41
+ it 'should not respond to some getters' do
28
42
  expect { new_setting_object.foo! }.to raise_error(NoMethodError)
29
43
  expect { new_setting_object.foo? }.to raise_error(NoMethodError)
30
44
  end
31
45
 
32
- it "should not respond if a block is given" do
33
- expect {
34
- new_setting_object.foo do
35
- end
36
- }.to raise_error(NoMethodError)
46
+ it 'should not respond if a block is given' do
47
+ expect { new_setting_object.foo {} }.to raise_error(NoMethodError)
37
48
  end
38
49
 
39
- it "should not respond if params are given" do
50
+ it 'should not respond if params are given' do
40
51
  expect { new_setting_object.foo(42) }.to raise_error(NoMethodError)
41
- expect { new_setting_object.foo(42,43) }.to raise_error(NoMethodError)
52
+ expect { new_setting_object.foo(42, 43) }.to raise_error(NoMethodError)
42
53
  end
43
54
 
44
- it "should return nil for unknown attribute" do
55
+ it 'should return nil for unknown attribute' do
45
56
  expect(new_setting_object.foo).to eq(nil)
46
57
  expect(new_setting_object.bar).to eq(nil)
58
+ expect(new_setting_object.c).to eq(nil)
47
59
  end
48
60
 
49
- it "should return defaults" do
61
+ it 'should return defaults' do
50
62
  expect(new_setting_object.theme).to eq('blue')
51
63
  expect(new_setting_object.view).to eq('monthly')
52
64
  expect(new_setting_object.filter).to eq(true)
65
+ expect(new_setting_object.a).to eq('b')
53
66
  end
54
67
 
55
- it "should return defaults when using `try`" do
68
+ it 'should return defaults when using `try`' do
56
69
  expect(new_setting_object.try(:theme)).to eq('blue')
57
70
  expect(new_setting_object.try(:view)).to eq('monthly')
58
71
  expect(new_setting_object.try(:filter)).to eq(true)
59
72
  end
60
73
 
61
- it "should store different objects to value hash" do
74
+ it 'should return value from target method if proc is a default value' do
75
+ expect(new_setting_object.owner_name).to eq('Mr. Pink')
76
+ end
77
+
78
+ it 'should store different objects to value hash' do
62
79
  new_setting_object.integer = 42
63
- new_setting_object.float = 1.234
64
- new_setting_object.string = 'Hello, World!'
65
- new_setting_object.array = [ 1,2,3 ]
66
- new_setting_object.symbol = :foo
67
-
68
- expect(new_setting_object.value).to eq('integer' => 42,
69
- 'float' => 1.234,
70
- 'string' => 'Hello, World!',
71
- 'array' => [ 1,2,3 ],
72
- 'symbol' => :foo)
80
+ new_setting_object.float = 1.234
81
+ new_setting_object.string = 'Hello, World!'
82
+ new_setting_object.array = [1, 2, 3]
83
+ new_setting_object.symbol = :foo
84
+
85
+ expect(new_setting_object.value).to eq(
86
+ 'integer' => 42,
87
+ 'float' => 1.234,
88
+ 'string' => 'Hello, World!',
89
+ 'array' => [1, 2, 3],
90
+ 'symbol' => :foo,
91
+ )
73
92
  end
74
93
 
75
- it "should set and return attributes" do
94
+ it 'should set and return attributes' do
76
95
  new_setting_object.theme = 'pink'
77
96
  new_setting_object.foo = 42
78
97
  new_setting_object.bar = 'hello'
@@ -82,30 +101,30 @@ describe RailsSettings::SettingObject do
82
101
  expect(new_setting_object.bar).to eq('hello')
83
102
  end
84
103
 
85
- it "should set dirty trackers on change" do
104
+ it 'should set dirty trackers on change' do
86
105
  new_setting_object.theme = 'pink'
87
106
  expect(new_setting_object).to be_value_changed
88
107
  expect(new_setting_object).to be_changed
89
108
  end
90
109
  end
91
110
 
92
- context "on saved settings" do
93
- it "should not set dirty trackers on setting same value" do
111
+ context 'on saved settings' do
112
+ it 'should not set dirty trackers on setting same value' do
94
113
  saved_setting_object.theme = 'pink'
95
114
  expect(saved_setting_object).not_to be_value_changed
96
115
  expect(saved_setting_object).not_to be_changed
97
116
  end
98
117
 
99
- it "should delete key on assigning nil" do
118
+ it 'should delete key on assigning nil' do
100
119
  saved_setting_object.theme = nil
101
120
  expect(saved_setting_object.value).to eq({ 'filter' => false })
102
121
  end
103
122
  end
104
123
  end
105
124
 
106
- describe "update_attributes" do
125
+ describe 'update' do
107
126
  it 'should save' do
108
- expect(new_setting_object.update_attributes(:foo => 42, :bar => 'string')).to be_truthy
127
+ expect(new_setting_object.update(foo: 42, bar: 'string')).to be_truthy
109
128
  new_setting_object.reload
110
129
 
111
130
  expect(new_setting_object.foo).to eq(42)
@@ -115,12 +134,12 @@ describe RailsSettings::SettingObject do
115
134
  end
116
135
 
117
136
  it 'should not save blank hash' do
118
- expect(new_setting_object.update_attributes({})).to be_truthy
137
+ expect(new_setting_object.update({})).to be_truthy
119
138
  end
120
139
 
121
140
  if RailsSettings.can_protect_attributes?
122
141
  it 'should not allow changing protected attributes' do
123
- new_setting_object.update_attributes!(:var => 'calendar', :foo => 42)
142
+ new_setting_object.update!(var: 'calendar', foo: 42)
124
143
 
125
144
  expect(new_setting_object.var).to eq('dashboard')
126
145
  expect(new_setting_object.foo).to eq(42)
@@ -128,8 +147,8 @@ describe RailsSettings::SettingObject do
128
147
  end
129
148
  end
130
149
 
131
- describe "save" do
132
- it "should save" do
150
+ describe 'save' do
151
+ it 'should save' do
133
152
  new_setting_object.foo = 42
134
153
  new_setting_object.bar = 'string'
135
154
  expect(new_setting_object.save).to be_truthy
@@ -142,9 +161,9 @@ describe RailsSettings::SettingObject do
142
161
  end
143
162
  end
144
163
 
145
- describe "validation" do
146
- it "should not validate for unknown var" do
147
- new_setting_object.var = "unknown-var"
164
+ describe 'validation' do
165
+ it 'should not validate for unknown var' do
166
+ new_setting_object.var = 'unknown-var'
148
167
 
149
168
  expect(new_setting_object).not_to be_valid
150
169
  expect(new_setting_object.errors[:var]).to be_present