granite-form 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/CODEOWNERS +2 -0
- data/.rubocop_todo.yml +1 -1
- data/CHANGELOG.md +9 -66
- data/README.md +53 -51
- data/Rakefile +4 -0
- data/granite-form.gemspec +1 -0
- data/lib/granite/form/config.rb +10 -10
- data/lib/granite/form/model/associations/nested_attributes.rb +3 -3
- data/lib/granite/form/model/associations/reflections/references_many.rb +3 -1
- data/lib/granite/form/model/associations/reflections/references_one.rb +3 -1
- data/lib/granite/form/model/attributes/attribute.rb +1 -1
- data/lib/granite/form/model/attributes/base.rb +13 -16
- data/lib/granite/form/model/attributes/collection.rb +1 -1
- data/lib/granite/form/model/attributes/dictionary.rb +1 -1
- data/lib/granite/form/model/attributes/localized.rb +1 -1
- data/lib/granite/form/model/attributes/reference_many.rb +1 -1
- data/lib/granite/form/model/attributes/reference_one.rb +1 -9
- data/lib/granite/form/model/attributes/reflections/base.rb +1 -5
- data/lib/granite/form/model/attributes/reflections/reference_one.rb +0 -4
- data/lib/granite/form/model/representation.rb +1 -1
- data/lib/granite/form/model/validations/nested.rb +1 -1
- data/lib/granite/form/types/active_support/time_zone.rb +22 -0
- data/lib/granite/form/types/array.rb +17 -0
- data/lib/granite/form/types/big_decimal.rb +15 -0
- data/lib/granite/form/types/boolean.rb +38 -0
- data/lib/granite/form/types/date.rb +15 -0
- data/lib/granite/form/types/date_time.rb +15 -0
- data/lib/granite/form/types/float.rb +15 -0
- data/lib/granite/form/types/hash_with_action_controller_parameters.rb +18 -0
- data/lib/granite/form/types/integer.rb +13 -0
- data/lib/granite/form/types/object.rb +30 -0
- data/lib/granite/form/types/string.rb +13 -0
- data/lib/granite/form/types/time.rb +15 -0
- data/lib/granite/form/types/uuid.rb +22 -0
- data/lib/granite/form/types.rb +15 -0
- data/lib/granite/form/version.rb +1 -1
- data/lib/granite/form.rb +19 -118
- data/spec/{lib/granite → granite}/form/active_record/associations_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/active_record/nested_attributes_spec.rb +0 -1
- data/spec/{lib/granite → granite}/form/config_spec.rb +22 -10
- data/spec/granite/form/extensions_spec.rb +12 -0
- data/spec/{lib/granite → granite}/form/model/associations/embeds_many_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/associations/embeds_one_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/associations/nested_attributes_spec.rb +0 -1
- data/spec/{lib/granite → granite}/form/model/associations/persistence_adapters/active_record_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/associations/references_many_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/associations/references_one_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/associations/reflections/embeds_any_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/associations/reflections/embeds_many_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/associations/reflections/embeds_one_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/associations/reflections/references_many_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/associations/reflections/references_one_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/associations/validations_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/associations_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/attributes/attribute_spec.rb +4 -46
- data/spec/{lib/granite → granite}/form/model/attributes/base_spec.rb +11 -2
- data/spec/{lib/granite → granite}/form/model/attributes/collection_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/attributes/dictionary_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/attributes/localized_spec.rb +1 -1
- data/spec/{lib/granite → granite}/form/model/attributes/reflections/attribute_spec.rb +0 -12
- data/spec/{lib/granite → granite}/form/model/attributes/reflections/base_spec.rb +1 -1
- data/spec/{lib/granite → granite}/form/model/attributes/reflections/collection_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/attributes/reflections/dictionary_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/attributes/reflections/localized_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/attributes/reflections/represents_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/attributes/represents_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/attributes_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/callbacks_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/conventions_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/dirty_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/lifecycle_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/persistence_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/primary_spec.rb +1 -1
- data/spec/{lib/granite → granite}/form/model/representation_spec.rb +14 -2
- data/spec/{lib/granite → granite}/form/model/scopes_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/validations/associated_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model/validations/nested_spec.rb +55 -11
- data/spec/{lib/granite → granite}/form/model/validations_spec.rb +0 -0
- data/spec/{lib/granite → granite}/form/model_spec.rb +0 -0
- data/spec/granite/form/types/active_support/time_zone_spec.rb +24 -0
- data/spec/granite/form/types/array_spec.rb +13 -0
- data/spec/granite/form/types/big_decimal_spec.rb +19 -0
- data/spec/granite/form/types/boolean_spec.rb +21 -0
- data/spec/granite/form/types/date_spec.rb +18 -0
- data/spec/granite/form/types/date_time_spec.rb +20 -0
- data/spec/granite/form/types/float_spec.rb +19 -0
- data/spec/granite/form/types/hash_with_action_controller_parameters_spec.rb +22 -0
- data/spec/granite/form/types/integer_spec.rb +18 -0
- data/spec/granite/form/types/object_spec.rb +40 -0
- data/spec/granite/form/types/string_spec.rb +13 -0
- data/spec/granite/form/types/time_spec.rb +31 -0
- data/spec/granite/form/types/uuid_spec.rb +21 -0
- data/spec/{lib/granite → granite}/form_spec.rb +0 -0
- data/spec/spec_helper.rb +1 -2
- data/spec/{shared → support/shared}/nested_attribute_examples.rb +0 -0
- data/spec/support/shared/type_examples.rb +7 -0
- data/spec/support/translations.rb +6 -0
- metadata +150 -91
- data/spec/lib/granite/form/model/typecasting_spec.rb +0 -193
@@ -57,51 +57,6 @@ describe Granite::Form::Model::Attributes::Attribute do
|
|
57
57
|
specify { expect(attribute(default: false, type: Boolean).defaultize(nil)).to eq(false) }
|
58
58
|
end
|
59
59
|
|
60
|
-
describe '#typecast' do
|
61
|
-
context 'when Object' do
|
62
|
-
specify { expect(attribute.typecast(:hello)).to eq(:hello) }
|
63
|
-
end
|
64
|
-
|
65
|
-
context 'when Integer' do
|
66
|
-
specify { expect(attribute(type: Integer).typecast(42)).to eq(42) }
|
67
|
-
specify { expect(attribute(type: Integer).typecast('42')).to eq(42) }
|
68
|
-
end
|
69
|
-
|
70
|
-
context 'when Hash' do
|
71
|
-
let(:to_h) { {'x' => {'foo' => 'bar'}, 'y' => 2} }
|
72
|
-
let(:parameters) { ActionController::Parameters.new(to_h) }
|
73
|
-
|
74
|
-
before(:all) do
|
75
|
-
@default_hash_typecaster = Granite::Form.typecaster('Hash')
|
76
|
-
require 'action_controller'
|
77
|
-
Class.new(ActionController::Base)
|
78
|
-
@action_controller_hash_typecaster = Granite::Form.typecaster('Hash')
|
79
|
-
end
|
80
|
-
|
81
|
-
context 'when ActionController is loaded' do
|
82
|
-
before { Granite::Form.typecaster('Hash', &@action_controller_hash_typecaster) }
|
83
|
-
after { Granite::Form.typecaster('Hash', &@default_hash_typecaster) }
|
84
|
-
|
85
|
-
specify { expect(attribute(type: Hash).typecast(nil)).to be_nil }
|
86
|
-
specify { expect(attribute(type: Hash).typecast(to_h)).to eq(to_h) }
|
87
|
-
specify { expect(attribute(type: Hash).typecast(parameters)).to be_nil }
|
88
|
-
specify { expect(attribute(type: Hash).typecast(parameters.permit(:y, x: [:foo]))).to eq(to_h) }
|
89
|
-
end
|
90
|
-
|
91
|
-
context 'when ActionController is not loaded' do
|
92
|
-
before { Granite::Form.typecaster('Hash', &@default_hash_typecaster) }
|
93
|
-
|
94
|
-
specify { expect(attribute(type: Hash).typecast(nil)).to be_nil }
|
95
|
-
specify { expect(attribute(type: Hash).typecast(to_h)).to eq(to_h) }
|
96
|
-
if ActiveSupport.version > Gem::Version.new('4.3')
|
97
|
-
specify { expect(attribute(type: Hash).typecast(parameters.permit(:y, x: [:foo]))).to be_nil }
|
98
|
-
else
|
99
|
-
specify { expect(attribute(type: Hash).typecast(parameters.permit(:y, x: [:foo]))).to eq(to_h) }
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
60
|
describe '#enum' do
|
106
61
|
before { allow_any_instance_of(Dummy).to receive_messages(value: 1..5) }
|
107
62
|
|
@@ -144,7 +99,10 @@ describe Granite::Form::Model::Attributes::Attribute do
|
|
144
99
|
|
145
100
|
context 'integration' do
|
146
101
|
before do
|
147
|
-
|
102
|
+
config = Granite::Form::Config.send(:new)
|
103
|
+
config.types.merge! Granite::Form.config.types
|
104
|
+
allow(Granite::Form).to receive_messages(config: config)
|
105
|
+
|
148
106
|
Granite::Form.normalizer(:strip) { |value, _, _| value.strip }
|
149
107
|
Granite::Form.normalizer(:trim) do |value, options, _attribute|
|
150
108
|
value.first(length || options[:length] || 2)
|
@@ -1,12 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Granite::Form::Model::Attributes::Base do
|
4
|
+
let(:model) { Dummy.new }
|
5
|
+
|
4
6
|
before { stub_model(:dummy) }
|
5
7
|
|
6
8
|
def attribute(*args)
|
7
9
|
options = args.extract_options!
|
8
|
-
Dummy.add_attribute(Granite::Form::Model::Attributes::Reflections::Base, :field, options)
|
9
|
-
|
10
|
+
Dummy.add_attribute(Granite::Form::Model::Attributes::Reflections::Base, :field, options.reverse_merge(type: Object))
|
11
|
+
model.attribute(:field)
|
10
12
|
end
|
11
13
|
|
12
14
|
describe '#read' do
|
@@ -94,4 +96,11 @@ describe Granite::Form::Model::Attributes::Base do
|
|
94
96
|
specify { expect(attribute(readonly: -> { false })).not_to be_readonly }
|
95
97
|
specify { expect(attribute(readonly: -> { true })).to be_readonly }
|
96
98
|
end
|
99
|
+
|
100
|
+
describe '#type_definition' do
|
101
|
+
subject { attr.type_definition }
|
102
|
+
let(:attr) { attribute(type: String) }
|
103
|
+
|
104
|
+
it { is_expected.to have_attributes(type: String, reflection: subject.reflection, owner: model) }
|
105
|
+
end
|
97
106
|
end
|
File without changes
|
File without changes
|
@@ -6,7 +6,7 @@ describe Granite::Form::Model::Attributes::Localized do
|
|
6
6
|
def attribute(*args)
|
7
7
|
options = args.extract_options!
|
8
8
|
Dummy.add_attribute(Granite::Form::Model::Attributes::Reflections::Localized, :field, options)
|
9
|
-
|
9
|
+
Dummy.new.attribute(:field)
|
10
10
|
end
|
11
11
|
|
12
12
|
describe '#read' do
|
@@ -43,18 +43,6 @@ describe Granite::Form::Model::Attributes::Reflections::Attribute do
|
|
43
43
|
specify { expect(reflection(default: -> {}).defaultizer).to be_a Proc }
|
44
44
|
end
|
45
45
|
|
46
|
-
describe '#typecaster' do
|
47
|
-
before do
|
48
|
-
stub_class(:dummy, String)
|
49
|
-
stub_class(:dummy_dummy, Dummy)
|
50
|
-
end
|
51
|
-
|
52
|
-
specify { expect(reflection(type: Object).typecaster).to eq(Granite::Form.typecaster(Object)) }
|
53
|
-
specify { expect(reflection(type: String).typecaster).to eq(Granite::Form.typecaster(String)) }
|
54
|
-
specify { expect(reflection(type: Dummy).typecaster).to eq(Granite::Form.typecaster(String)) }
|
55
|
-
specify { expect(reflection(type: DummyDummy).typecaster).to eq(Granite::Form.typecaster(String)) }
|
56
|
-
end
|
57
|
-
|
58
46
|
describe '#enumerizer' do
|
59
47
|
specify { expect(reflection.enumerizer).to be_nil }
|
60
48
|
specify { expect(reflection(enum: 42).enumerizer).to eq(42) }
|
@@ -36,7 +36,7 @@ describe Granite::Form::Model::Attributes::Reflections::Base do
|
|
36
36
|
stub_class(:owner)
|
37
37
|
end
|
38
38
|
|
39
|
-
let(:reflection) { SomeScope::Borogoves.new(:field) }
|
39
|
+
let(:reflection) { SomeScope::Borogoves.new(:field, type: Object) }
|
40
40
|
let(:owner) { Owner.new }
|
41
41
|
|
42
42
|
specify { expect(reflection.build_attribute(owner, nil)).to be_a(Granite::Form::Model::Attributes::Borogoves) }
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -126,13 +126,25 @@ describe Granite::Form::Model::Representation do
|
|
126
126
|
|
127
127
|
specify do
|
128
128
|
expect { post.validate }.to change { post.errors.messages }
|
129
|
-
.to(
|
129
|
+
.to('author.user.email': ['is invalid'], name: ["can't be blank"])
|
130
130
|
end
|
131
131
|
|
132
132
|
if ActiveModel.version >= Gem::Version.new('6.1.0')
|
133
133
|
specify do
|
134
134
|
expect { post.validate }.to change { post.errors.details }
|
135
|
-
.to(
|
135
|
+
.to('author.user.email': [{error: 'is invalid'}], name: [{error: :blank}])
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context 'when using symbol in error message of represented model' do
|
140
|
+
before do
|
141
|
+
Author.validates :name, inclusion: {in: ['Author'], message: :invalid_name, allow_blank: true}
|
142
|
+
post.author.name = 'Not Author'
|
143
|
+
end
|
144
|
+
|
145
|
+
specify do
|
146
|
+
expect { post.validate }.to change { post.errors.messages }
|
147
|
+
.to('author.user.email': ['is invalid'], name: ['must be Author'])
|
136
148
|
end
|
137
149
|
end
|
138
150
|
end
|
File without changes
|
File without changes
|
@@ -114,35 +114,79 @@ describe Granite::Form::Model::Validations::NestedValidator do
|
|
114
114
|
|
115
115
|
context 'object field is invalid and referenced object does not include AutosaveAssociation' do
|
116
116
|
before do
|
117
|
-
|
118
|
-
include Granite::Form::Model::Validations
|
119
|
-
include Granite::Form::Model::Attributes
|
120
|
-
include Granite::Form::Model::Primary
|
121
|
-
|
122
|
-
primary :id
|
117
|
+
stub_model(:validated_object) do
|
123
118
|
attribute :title, String
|
124
119
|
validates_presence_of :title
|
125
120
|
end
|
126
121
|
|
127
|
-
|
128
|
-
include Granite::Form::Model::
|
122
|
+
stub_model(:main) do
|
123
|
+
include Granite::Form::Model::Persistence
|
124
|
+
|
129
125
|
attribute :object, Object
|
130
126
|
validates :object, nested: true
|
131
127
|
end
|
132
128
|
end
|
133
129
|
|
134
|
-
subject(:instance) { Main.instantiate name: 'hello', object: object
|
130
|
+
subject(:instance) { Main.instantiate name: 'hello', object: object }
|
135
131
|
|
136
|
-
context do
|
132
|
+
context 'nested object is valid' do
|
137
133
|
let(:object) { ValidatedObject.new(title: 'Mr.') }
|
134
|
+
|
138
135
|
it { is_expected.to be_valid }
|
139
136
|
end
|
140
137
|
|
141
|
-
context do
|
138
|
+
context 'nested object is invalid' do
|
142
139
|
let(:object) { ValidatedObject.new }
|
140
|
+
|
143
141
|
it do
|
144
142
|
expect { subject.valid? }.not_to raise_error
|
145
143
|
expect(subject).not_to be_valid
|
144
|
+
expect(subject.errors.count).to eq(1)
|
145
|
+
end
|
146
|
+
|
147
|
+
context 'nested validation runs twice' do
|
148
|
+
before do
|
149
|
+
stub_model(:main) do
|
150
|
+
include Granite::Form::Model::Persistence
|
151
|
+
|
152
|
+
attribute :object, Object
|
153
|
+
validates :object, nested: true
|
154
|
+
validates :object, nested: true
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
it do
|
159
|
+
subject.validate
|
160
|
+
expect(subject.errors.count).to eq(1)
|
161
|
+
end
|
162
|
+
|
163
|
+
context 'nested object validation has condition' do
|
164
|
+
before do
|
165
|
+
stub_model(:validated_object) do
|
166
|
+
attribute :title, String
|
167
|
+
validates_presence_of :title, if: -> { true }
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
it do
|
172
|
+
subject.validate
|
173
|
+
expect(subject.errors.count).to eq(1)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
context 'nested object validation has message' do
|
178
|
+
before do
|
179
|
+
stub_model(:validated_object) do
|
180
|
+
attribute :title, String
|
181
|
+
validates_presence_of :title, message: 'test'
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
it do
|
186
|
+
subject.validate
|
187
|
+
expect(subject.errors.count).to eq(1)
|
188
|
+
end
|
189
|
+
end
|
146
190
|
end
|
147
191
|
end
|
148
192
|
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Granite::Form::Types::ActiveSupport::TimeZone do
|
6
|
+
describe 'typecasting' do
|
7
|
+
include_context 'type setup', 'ActiveSupport::TimeZone'
|
8
|
+
|
9
|
+
specify { expect(typecast(nil)).to be_nil }
|
10
|
+
specify { expect(typecast(Object.new)).to be_nil }
|
11
|
+
specify { expect(typecast(Time.now)).to be_nil }
|
12
|
+
specify { expect(typecast('blablabla')).to be_nil }
|
13
|
+
specify { expect(typecast(TZInfo::Timezone.all.first)).to be_a ActiveSupport::TimeZone }
|
14
|
+
specify { expect(typecast('Moscow')).to be_a ActiveSupport::TimeZone }
|
15
|
+
specify { expect(typecast('+4')).to be_a ActiveSupport::TimeZone }
|
16
|
+
specify { expect(typecast('-3')).to be_a ActiveSupport::TimeZone }
|
17
|
+
specify { expect(typecast('3600')).to be_a ActiveSupport::TimeZone }
|
18
|
+
specify { expect(typecast('-7200')).to be_a ActiveSupport::TimeZone }
|
19
|
+
specify { expect(typecast(4)).to be_a ActiveSupport::TimeZone }
|
20
|
+
specify { expect(typecast(-3)).to be_a ActiveSupport::TimeZone }
|
21
|
+
specify { expect(typecast(3600)).to be_a ActiveSupport::TimeZone }
|
22
|
+
specify { expect(typecast(-7200)).to be_a ActiveSupport::TimeZone }
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Granite::Form::Types::Array do
|
6
|
+
describe 'typecasting' do
|
7
|
+
include_context 'type setup', 'Array'
|
8
|
+
|
9
|
+
specify { expect(typecast([1, 2, 3])).to eq([1, 2, 3]) }
|
10
|
+
specify { expect(typecast('hello, world')).to eq(%w[hello world]) }
|
11
|
+
specify { expect(typecast(10)).to be_nil }
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Granite::Form::Types::BigDecimal do
|
6
|
+
describe 'typecasting' do
|
7
|
+
include_context 'type setup', 'BigDecimal'
|
8
|
+
|
9
|
+
specify { expect(typecast('hello')).to be_nil }
|
10
|
+
specify { expect(typecast('123hello')).to be_nil }
|
11
|
+
specify { expect(typecast('123')).to eq(BigDecimal('123.0')) }
|
12
|
+
specify { expect(typecast('123.')).to be_nil }
|
13
|
+
specify { expect(typecast('123.5')).to eq(BigDecimal('123.5')) }
|
14
|
+
specify { expect(typecast(123)).to eq(BigDecimal('123.0')) }
|
15
|
+
specify { expect(typecast(123.5)).to eq(BigDecimal('123.5')) }
|
16
|
+
specify { expect(typecast(nil)).to be_nil }
|
17
|
+
specify { expect(typecast([123.5])).to be_nil }
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Granite::Form::Types::Boolean do
|
6
|
+
describe 'typecasting' do
|
7
|
+
include_context 'type setup', 'Boolean'
|
8
|
+
|
9
|
+
specify { expect(typecast('hello')).to be_nil }
|
10
|
+
specify { expect(typecast('true')).to eq(true) }
|
11
|
+
specify { expect(typecast('false')).to eq(false) }
|
12
|
+
specify { expect(typecast('1')).to eq(true) }
|
13
|
+
specify { expect(typecast('0')).to eq(false) }
|
14
|
+
specify { expect(typecast(true)).to eq(true) }
|
15
|
+
specify { expect(typecast(false)).to eq(false) }
|
16
|
+
specify { expect(typecast(1)).to eq(true) }
|
17
|
+
specify { expect(typecast(0)).to eq(false) }
|
18
|
+
specify { expect(typecast(nil)).to be_nil }
|
19
|
+
specify { expect(typecast([123])).to be_nil }
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Granite::Form::Types::Date do
|
6
|
+
describe 'typecasting' do
|
7
|
+
include_context 'type setup', 'Date'
|
8
|
+
let(:date) { Date.new(2013, 6, 13) }
|
9
|
+
|
10
|
+
specify { expect(typecast(nil)).to be_nil }
|
11
|
+
specify { expect(typecast('2013-06-13')).to eq(date) }
|
12
|
+
specify { expect(typecast('2013-55-55')).to be_nil }
|
13
|
+
specify { expect(typecast('blablabla')).to be_nil }
|
14
|
+
specify { expect(typecast(DateTime.new(2013, 6, 13, 23, 13))).to eq(date) } # rubocop:disable Style/DateTime
|
15
|
+
specify { expect(typecast(Time.new(2013, 6, 13, 23, 13))).to eq(date) }
|
16
|
+
specify { expect(typecast(Date.new(2013, 6, 13))).to eq(date) }
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Granite::Form::Types::DateTime do
|
6
|
+
# rubocop:disable Style/DateTime
|
7
|
+
describe 'typecasting' do
|
8
|
+
include_context 'type setup', 'DateTime'
|
9
|
+
let(:datetime) { DateTime.new(2013, 6, 13, 23, 13) }
|
10
|
+
|
11
|
+
specify { expect(typecast(nil)).to be_nil }
|
12
|
+
specify { expect(typecast('2013-06-13 23:13')).to eq(datetime) }
|
13
|
+
specify { expect(typecast('2013-55-55 55:55')).to be_nil }
|
14
|
+
specify { expect(typecast('blablabla')).to be_nil }
|
15
|
+
specify { expect(typecast(Date.new(2013, 6, 13))).to eq(DateTime.new(2013, 6, 13, 0, 0)) }
|
16
|
+
specify { expect(typecast(Time.utc(2013, 6, 13, 23, 13).utc)).to eq(datetime) }
|
17
|
+
specify { expect(typecast(DateTime.new(2013, 6, 13, 23, 13))).to eq(datetime) }
|
18
|
+
end
|
19
|
+
# rubocop:enable Style/DateTime
|
20
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Granite::Form::Types::Float do
|
6
|
+
describe 'typecasting' do
|
7
|
+
include_context 'type setup', 'Float'
|
8
|
+
|
9
|
+
specify { expect(typecast('hello')).to be_nil }
|
10
|
+
specify { expect(typecast('123hello')).to be_nil }
|
11
|
+
specify { expect(typecast('123')).to eq(123.0) }
|
12
|
+
specify { expect(typecast('123.')).to be_nil }
|
13
|
+
specify { expect(typecast('123.5')).to eq(123.5) }
|
14
|
+
specify { expect(typecast(123)).to eq(123.0) }
|
15
|
+
specify { expect(typecast(123.5)).to eq(123.5) }
|
16
|
+
specify { expect(typecast(nil)).to be_nil }
|
17
|
+
specify { expect(typecast([123.5])).to be_nil }
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Granite::Form::Types::HashWithActionControllerParameters do
|
6
|
+
describe 'typecasting' do
|
7
|
+
include_context 'type setup', 'Hash'
|
8
|
+
|
9
|
+
before(:all) do
|
10
|
+
require 'action_controller'
|
11
|
+
Class.new(ActionController::Base)
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:to_h) { {'x' => {'foo' => 'bar'}, 'y' => 2} }
|
15
|
+
let(:parameters) { ActionController::Parameters.new(to_h) }
|
16
|
+
|
17
|
+
specify { expect(typecast(nil)).to be_nil }
|
18
|
+
specify { expect(typecast(to_h)).to eq(to_h) }
|
19
|
+
specify { expect(typecast(parameters)).to be_nil }
|
20
|
+
specify { expect(typecast(parameters.permit(:y, x: [:foo]))).to eq(to_h) }
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Granite::Form::Types::Integer do
|
6
|
+
describe 'typecasting' do
|
7
|
+
include_context 'type setup', 'Integer'
|
8
|
+
|
9
|
+
specify { expect(typecast('hello')).to be_nil }
|
10
|
+
specify { expect(typecast('123hello')).to be_nil }
|
11
|
+
specify { expect(typecast('123')).to eq(123) }
|
12
|
+
specify { expect(typecast('123.5')).to eq(123) }
|
13
|
+
specify { expect(typecast(123)).to eq(123) }
|
14
|
+
specify { expect(typecast(123.5)).to eq(123) }
|
15
|
+
specify { expect(typecast(nil)).to be_nil }
|
16
|
+
specify { expect(typecast([123])).to be_nil }
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Granite::Form::Types::Object do
|
6
|
+
subject(:type) { described_class.new(String, reflection, model.new) }
|
7
|
+
let(:model) { stub_model }
|
8
|
+
let(:reflection) { Granite::Form::Model::Attributes::Reflections::Base.new(:field) }
|
9
|
+
|
10
|
+
describe '#initialize' do
|
11
|
+
it { is_expected.to have_attributes(type: String, reflection: reflection, owner: an_instance_of(model)) }
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'typecasting' do
|
15
|
+
before { stub_class(:descendant) }
|
16
|
+
|
17
|
+
context 'with Object type' do
|
18
|
+
include_context 'type setup', 'Object'
|
19
|
+
|
20
|
+
specify { expect(typecast('hello')).to eq('hello') }
|
21
|
+
specify { expect(typecast([])).to eq([]) }
|
22
|
+
specify { expect(typecast(Descendant.new)).to be_a(Descendant) }
|
23
|
+
specify { expect(typecast(Object.new)).to be_a(Object) }
|
24
|
+
specify { expect(typecast(nil)).to be_nil }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with Descendant type' do
|
28
|
+
include_context 'type setup', 'Descendant'
|
29
|
+
|
30
|
+
before { stub_class(:descendant2, Descendant) }
|
31
|
+
|
32
|
+
specify { expect(typecast('hello')).to be_nil }
|
33
|
+
specify { expect(typecast([])).to be_nil }
|
34
|
+
specify { expect(typecast(Descendant.new)).to be_a(Descendant) }
|
35
|
+
specify { expect(typecast(Descendant2.new)).to be_a(Descendant2) }
|
36
|
+
specify { expect(typecast(Object.new)).to be_nil }
|
37
|
+
specify { expect(typecast(nil)).to be_nil }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Granite::Form::Types::String do
|
6
|
+
describe 'typecasting' do
|
7
|
+
include_context 'type setup', 'String'
|
8
|
+
|
9
|
+
specify { expect(typecast('hello')).to eq('hello') }
|
10
|
+
specify { expect(typecast(123)).to eq('123') }
|
11
|
+
specify { expect(typecast(nil)).to be_nil }
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Granite::Form::Types::Time do
|
6
|
+
# rubocop:disable Style/DateTime
|
7
|
+
describe 'typecasting' do
|
8
|
+
include_context 'type setup', 'Time'
|
9
|
+
|
10
|
+
specify { expect(typecast(nil)).to be_nil }
|
11
|
+
specify { expect(typecast('2013-06-13 23:13')).to eq('2013-06-13 23:13'.to_time) }
|
12
|
+
specify { expect(typecast('2013-55-55 55:55')).to be_nil }
|
13
|
+
specify { expect(typecast('blablabla')).to be_nil }
|
14
|
+
specify { expect(typecast(Date.new(2013, 6, 13))).to eq(Time.new(2013, 6, 13, 0, 0)) }
|
15
|
+
specify { expect(typecast(DateTime.new(2013, 6, 13, 19, 13))).to eq(DateTime.new(2013, 6, 13, 19, 13).to_time) }
|
16
|
+
specify { expect(typecast(Time.new(2013, 6, 13, 23, 13))).to eq(Time.new(2013, 6, 13, 23, 13)) }
|
17
|
+
|
18
|
+
context 'Time.zone set' do
|
19
|
+
around { |example| Time.use_zone('Bangkok', &example) }
|
20
|
+
|
21
|
+
specify { expect(typecast(nil)).to be_nil }
|
22
|
+
specify { expect(typecast('2013-06-13 23:13')).to eq(Time.zone.parse('2013-06-13 23:13')) }
|
23
|
+
specify { expect(typecast('2013-55-55 55:55')).to be_nil }
|
24
|
+
specify { expect(typecast('blablabla')).to be_nil }
|
25
|
+
specify { expect(typecast(Date.new(2013, 6, 13))).to eq(Time.new(2013, 6, 13, 0, 0)) }
|
26
|
+
specify { expect(typecast(DateTime.new(2013, 6, 13, 19, 13))).to eq(DateTime.new(2013, 6, 13, 19, 13).to_time) }
|
27
|
+
specify { expect(typecast(Time.new(2013, 6, 13, 23, 13))).to eq(Time.new(2013, 6, 13, 23, 13)) }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
# rubocop:enable Style/DateTime
|
31
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe Granite::Form::Types::UUID do
|
6
|
+
describe 'typecasting' do
|
7
|
+
include_context 'type setup', 'Granite::Form::UUID'
|
8
|
+
let(:uuid) { Granite::Form::UUID.random_create }
|
9
|
+
let(:uuid_tools) { UUIDTools::UUID.random_create }
|
10
|
+
|
11
|
+
specify { expect(typecast(nil)).to be_nil }
|
12
|
+
specify { expect(typecast(Object.new)).to be_nil }
|
13
|
+
specify { expect(typecast(uuid_tools)).to be_a Granite::Form::UUID }
|
14
|
+
specify { expect(typecast(uuid_tools)).to eq(uuid_tools) }
|
15
|
+
specify { expect(typecast(uuid)).to eq(uuid) }
|
16
|
+
specify { expect(typecast(uuid.to_s)).to eq(uuid) }
|
17
|
+
specify { expect(typecast(uuid.to_i)).to eq(uuid) }
|
18
|
+
specify { expect(typecast(uuid.hexdigest)).to eq(uuid) }
|
19
|
+
specify { expect(typecast(uuid.raw)).to eq(uuid) }
|
20
|
+
end
|
21
|
+
end
|
File without changes
|
data/spec/spec_helper.rb
CHANGED
@@ -8,8 +8,7 @@ require 'rack/test'
|
|
8
8
|
require 'action_controller/metal/strong_parameters'
|
9
9
|
require 'database_cleaner'
|
10
10
|
|
11
|
-
|
12
|
-
require 'support/muffle_helper'
|
11
|
+
Dir[File.join(__dir__, 'support', '**', '*.rb')].sort.each { |f| require f }
|
13
12
|
|
14
13
|
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
15
14
|
ActiveRecord::Base.logger = Logger.new('/dev/null')
|
File without changes
|