spreeference 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.md +26 -0
- data/README.md +233 -0
- data/Rakefile +26 -0
- data/app/models/spreeference/app_configuration.rb +9 -0
- data/app/models/spreeference/application_record.rb +16 -0
- data/app/models/spreeference/preference.rb +6 -0
- data/config/routes.rb +2 -0
- data/db/migrate/20170217094656_create_spreeference_preferences.rb +10 -0
- data/lib/generators/spreeference/install_generator.rb +17 -0
- data/lib/generators/templates/app_configuration.rb +6 -0
- data/lib/generators/templates/spreeference.rb +12 -0
- data/lib/spreeference.rb +16 -0
- data/lib/spreeference/configuration.rb +48 -0
- data/lib/spreeference/engine.rb +28 -0
- data/lib/spreeference/environment.rb +12 -0
- data/lib/spreeference/environment_extension.rb +23 -0
- data/lib/spreeference/preferable.rb +103 -0
- data/lib/spreeference/preferable_methods.rb +52 -0
- data/lib/spreeference/scoped_store.rb +34 -0
- data/lib/spreeference/store.rb +93 -0
- data/lib/spreeference/version.rb +3 -0
- data/lib/tasks/spreeference_tasks.rake +4 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/config/manifest.js +3 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/jobs/application_job.rb +2 -0
- data/spec/dummy/app/models/app_configuration.rb +6 -0
- data/spec/dummy/app/models/application_record.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +13 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +34 -0
- data/spec/dummy/bin/update +29 -0
- data/spec/dummy/config.ru +5 -0
- data/spec/dummy/config/application.rb +23 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/cable.yml +9 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +42 -0
- data/spec/dummy/config/environments/production.rb +73 -0
- data/spec/dummy/config/environments/test.rb +36 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +6 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/new_framework_defaults.rb +24 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/spreeference.rb +13 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/puma.rb +47 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/config/spring.rb +6 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/schema.rb +23 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +69 -0
- data/spec/dummy/log/test.log +1995 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/lib/spreeference/configuration_spec.rb +27 -0
- data/spec/lib/spreeference/preferable_spec.rb +344 -0
- data/spec/lib/spreeference/scoped_store_spec.rb +58 -0
- data/spec/lib/spreeference/store_spec.rb +46 -0
- data/spec/models/spreeference/preference_spec.rb +80 -0
- data/spec/rails_helper.rb +31 -0
- data/spec/spec_helper.rb +19 -0
- metadata +227 -0
@@ -0,0 +1,67 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>The page you were looking for doesn't exist (404)</title>
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
6
|
+
<style>
|
7
|
+
body {
|
8
|
+
background-color: #EFEFEF;
|
9
|
+
color: #2E2F30;
|
10
|
+
text-align: center;
|
11
|
+
font-family: arial, sans-serif;
|
12
|
+
margin: 0;
|
13
|
+
}
|
14
|
+
|
15
|
+
div.dialog {
|
16
|
+
width: 95%;
|
17
|
+
max-width: 33em;
|
18
|
+
margin: 4em auto 0;
|
19
|
+
}
|
20
|
+
|
21
|
+
div.dialog > div {
|
22
|
+
border: 1px solid #CCC;
|
23
|
+
border-right-color: #999;
|
24
|
+
border-left-color: #999;
|
25
|
+
border-bottom-color: #BBB;
|
26
|
+
border-top: #B00100 solid 4px;
|
27
|
+
border-top-left-radius: 9px;
|
28
|
+
border-top-right-radius: 9px;
|
29
|
+
background-color: white;
|
30
|
+
padding: 7px 12% 0;
|
31
|
+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
|
32
|
+
}
|
33
|
+
|
34
|
+
h1 {
|
35
|
+
font-size: 100%;
|
36
|
+
color: #730E15;
|
37
|
+
line-height: 1.5em;
|
38
|
+
}
|
39
|
+
|
40
|
+
div.dialog > p {
|
41
|
+
margin: 0 0 1em;
|
42
|
+
padding: 1em;
|
43
|
+
background-color: #F7F7F7;
|
44
|
+
border: 1px solid #CCC;
|
45
|
+
border-right-color: #999;
|
46
|
+
border-left-color: #999;
|
47
|
+
border-bottom-color: #999;
|
48
|
+
border-bottom-left-radius: 4px;
|
49
|
+
border-bottom-right-radius: 4px;
|
50
|
+
border-top-color: #DADADA;
|
51
|
+
color: #666;
|
52
|
+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
|
53
|
+
}
|
54
|
+
</style>
|
55
|
+
</head>
|
56
|
+
|
57
|
+
<body>
|
58
|
+
<!-- This file lives in public/404.html -->
|
59
|
+
<div class="dialog">
|
60
|
+
<div>
|
61
|
+
<h1>The page you were looking for doesn't exist.</h1>
|
62
|
+
<p>You may have mistyped the address or the page may have moved.</p>
|
63
|
+
</div>
|
64
|
+
<p>If you are the application owner check the logs for more information.</p>
|
65
|
+
</div>
|
66
|
+
</body>
|
67
|
+
</html>
|
@@ -0,0 +1,67 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>The change you wanted was rejected (422)</title>
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
6
|
+
<style>
|
7
|
+
body {
|
8
|
+
background-color: #EFEFEF;
|
9
|
+
color: #2E2F30;
|
10
|
+
text-align: center;
|
11
|
+
font-family: arial, sans-serif;
|
12
|
+
margin: 0;
|
13
|
+
}
|
14
|
+
|
15
|
+
div.dialog {
|
16
|
+
width: 95%;
|
17
|
+
max-width: 33em;
|
18
|
+
margin: 4em auto 0;
|
19
|
+
}
|
20
|
+
|
21
|
+
div.dialog > div {
|
22
|
+
border: 1px solid #CCC;
|
23
|
+
border-right-color: #999;
|
24
|
+
border-left-color: #999;
|
25
|
+
border-bottom-color: #BBB;
|
26
|
+
border-top: #B00100 solid 4px;
|
27
|
+
border-top-left-radius: 9px;
|
28
|
+
border-top-right-radius: 9px;
|
29
|
+
background-color: white;
|
30
|
+
padding: 7px 12% 0;
|
31
|
+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
|
32
|
+
}
|
33
|
+
|
34
|
+
h1 {
|
35
|
+
font-size: 100%;
|
36
|
+
color: #730E15;
|
37
|
+
line-height: 1.5em;
|
38
|
+
}
|
39
|
+
|
40
|
+
div.dialog > p {
|
41
|
+
margin: 0 0 1em;
|
42
|
+
padding: 1em;
|
43
|
+
background-color: #F7F7F7;
|
44
|
+
border: 1px solid #CCC;
|
45
|
+
border-right-color: #999;
|
46
|
+
border-left-color: #999;
|
47
|
+
border-bottom-color: #999;
|
48
|
+
border-bottom-left-radius: 4px;
|
49
|
+
border-bottom-right-radius: 4px;
|
50
|
+
border-top-color: #DADADA;
|
51
|
+
color: #666;
|
52
|
+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
|
53
|
+
}
|
54
|
+
</style>
|
55
|
+
</head>
|
56
|
+
|
57
|
+
<body>
|
58
|
+
<!-- This file lives in public/422.html -->
|
59
|
+
<div class="dialog">
|
60
|
+
<div>
|
61
|
+
<h1>The change you wanted was rejected.</h1>
|
62
|
+
<p>Maybe you tried to change something you didn't have access to.</p>
|
63
|
+
</div>
|
64
|
+
<p>If you are the application owner check the logs for more information.</p>
|
65
|
+
</div>
|
66
|
+
</body>
|
67
|
+
</html>
|
@@ -0,0 +1,66 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>We're sorry, but something went wrong (500)</title>
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
6
|
+
<style>
|
7
|
+
body {
|
8
|
+
background-color: #EFEFEF;
|
9
|
+
color: #2E2F30;
|
10
|
+
text-align: center;
|
11
|
+
font-family: arial, sans-serif;
|
12
|
+
margin: 0;
|
13
|
+
}
|
14
|
+
|
15
|
+
div.dialog {
|
16
|
+
width: 95%;
|
17
|
+
max-width: 33em;
|
18
|
+
margin: 4em auto 0;
|
19
|
+
}
|
20
|
+
|
21
|
+
div.dialog > div {
|
22
|
+
border: 1px solid #CCC;
|
23
|
+
border-right-color: #999;
|
24
|
+
border-left-color: #999;
|
25
|
+
border-bottom-color: #BBB;
|
26
|
+
border-top: #B00100 solid 4px;
|
27
|
+
border-top-left-radius: 9px;
|
28
|
+
border-top-right-radius: 9px;
|
29
|
+
background-color: white;
|
30
|
+
padding: 7px 12% 0;
|
31
|
+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
|
32
|
+
}
|
33
|
+
|
34
|
+
h1 {
|
35
|
+
font-size: 100%;
|
36
|
+
color: #730E15;
|
37
|
+
line-height: 1.5em;
|
38
|
+
}
|
39
|
+
|
40
|
+
div.dialog > p {
|
41
|
+
margin: 0 0 1em;
|
42
|
+
padding: 1em;
|
43
|
+
background-color: #F7F7F7;
|
44
|
+
border: 1px solid #CCC;
|
45
|
+
border-right-color: #999;
|
46
|
+
border-left-color: #999;
|
47
|
+
border-bottom-color: #999;
|
48
|
+
border-bottom-left-radius: 4px;
|
49
|
+
border-bottom-right-radius: 4px;
|
50
|
+
border-top-color: #DADADA;
|
51
|
+
color: #666;
|
52
|
+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
|
53
|
+
}
|
54
|
+
</style>
|
55
|
+
</head>
|
56
|
+
|
57
|
+
<body>
|
58
|
+
<!-- This file lives in public/500.html -->
|
59
|
+
<div class="dialog">
|
60
|
+
<div>
|
61
|
+
<h1>We're sorry, but something went wrong.</h1>
|
62
|
+
</div>
|
63
|
+
<p>If you are the application owner check the logs for more information.</p>
|
64
|
+
</div>
|
65
|
+
</body>
|
66
|
+
</html>
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Spreeference
|
2
|
+
describe Spreeference::Configuration, type: :model do
|
3
|
+
|
4
|
+
before :all do
|
5
|
+
class AppConfig < Spreeference::Configuration
|
6
|
+
preference :color, :string, default: :blue
|
7
|
+
end
|
8
|
+
@config = AppConfig.new
|
9
|
+
end
|
10
|
+
|
11
|
+
it "has named methods to access preferences" do
|
12
|
+
@config.color = 'orange'
|
13
|
+
expect(@config.color).to eq 'orange'
|
14
|
+
end
|
15
|
+
|
16
|
+
it "uses [ ] to access preferences" do
|
17
|
+
@config[:color] = 'red'
|
18
|
+
expect(@config[:color]).to eq 'red'
|
19
|
+
end
|
20
|
+
|
21
|
+
it "uses set/get to access preferences" do
|
22
|
+
@config.set :color, 'green'
|
23
|
+
expect(@config.get(:color)).to eq 'green'
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,344 @@
|
|
1
|
+
module Spreeference
|
2
|
+
describe Spreeference::Preferable, type: :model do
|
3
|
+
|
4
|
+
before :all do
|
5
|
+
class A
|
6
|
+
include Spreeference::Preferable
|
7
|
+
attr_reader :id
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@id = rand(999)
|
11
|
+
end
|
12
|
+
|
13
|
+
def preferences
|
14
|
+
@preferences ||= default_preferences
|
15
|
+
end
|
16
|
+
|
17
|
+
preference :color, :string, default: 'green'
|
18
|
+
end
|
19
|
+
|
20
|
+
class B < A
|
21
|
+
preference :flavor, :string
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
before :each do
|
26
|
+
@a = A.new
|
27
|
+
allow(@a).to receive_messages(persisted?: true)
|
28
|
+
@b = B.new
|
29
|
+
allow(@b).to receive_messages(persisted?: true)
|
30
|
+
|
31
|
+
# ensure we're persisting as that is the default
|
32
|
+
#
|
33
|
+
store = Spreeference::Store.instance
|
34
|
+
store.persistence = true
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "preference definitions" do
|
38
|
+
it "parent should not see child definitions" do
|
39
|
+
expect(@a.has_preference?(:color)).to be true
|
40
|
+
expect(@a.has_preference?(:flavor)).not_to be true
|
41
|
+
end
|
42
|
+
|
43
|
+
it "child should have parent and own definitions" do
|
44
|
+
expect(@b.has_preference?(:color)).to be true
|
45
|
+
expect(@b.has_preference?(:flavor)).to be true
|
46
|
+
end
|
47
|
+
|
48
|
+
it "instances have defaults" do
|
49
|
+
expect(@a.preferred_color).to eq 'green'
|
50
|
+
expect(@b.preferred_color).to eq 'green'
|
51
|
+
expect(@b.preferred_flavor).to be_nil
|
52
|
+
end
|
53
|
+
|
54
|
+
it "can be asked if it has a preference definition" do
|
55
|
+
expect(@a.has_preference?(:color)).to be true
|
56
|
+
expect(@a.has_preference?(:bad)).to be false
|
57
|
+
end
|
58
|
+
|
59
|
+
it "can be asked and raises" do
|
60
|
+
expect {
|
61
|
+
@a.has_preference! :flavor
|
62
|
+
}.to raise_error(NoMethodError, "flavor preference not defined")
|
63
|
+
end
|
64
|
+
|
65
|
+
it "has a type" do
|
66
|
+
expect(@a.preferred_color_type).to eq :string
|
67
|
+
expect(@a.preference_type(:color)).to eq :string
|
68
|
+
end
|
69
|
+
|
70
|
+
it "has a default" do
|
71
|
+
expect(@a.preferred_color_default).to eq 'green'
|
72
|
+
expect(@a.preference_default(:color)).to eq 'green'
|
73
|
+
end
|
74
|
+
|
75
|
+
it "raises if not defined" do
|
76
|
+
expect {
|
77
|
+
@a.get_preference :flavor
|
78
|
+
}.to raise_error(NoMethodError, "flavor preference not defined")
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "preference access" do
|
84
|
+
it "handles ghost methods for preferences" do
|
85
|
+
@a.preferred_color = 'blue'
|
86
|
+
expect(@a.preferred_color).to eq 'blue'
|
87
|
+
end
|
88
|
+
|
89
|
+
it "parent and child instances have their own prefs" do
|
90
|
+
@a.preferred_color = 'red'
|
91
|
+
@b.preferred_color = 'blue'
|
92
|
+
|
93
|
+
expect(@a.preferred_color).to eq 'red'
|
94
|
+
expect(@b.preferred_color).to eq 'blue'
|
95
|
+
end
|
96
|
+
|
97
|
+
it "raises when preference not defined" do
|
98
|
+
expect {
|
99
|
+
@a.set_preference(:bad, :bone)
|
100
|
+
}.to raise_exception(NoMethodError, "bad preference not defined")
|
101
|
+
end
|
102
|
+
|
103
|
+
it "builds a hash of preferences" do
|
104
|
+
@b.preferred_flavor = :strawberry
|
105
|
+
expect(@b.preferences[:flavor]).to eq 'strawberry'
|
106
|
+
expect(@b.preferences[:color]).to eq 'green' #default from A
|
107
|
+
end
|
108
|
+
|
109
|
+
it "builds a hash of preference defaults" do
|
110
|
+
expect(@b.default_preferences).to eq({
|
111
|
+
flavor: nil,
|
112
|
+
color: 'green'
|
113
|
+
})
|
114
|
+
end
|
115
|
+
|
116
|
+
context "converts integer preferences to integer values" do
|
117
|
+
before do
|
118
|
+
A.preference :is_integer, :integer
|
119
|
+
end
|
120
|
+
|
121
|
+
it "with strings" do
|
122
|
+
@a.set_preference(:is_integer, '3')
|
123
|
+
expect(@a.preferences[:is_integer]).to eq(3)
|
124
|
+
|
125
|
+
@a.set_preference(:is_integer, '')
|
126
|
+
expect(@a.preferences[:is_integer]).to eq(0)
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
context "converts decimal preferences to BigDecimal values" do
|
132
|
+
before do
|
133
|
+
A.preference :if_decimal, :decimal
|
134
|
+
end
|
135
|
+
|
136
|
+
it "returns a BigDecimal" do
|
137
|
+
@a.set_preference(:if_decimal, 3.3)
|
138
|
+
expect(@a.preferences[:if_decimal].class).to eq(BigDecimal)
|
139
|
+
end
|
140
|
+
|
141
|
+
it "with strings" do
|
142
|
+
@a.set_preference(:if_decimal, '3.3')
|
143
|
+
expect(@a.preferences[:if_decimal]).to eq(3.3)
|
144
|
+
|
145
|
+
@a.set_preference(:if_decimal, '')
|
146
|
+
expect(@a.preferences[:if_decimal]).to eq(0.0)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
context "converts boolean preferences to boolean values" do
|
151
|
+
before do
|
152
|
+
A.preference :is_boolean, :boolean, default: true
|
153
|
+
end
|
154
|
+
|
155
|
+
it "with strings" do
|
156
|
+
@a.set_preference(:is_boolean, '0')
|
157
|
+
expect(@a.preferences[:is_boolean]).to be false
|
158
|
+
@a.set_preference(:is_boolean, 'f')
|
159
|
+
expect(@a.preferences[:is_boolean]).to be false
|
160
|
+
@a.set_preference(:is_boolean, 't')
|
161
|
+
expect(@a.preferences[:is_boolean]).to be true
|
162
|
+
end
|
163
|
+
|
164
|
+
it "with integers" do
|
165
|
+
@a.set_preference(:is_boolean, 0)
|
166
|
+
expect(@a.preferences[:is_boolean]).to be false
|
167
|
+
@a.set_preference(:is_boolean, 1)
|
168
|
+
expect(@a.preferences[:is_boolean]).to be true
|
169
|
+
end
|
170
|
+
|
171
|
+
it "with an empty string" do
|
172
|
+
@a.set_preference(:is_boolean, '')
|
173
|
+
expect(@a.preferences[:is_boolean]).to be false
|
174
|
+
end
|
175
|
+
|
176
|
+
it "with an empty hash" do
|
177
|
+
@a.set_preference(:is_boolean, [])
|
178
|
+
expect(@a.preferences[:is_boolean]).to be false
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
context "converts array preferences to array values" do
|
183
|
+
before do
|
184
|
+
A.preference :is_array, :array, default: []
|
185
|
+
end
|
186
|
+
|
187
|
+
it "with arrays" do
|
188
|
+
@a.set_preference(:is_array, [])
|
189
|
+
expect(@a.preferences[:is_array]).to be_is_a(Array)
|
190
|
+
end
|
191
|
+
|
192
|
+
it "with string" do
|
193
|
+
@a.set_preference(:is_array, "string")
|
194
|
+
expect(@a.preferences[:is_array]).to be_is_a(Array)
|
195
|
+
end
|
196
|
+
|
197
|
+
it "with hash" do
|
198
|
+
@a.set_preference(:is_array, {})
|
199
|
+
expect(@a.preferences[:is_array]).to be_is_a(Array)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
context "converts hash preferences to hash values" do
|
204
|
+
before do
|
205
|
+
A.preference :is_hash, :hash, default: {}
|
206
|
+
end
|
207
|
+
|
208
|
+
it "with hash" do
|
209
|
+
@a.set_preference(:is_hash, {})
|
210
|
+
expect(@a.preferences[:is_hash]).to be_is_a(Hash)
|
211
|
+
end
|
212
|
+
|
213
|
+
it "with hash and keys are integers" do
|
214
|
+
@a.set_preference(:is_hash, {1 => 2, 3 => 4})
|
215
|
+
expect(@a.preferences[:is_hash]).to eql({1 => 2, 3 => 4})
|
216
|
+
end
|
217
|
+
|
218
|
+
it "with string" do
|
219
|
+
@a.set_preference(:is_hash, "{\"0\"=>{\"answer\"=>\"1\", \"value\"=>\"No\"}}")
|
220
|
+
expect(@a.preferences[:is_hash]).to be_is_a(Hash)
|
221
|
+
end
|
222
|
+
|
223
|
+
it "with boolean" do
|
224
|
+
@a.set_preference(:is_hash, false)
|
225
|
+
expect(@a.preferences[:is_hash]).to be_is_a(Hash)
|
226
|
+
@a.set_preference(:is_hash, true)
|
227
|
+
expect(@a.preferences[:is_hash]).to be_is_a(Hash)
|
228
|
+
end
|
229
|
+
|
230
|
+
it "with simple array" do
|
231
|
+
@a.set_preference(:is_hash, ["key", "value", "another key", "another value"])
|
232
|
+
expect(@a.preferences[:is_hash]).to be_is_a(Hash)
|
233
|
+
expect(@a.preferences[:is_hash]["key"]).to eq("value")
|
234
|
+
expect(@a.preferences[:is_hash]["another key"]).to eq("another value")
|
235
|
+
end
|
236
|
+
|
237
|
+
it "with a nested array" do
|
238
|
+
@a.set_preference(:is_hash, [["key", "value"], ["another key", "another value"]])
|
239
|
+
expect(@a.preferences[:is_hash]).to be_is_a(Hash)
|
240
|
+
expect(@a.preferences[:is_hash]["key"]).to eq("value")
|
241
|
+
expect(@a.preferences[:is_hash]["another key"]).to eq("another value")
|
242
|
+
end
|
243
|
+
|
244
|
+
it "with single array" do
|
245
|
+
expect { @a.set_preference(:is_hash, ["key"]) }.to raise_error(ArgumentError)
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
context "converts any preferences to any values" do
|
250
|
+
before do
|
251
|
+
A.preference :product_ids, :any, default: []
|
252
|
+
A.preference :product_attributes, :any, default: {}
|
253
|
+
end
|
254
|
+
|
255
|
+
it "with array" do
|
256
|
+
expect(@a.preferences[:product_ids]).to eq([])
|
257
|
+
@a.set_preference(:product_ids, [1, 2])
|
258
|
+
expect(@a.preferences[:product_ids]).to eq([1, 2])
|
259
|
+
end
|
260
|
+
|
261
|
+
it "with hash" do
|
262
|
+
expect(@a.preferences[:product_attributes]).to eq({})
|
263
|
+
@a.set_preference(:product_attributes, {id: 1, name: 2})
|
264
|
+
expect(@a.preferences[:product_attributes]).to eq({id: 1, name: 2})
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
end
|
269
|
+
|
270
|
+
describe "persisted preferables" do
|
271
|
+
before(:all) do
|
272
|
+
class CreatePrefTest < ActiveRecord::Migration[4.2]
|
273
|
+
def self.up
|
274
|
+
create_table :pref_tests do |t|
|
275
|
+
t.string :col
|
276
|
+
t.text :preferences
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
def self.down
|
281
|
+
drop_table :pref_tests
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
@migration_verbosity = ActiveRecord::Migration.verbose
|
286
|
+
ActiveRecord::Migration.verbose = false
|
287
|
+
CreatePrefTest.migrate(:up)
|
288
|
+
|
289
|
+
class PrefTest < Spreeference::ApplicationRecord
|
290
|
+
preference :pref_test_pref, :string, default: 'abc'
|
291
|
+
preference :pref_test_any, :any, default: []
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
after(:all) do
|
296
|
+
CreatePrefTest.migrate(:down)
|
297
|
+
ActiveRecord::Migration.verbose = @migration_verbosity
|
298
|
+
end
|
299
|
+
|
300
|
+
before(:each) do
|
301
|
+
# load PrefTest table
|
302
|
+
PrefTest.first
|
303
|
+
@pt = PrefTest.create
|
304
|
+
end
|
305
|
+
|
306
|
+
describe "pending preferences for new activerecord objects" do
|
307
|
+
it "saves preferences after record is saved" do
|
308
|
+
pr = PrefTest.new
|
309
|
+
pr.set_preference(:pref_test_pref, 'XXX')
|
310
|
+
expect(pr.get_preference(:pref_test_pref)).to eq('XXX')
|
311
|
+
pr.save!
|
312
|
+
expect(pr.get_preference(:pref_test_pref)).to eq('XXX')
|
313
|
+
end
|
314
|
+
|
315
|
+
it "saves preferences for serialized object" do
|
316
|
+
pr = PrefTest.new
|
317
|
+
pr.set_preference(:pref_test_any, [1, 2])
|
318
|
+
expect(pr.get_preference(:pref_test_any)).to eq([1, 2])
|
319
|
+
pr.save!
|
320
|
+
expect(pr.get_preference(:pref_test_any)).to eq([1, 2])
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
it "clear preferences" do
|
325
|
+
@pt.set_preference(:pref_test_pref, 'xyz')
|
326
|
+
expect(@pt.preferred_pref_test_pref).to eq('xyz')
|
327
|
+
@pt.clear_preferences
|
328
|
+
expect(@pt.preferred_pref_test_pref).to eq('abc')
|
329
|
+
end
|
330
|
+
|
331
|
+
it "clear preferences when record is deleted" do
|
332
|
+
@pt.save!
|
333
|
+
@pt.preferred_pref_test_pref = 'lmn'
|
334
|
+
@pt.save!
|
335
|
+
@pt.destroy
|
336
|
+
@pt1 = PrefTest.new(col: 'aaaa')
|
337
|
+
@pt1.id = @pt.id
|
338
|
+
@pt1.save!
|
339
|
+
expect(@pt1.get_preference(:pref_test_pref)).to eq('abc')
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
end
|
344
|
+
end
|