volt 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +56 -0
  3. data/CHANGELOG.md +4 -0
  4. data/Rakefile +0 -1
  5. data/app/volt/models/user.rb +1 -3
  6. data/app/volt/tasks/live_query/live_query.rb +1 -1
  7. data/app/volt/tasks/store_tasks.rb +4 -5
  8. data/app/volt/tasks/user_tasks.rb +1 -1
  9. data/lib/volt.rb +2 -6
  10. data/lib/volt/boot.rb +0 -2
  11. data/lib/volt/cli.rb +0 -1
  12. data/lib/volt/cli/asset_compile.rb +0 -2
  13. data/lib/volt/cli/console.rb +3 -4
  14. data/lib/volt/cli/generate.rb +3 -4
  15. data/lib/volt/cli/new_gem.rb +1 -3
  16. data/lib/volt/controllers/actions.rb +6 -6
  17. data/lib/volt/controllers/model_controller.rb +4 -8
  18. data/lib/volt/data_stores/base.rb +1 -2
  19. data/lib/volt/data_stores/data_store.rb +2 -2
  20. data/lib/volt/data_stores/mongo_driver.rb +3 -3
  21. data/lib/volt/extra_core/hash.rb +1 -1
  22. data/lib/volt/extra_core/inflector/inflections.rb +10 -2
  23. data/lib/volt/extra_core/logger.rb +1 -4
  24. data/lib/volt/extra_core/object.rb +1 -1
  25. data/lib/volt/models.rb +2 -4
  26. data/lib/volt/models/array_model.rb +3 -7
  27. data/lib/volt/models/associations.rb +5 -4
  28. data/lib/volt/models/buffer.rb +1 -2
  29. data/lib/volt/models/dirty.rb +2 -2
  30. data/lib/volt/models/errors.rb +1 -1
  31. data/lib/volt/models/listener_tracker.rb +1 -3
  32. data/lib/volt/models/model.rb +16 -20
  33. data/lib/volt/models/permissions.rb +15 -18
  34. data/lib/volt/models/persistors/array_store.rb +5 -10
  35. data/lib/volt/models/persistors/cookies.rb +2 -2
  36. data/lib/volt/models/persistors/model_store.rb +3 -9
  37. data/lib/volt/models/persistors/params.rb +1 -3
  38. data/lib/volt/models/persistors/query/normalizer.rb +3 -3
  39. data/lib/volt/models/persistors/query/query_listener.rb +1 -1
  40. data/lib/volt/models/persistors/store.rb +2 -2
  41. data/lib/volt/models/state_helpers.rb +1 -1
  42. data/lib/volt/models/state_manager.rb +4 -4
  43. data/lib/volt/models/url.rb +2 -6
  44. data/lib/volt/models/validations.rb +8 -13
  45. data/lib/volt/models/validators/numericality_validator.rb +2 -6
  46. data/lib/volt/models/validators/unique_validator.rb +1 -1
  47. data/lib/volt/models/validators/user_validation.rb +0 -1
  48. data/lib/volt/page/bindings/attribute_binding.rb +9 -19
  49. data/lib/volt/page/bindings/content_binding.rb +1 -1
  50. data/lib/volt/page/bindings/each_binding.rb +3 -4
  51. data/lib/volt/page/bindings/if_binding.rb +1 -1
  52. data/lib/volt/page/bindings/view_binding.rb +15 -22
  53. data/lib/volt/page/bindings/view_binding/controller_handler.rb +5 -7
  54. data/lib/volt/page/bindings/view_binding/view_lookup_for_path.rb +1 -2
  55. data/lib/volt/page/bindings/yield_binding.rb +1 -2
  56. data/lib/volt/page/channel.rb +3 -3
  57. data/lib/volt/page/document_events.rb +1 -1
  58. data/lib/volt/page/page.rb +3 -6
  59. data/lib/volt/page/path_string_renderer.rb +7 -8
  60. data/lib/volt/page/sub_context.rb +1 -3
  61. data/lib/volt/page/targets/binding_document/component_node.rb +1 -3
  62. data/lib/volt/page/tasks.rb +3 -5
  63. data/lib/volt/reactive/class_eventable.rb +2 -2
  64. data/lib/volt/reactive/computation.rb +7 -7
  65. data/lib/volt/reactive/dependency.rb +1 -1
  66. data/lib/volt/reactive/eventable.rb +3 -4
  67. data/lib/volt/reactive/reactive_accessors.rb +0 -1
  68. data/lib/volt/reactive/reactive_array.rb +4 -12
  69. data/lib/volt/router/routes.rb +1 -3
  70. data/lib/volt/server.rb +6 -6
  71. data/lib/volt/server/component_handler.rb +1 -1
  72. data/lib/volt/server/component_templates.rb +2 -2
  73. data/lib/volt/server/forking_server.rb +6 -9
  74. data/lib/volt/server/html_parser/component_view_scope.rb +2 -2
  75. data/lib/volt/server/html_parser/each_scope.rb +1 -1
  76. data/lib/volt/server/html_parser/sandlebars_parser.rb +2 -6
  77. data/lib/volt/server/html_parser/view_handler.rb +1 -1
  78. data/lib/volt/server/html_parser/view_parser.rb +1 -1
  79. data/lib/volt/server/html_parser/view_scope.rb +2 -4
  80. data/lib/volt/server/rack/asset_files.rb +1 -3
  81. data/lib/volt/server/rack/component_paths.rb +1 -3
  82. data/lib/volt/server/rack/http_request.rb +1 -3
  83. data/lib/volt/server/rack/http_resource.rb +1 -3
  84. data/lib/volt/server/rack/http_response_renderer.rb +2 -2
  85. data/lib/volt/server/rack/index_files.rb +1 -1
  86. data/lib/volt/server/rack/opal_files.rb +1 -3
  87. data/lib/volt/server/socket_connection_handler.rb +1 -3
  88. data/lib/volt/server/websocket/rack_server_adaptor.rb +1 -1
  89. data/lib/volt/server/websocket/websocket_handler.rb +1 -2
  90. data/lib/volt/spec/capybara.rb +1 -1
  91. data/lib/volt/spec/setup.rb +18 -12
  92. data/lib/volt/tasks/dispatcher.rb +1 -2
  93. data/lib/volt/tasks/task_handler.rb +1 -3
  94. data/lib/volt/utils/boolean_patch.rb +1 -1
  95. data/lib/volt/utils/event_counter.rb +2 -2
  96. data/lib/volt/utils/generic_pool.rb +5 -9
  97. data/lib/volt/utils/logging/task_argument_filterer.rb +4 -4
  98. data/lib/volt/utils/logging/task_logger.rb +2 -2
  99. data/lib/volt/utils/modes.rb +2 -2
  100. data/lib/volt/utils/promise_patch.rb +1 -2
  101. data/lib/volt/utils/read_write_lock.rb +21 -23
  102. data/lib/volt/utils/tilt_patch.rb +1 -1
  103. data/lib/volt/utils/timers.rb +3 -7
  104. data/lib/volt/utils/volt_user_error.rb +1 -1
  105. data/lib/volt/version.rb +2 -2
  106. data/lib/volt/volt/app.rb +4 -6
  107. data/lib/volt/volt/users.rb +4 -3
  108. data/spec/apps/kitchen_sink/app/main/controllers/main_controller.rb +1 -1
  109. data/spec/apps/kitchen_sink/app/main/controllers/server/simple_http_controller.rb +1 -1
  110. data/spec/apps/kitchen_sink/app/main/controllers/todos_controller.rb +5 -5
  111. data/spec/apps/kitchen_sink/app/main/controllers/upload_controller.rb +1 -1
  112. data/spec/apps/kitchen_sink/app/main/controllers/yield_component_controller.rb +1 -1
  113. data/spec/controllers/actions_spec.rb +9 -10
  114. data/spec/controllers/model_controller_spec.rb +1 -1
  115. data/spec/extra_core/blank_spec.rb +2 -2
  116. data/spec/extra_core/logger_spec.rb +6 -8
  117. data/spec/extra_core/string_transformations_spec.rb +3 -3
  118. data/spec/integration/bindings_spec.rb +3 -9
  119. data/spec/integration/first_last_spec.rb +1 -1
  120. data/spec/integration/missing_spec.rb +1 -2
  121. data/spec/integration/raw_html_binding.rb +2 -2
  122. data/spec/integration/todos_spec.rb +22 -0
  123. data/spec/integration/yield_spec.rb +2 -2
  124. data/spec/models/associations_spec.rb +4 -4
  125. data/spec/models/buffer_spec.rb +2 -2
  126. data/spec/models/dirty_spec.rb +10 -11
  127. data/spec/models/model_spec.rb +19 -13
  128. data/spec/models/model_state_spec.rb +1 -1
  129. data/spec/models/permissions_spec.rb +8 -12
  130. data/spec/models/url_spec.rb +1 -1
  131. data/spec/models/user_spec.rb +17 -10
  132. data/spec/models/user_validation_spec.rb +2 -3
  133. data/spec/models/validations_spec.rb +7 -10
  134. data/spec/models/validators/email_validator_spec.rb +5 -7
  135. data/spec/models/validators/format_validator_spec.rb +21 -3
  136. data/spec/models/validators/length_validator_spec.rb +12 -22
  137. data/spec/models/validators/shared_examples_for_validators.rb +1 -1
  138. data/spec/models/validators/unique_validator_spec.rb +1 -2
  139. data/spec/page/bindings/content_binding_spec.rb +1 -1
  140. data/spec/page/bindings/template_binding/view_lookup_for_path_spec.rb +0 -1
  141. data/spec/page/path_string_renderer_spec.rb +2 -3
  142. data/spec/reactive/class_eventable_spec.rb +1 -1
  143. data/spec/reactive/computation_spec.rb +30 -31
  144. data/spec/reactive/reactive_hash_spec.rb +1 -1
  145. data/spec/router/routes_spec.rb +14 -16
  146. data/spec/server/html_parser/view_handler_spec.rb +1 -1
  147. data/spec/server/html_parser/view_parser_spec.rb +0 -1
  148. data/spec/server/rack/quite_common_logger_spec.rb +3 -3
  149. data/spec/tasks/dispatcher_spec.rb +1 -1
  150. data/spec/tasks/query_tracker_spec.rb +1 -3
  151. data/spec/utils/task_argument_filtererer_spec.rb +5 -5
  152. data/templates/project/app/main/controllers/main_controller.rb +1 -1
  153. data/templates/project/spec/app/main/models/sample_model_spec.rb +2 -2
  154. data/templates/project/spec/app/main/tasks/sample_task_spec.rb +2 -2
  155. data/volt.gemspec +3 -5
  156. metadata +19 -4
@@ -201,7 +201,7 @@ describe Volt::Model do
201
201
  a = Volt::Model.new
202
202
 
203
203
  count = 0
204
- -> { a._blue ; count += 1 }.watch!
204
+ -> { a._blue; count += 1 }.watch!
205
205
 
206
206
  expect(count).to eq(1)
207
207
 
@@ -219,7 +219,7 @@ describe Volt::Model do
219
219
 
220
220
  expect(count).to eq(0)
221
221
 
222
- a._todos << {label: 'Be active'}
222
+ a._todos << { label: 'Be active' }
223
223
  Volt::Computation.flush!
224
224
 
225
225
  expect(count).to eq(1)
@@ -233,7 +233,7 @@ describe Volt::Model do
233
233
 
234
234
  expect(last_count).to eq(0)
235
235
 
236
- a._todos! << {checked: true}
236
+ a._todos! << { checked: true }
237
237
  Volt::Computation.flush!
238
238
 
239
239
  expect(last_count).to eq(1)
@@ -304,7 +304,7 @@ describe Volt::Model do
304
304
  # Expand first
305
305
  a._new_item!
306
306
  count = 0
307
- -> do
307
+ lambda do
308
308
  count += 1
309
309
  a._new_item._name
310
310
  end.watch!
@@ -459,7 +459,6 @@ describe Volt::Model do
459
459
  model.send(:"_#{attr_name}")
460
460
  end.to raise_error(Volt::InvalidFieldName, "`#{attr_name}` is reserved and can not be used as a field")
461
461
  end
462
-
463
462
  end
464
463
 
465
464
  it 'should prevent reserved attributes from being assigned directly' do
@@ -502,7 +501,7 @@ describe Volt::Model do
502
501
  expect(model._name).to eq('Jimmy')
503
502
  end
504
503
 
505
- describe "model state" do
504
+ describe 'model state' do
506
505
  it 'should be new when created, then false after a change' do
507
506
  a = Volt::Model.new
508
507
  expect(a.new?).to eq(true)
@@ -515,12 +514,11 @@ describe Volt::Model do
515
514
  a = Volt::Model.new
516
515
  expect(a.new?).to eq(true)
517
516
 
518
- a.attributes = {first: 'Jimmy', last: 'Dean'}
517
+ a.attributes = { first: 'Jimmy', last: 'Dean' }
519
518
  expect(a.new?).to eq(false)
520
519
  end
521
520
  end
522
521
 
523
-
524
522
  if RUBY_PLATFORM != 'opal'
525
523
  it 'should update other queries on the server when a new model is created' do
526
524
  query1 = store._todos!
@@ -529,21 +527,21 @@ describe Volt::Model do
529
527
  count = 0
530
528
 
531
529
  # count the number of todos
532
- query2.fetch {|v| count += v.size }
530
+ query2.fetch { |v| count += v.size }
533
531
 
534
532
  expect(count).to eq(0)
535
533
 
536
- query1 << {label: 'One'}
534
+ query1 << { label: 'One' }
537
535
 
538
536
  count = 0
539
- query2.fetch {|v| count += v.size }
537
+ query2.fetch { |v| count += v.size }
540
538
 
541
539
  expect(count).to eq(1)
542
540
  end
543
541
 
544
542
  it 'should query twice and return twice' do
545
- store._items << {name: 'One'}
546
- store._items << {name: 'Two'}
543
+ store._items << { name: 'One' }
544
+ store._items << { name: 'Two' }
547
545
 
548
546
  a = store._items.fetch.sync
549
547
  b = store._items.fetch.sync
@@ -554,4 +552,12 @@ describe Volt::Model do
554
552
  expect(a.to_a).to eq(b.to_a)
555
553
  end
556
554
  end
555
+
556
+ describe 'destroy' do
557
+ it 'fails if attempting to destroy while parentless' do
558
+ model = Volt::Model.new(test: "yeah")
559
+ expect { model.destroy }.to raise_error(RuntimeError,
560
+ 'Model does not have a parent and cannot be deleted.')
561
+ end
562
+ end
557
563
  end
@@ -21,4 +21,4 @@ if RUBY_PLATFORM != 'opal'
21
21
  expect(store._items.loaded_state).to eq(:dirty)
22
22
  end
23
23
  end
24
- end
24
+ end
@@ -1,6 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
-
4
3
  class TestUserTodo < Volt::Model
5
4
  own_by_user
6
5
 
@@ -47,7 +46,7 @@ class ::TestUpdateReadCheck < Volt::Model
47
46
  end
48
47
  end
49
48
 
50
- describe "model permissions" do
49
+ describe 'model permissions' do
51
50
  it 'should follow CRUD states when checking permissions' do
52
51
  todo = TestUserTodoWithCrudStates.new.buffer
53
52
 
@@ -59,26 +58,23 @@ describe "model permissions" do
59
58
  todo._name = 'Jimmy'
60
59
 
61
60
  todo.save!.then do
62
- spec_err = "should not have saved"
61
+ spec_err = 'should not have saved'
63
62
  end.fail do |err|
64
- expect(err).to eq({:name=>["can not be changed"]})
63
+ expect(err).to eq(name: ['can not be changed'])
65
64
  end
66
65
  end.fail do |err|
67
66
  spec_err = "Did not save because: #{err.inspect}"
68
67
  end
69
68
 
70
- if spec_err
71
- fail spec_err
72
- end
69
+ fail spec_err if spec_err
73
70
  end
74
71
 
75
72
  # it 'should deny an insert/create if a deny without fields' do
76
73
  # store._todos << {name: 'Ryan'}
77
74
  # end
78
75
 
79
-
80
76
  if RUBY_PLATFORM != 'opal'
81
- describe "read permissions" do
77
+ describe 'read permissions' do
82
78
  it 'should deny read on a field' do
83
79
  model = store._test_deny_read_names!.buffer
84
80
  model._name = 'Jimmy'
@@ -113,7 +109,7 @@ describe "model permissions" do
113
109
  end
114
110
 
115
111
  it 'should not check the read permissions when updating (so that all fields are present for the permissions check)' do
116
- model = store._test_update_read_checks!.append({name: 'Ryan'}).sync
112
+ model = store._test_update_read_checks!.append(name: 'Ryan').sync
117
113
 
118
114
  expect(model.create_check).to eq(true)
119
115
  expect(model.read_check).to eq(nil)
@@ -146,7 +142,7 @@ describe "model permissions" do
146
142
  end
147
143
 
148
144
  it 'should not check read on delete, so all fields are available to the permissions block' do
149
- model = store._test_update_read_checks!.append({name: 'Ryan'}).sync
145
+ model = store._test_update_read_checks!.append(name: 'Ryan').sync
150
146
 
151
147
  expect(model.read_check).to eq(nil)
152
148
 
@@ -155,4 +151,4 @@ describe "model permissions" do
155
151
  expect(model.read_check).to eq(nil)
156
152
  end
157
153
  end
158
- end
154
+ end
@@ -20,7 +20,7 @@ describe Volt::URL do
20
20
  subject { described_class.new fake_router }
21
21
 
22
22
  describe '#parse' do
23
- let(:fake_router) { double('Router', url_to_params: {foo: 'bar'}) }
23
+ let(:fake_router) { double('Router', url_to_params: { foo: 'bar' }) }
24
24
  context 'with a valid url' do
25
25
  it 'returns "http" for scheme' do
26
26
  expect(subject.scheme).to eq 'http'
@@ -2,9 +2,17 @@ require 'spec_helper'
2
2
  require 'volt/models/user'
3
3
 
4
4
  class FakeConfig
5
- def public; self; end
6
- def auth; self; end
7
- def use_username; true; end
5
+ def public
6
+ self
7
+ end
8
+
9
+ def auth
10
+ self
11
+ end
12
+
13
+ def use_username
14
+ true
15
+ end
8
16
  end
9
17
 
10
18
  describe Volt::User do
@@ -16,13 +24,13 @@ describe Volt::User do
16
24
  allow(Volt).to receive(:config).and_return FakeConfig.new
17
25
  end
18
26
 
19
- it "returns :username" do
27
+ it 'returns :username' do
20
28
  expect(subject).to eq :username
21
29
  end
22
30
  end
23
31
 
24
32
  describe 'when use_username is not set' do
25
- it "returns :email" do
33
+ it 'returns :email' do
26
34
  expect(subject).to eq :email
27
35
  end
28
36
  end
@@ -36,11 +44,11 @@ describe Volt::User do
36
44
  if RUBY_PLATFORM != 'opal'
37
45
  describe 'when it is a Volt server' do
38
46
  before do
39
- allow(BCrypt::Password).to receive(:create).with('test').
40
- and_return 'hashed-password'
47
+ allow(BCrypt::Password).to receive(:create).with('test')
48
+ .and_return 'hashed-password'
41
49
  end
42
50
 
43
- it "encrypts password" do
51
+ it 'encrypts password' do
44
52
  subject
45
53
 
46
54
  expect(BCrypt::Password).to have_received :create
@@ -49,7 +57,7 @@ describe Volt::User do
49
57
  it 'sets _hashed_password to passed value' do
50
58
  subject
51
59
 
52
- expect(user._hashed_password).to eq "hashed-password"
60
+ expect(user._hashed_password).to eq 'hashed-password'
53
61
  end
54
62
  end
55
63
  end
@@ -61,7 +69,6 @@ describe Volt::User do
61
69
 
62
70
  subject { user.password = 'a valid test password' }
63
71
 
64
-
65
72
  it 'sets _password to passed value' do
66
73
  subject
67
74
 
@@ -1,8 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
-
4
3
  describe Volt::UserValidatorHelpers do
5
- context "with user" do
4
+ context 'with user' do
6
5
  before do
7
6
  allow(Volt).to receive(:current_user_id) { 294 }
8
7
  end
@@ -21,4 +20,4 @@ describe Volt::UserValidatorHelpers do
21
20
  expect(todo._user_id).to eq(294)
22
21
  end
23
22
  end
24
- end
23
+ end
@@ -19,10 +19,9 @@ describe Volt::Model do
19
19
  Class.new(Volt::Model) do
20
20
  attr_accessor :ran_promise
21
21
  validate do
22
-
23
22
  Promise.new.then do
24
23
  self.ran_promise = true
25
- {name: 'Invalid'}
24
+ { name: 'Invalid' }
26
25
  end
27
26
  end
28
27
  end
@@ -54,7 +53,7 @@ describe Volt::Model do
54
53
  model = test_model_with_promises.new
55
54
 
56
55
  model.validate!.then do |errs|
57
- expect(errs).to eq({name: 'Invalid'})
56
+ expect(errs).to eq(name: 'Invalid')
58
57
  expect(model.ran_promise).to eq(true)
59
58
  end
60
59
  end
@@ -64,7 +63,7 @@ describe Volt::Model do
64
63
  specify do
65
64
  expect { model.mark_field! field }
66
65
  .to change { model.marked_errors }
67
- .from({}).to({ field => [message ] })
66
+ .from({}).to(field => [message])
68
67
  end
69
68
  end
70
69
 
@@ -112,7 +111,7 @@ describe Volt::Model do
112
111
  Class.new(Volt::Model) do
113
112
  validate :special_field, format: [
114
113
  { with: /regex/, message: 'regex failed' },
115
- { with: ->(x) {x == false}, message: 'proc failed' }
114
+ { with: ->(x) { x == false }, message: 'proc failed' }
116
115
  ]
117
116
  end
118
117
  end
@@ -121,9 +120,7 @@ describe Volt::Model do
121
120
  before { model._special_field = 'nope' }
122
121
 
123
122
  it 'returns an array of errors' do
124
- expect(model.errors).to eq({
125
- special_field: [ regex_message, proc_message ]
126
- })
123
+ expect(model.errors).to eq(special_field: [regex_message, proc_message])
127
124
  end
128
125
  end
129
126
 
@@ -135,7 +132,7 @@ describe Volt::Model do
135
132
  end
136
133
 
137
134
  it 'returns an array with a single error' do
138
- expect(model.errors.to_h).to eq({ special_field: [ proc_message ] })
135
+ expect(model.errors.to_h).to eq(special_field: [proc_message])
139
136
  end
140
137
  end
141
138
  end
@@ -167,4 +164,4 @@ describe Volt::Model do
167
164
  model._name = 'ok' # fails again
168
165
  expect(model._name).to eq('Jimmy')
169
166
  end
170
- end
167
+ end
@@ -41,7 +41,7 @@ describe Volt::Model do
41
41
  specify do
42
42
  expect { model.mark_field! field }
43
43
  .to change { model.marked_errors }
44
- .from({}).to({ field => [message ] })
44
+ .from({}).to(field => [message])
45
45
  end
46
46
  end
47
47
 
@@ -89,7 +89,7 @@ describe Volt::Model do
89
89
  Class.new(Volt::Model) do
90
90
  validate :special_field, format: [
91
91
  { with: /regex/, message: 'regex failed' },
92
- { with: ->(x) {x == false}, message: 'proc failed' }
92
+ { with: ->(x) { x == false }, message: 'proc failed' }
93
93
  ]
94
94
  end
95
95
  end
@@ -98,9 +98,7 @@ describe Volt::Model do
98
98
  before { model._special_field = 'nope' }
99
99
 
100
100
  it 'returns an array of errors' do
101
- expect(model.errors).to eq({
102
- special_field: [ regex_message, proc_message ]
103
- })
101
+ expect(model.errors).to eq(special_field: [regex_message, proc_message])
104
102
  end
105
103
  end
106
104
 
@@ -112,7 +110,7 @@ describe Volt::Model do
112
110
  end
113
111
 
114
112
  it 'returns an array with a single error' do
115
- expect(model.errors.to_h).to eq({ special_field: [ proc_message ] })
113
+ expect(model.errors.to_h).to eq(special_field: [proc_message])
116
114
  end
117
115
  end
118
116
  end
@@ -144,4 +142,4 @@ describe Volt::Model do
144
142
  model._name = 'ok' # fails again
145
143
  expect(model._name).to eq('Jimmy')
146
144
  end
147
- end
145
+ end
@@ -1,11 +1,19 @@
1
1
  require 'spec_helper'
2
2
  require 'models/validators/shared_examples_for_validators'
3
3
 
4
+ # test fake used for edge cases where default_options are not Hash types.
5
+ # Feel free to append stub methods to this class as needed for testing.
6
+ class SpecValidator < Volt::FormatValidator
7
+ def default_options
8
+ "No hash here, no sir!"
9
+ end
10
+ end
11
+
4
12
  describe Volt::FormatValidator do
5
13
  subject { described_class.new(*init_params) }
6
14
 
7
- let(:init_params) { [ model, field_name ] }
8
- let(:validate_params) { [ model, field_name, options ] }
15
+ let(:init_params) { [model, field_name] }
16
+ let(:validate_params) { [model, field_name, options] }
9
17
 
10
18
  let(:model) { Volt::Model.new field: field_content }
11
19
  let(:field_name) { :field }
@@ -38,4 +46,14 @@ describe Volt::FormatValidator do
38
46
 
39
47
  specify { expect(subject).to be_valid }
40
48
  end
41
- end
49
+
50
+ context 'when default_options is not a Hash' do
51
+ it 'returns the options hash instead of default_options' do
52
+ user = Volt::Model.new(email: "rick@nolematad.io")
53
+ validator = SpecValidator.new(user, 'email')
54
+ options = { with: /.+@.+/, message: 'must include an @ symbol' }
55
+ results = validator.apply(options).errors
56
+ expect(results.empty?).to be_truthy
57
+ end
58
+ end
59
+ end
@@ -6,23 +6,21 @@ describe Volt::LengthValidator do
6
6
 
7
7
  let(:model) { Volt::Model.new name: name }
8
8
  let(:field_name) { :name }
9
- let(:name) { "John Doe" }
9
+ let(:name) { 'John Doe' }
10
10
 
11
11
  describe '.validate' do
12
12
  describe 'when options is a Fixnum' do
13
13
  let(:options) { 5 }
14
14
 
15
15
  describe 'when name is "John Doe"' do
16
- let(:name) { "John Doe" }
16
+ let(:name) { 'John Doe' }
17
17
  it { expect(subject).to eq({}) }
18
18
  end
19
19
 
20
20
  describe 'when name is "John"' do
21
- let(:name) { "John" }
21
+ let(:name) { 'John' }
22
22
  it do
23
- expect(subject).to eq({
24
- name: ["must be at least 5 characters"]
25
- })
23
+ expect(subject).to eq(name: ['must be at least 5 characters'])
26
24
  end
27
25
  end
28
26
  end
@@ -33,36 +31,28 @@ describe Volt::LengthValidator do
33
31
  end
34
32
 
35
33
  describe 'when name is "John Doe"' do
36
- let(:name) { "John Doe" }
34
+ let(:name) { 'John Doe' }
37
35
  it { expect(subject).to eq({}) }
38
36
  end
39
37
 
40
38
  describe 'when name is "John"' do
41
- let(:name) { "John" }
39
+ let(:name) { 'John' }
42
40
  it do
43
41
  if RUBY_PLATFORM == 'opal'
44
- expect(subject).to eq({
45
- name: ["must be at least {\"length\"=>5, \"maximum\"=>10} characters"]
46
- })
42
+ expect(subject).to eq(name: ["must be at least {\"length\"=>5, \"maximum\"=>10} characters"])
47
43
  else
48
- expect(subject).to eq({
49
- name: ["must be at least {:length=>5, :maximum=>10} characters"]
50
- })
44
+ expect(subject).to eq(name: ['must be at least {:length=>5, :maximum=>10} characters'])
51
45
  end
52
46
  end
53
47
  end
54
48
 
55
49
  describe 'when name is "Zach Galifianakis"' do
56
- let(:name) { "Zach Galifianakis" }
50
+ let(:name) { 'Zach Galifianakis' }
57
51
  it do
58
52
  if RUBY_PLATFORM == 'opal'
59
- expect(subject).to eq({
60
- name: ["must be less than {\"length\"=>5, \"maximum\"=>10} characters"]
61
- })
53
+ expect(subject).to eq(name: ["must be less than {\"length\"=>5, \"maximum\"=>10} characters"])
62
54
  else
63
- expect(subject).to eq({
64
- name: ["must be less than {:length=>5, :maximum=>10} characters"]
65
- })
55
+ expect(subject).to eq(name: ['must be less than {:length=>5, :maximum=>10} characters'])
66
56
  end
67
57
  end
68
58
  end
@@ -71,7 +61,7 @@ describe Volt::LengthValidator do
71
61
  describe 'when options not a Fixnum or a Hash' do
72
62
  let(:options) { 'string' }
73
63
 
74
- it "raises an exception" do
64
+ it 'raises an exception' do
75
65
  expect { subject }.to raise_error(
76
66
  RuntimeError,
77
67
  'The arguments to length must be a number or a hash'