granite-form 0.1.1 → 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/.rubocop_todo.yml +1 -1
- data/CHANGELOG.md +7 -0
- 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/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 +0 -0
- 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/{shared → support/shared}/nested_attribute_examples.rb +0 -0
- data/spec/support/shared/type_examples.rb +7 -0
- metadata +147 -91
- data/spec/lib/granite/form/model/typecasting_spec.rb +0 -193
@@ -1,193 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Granite::Form::Model::Attributes do
|
4
|
-
subject { model.new }
|
5
|
-
|
6
|
-
context 'object' do
|
7
|
-
before { stub_class(:descendant) }
|
8
|
-
let(:model) { stub_model { attribute :column, Object } }
|
9
|
-
|
10
|
-
specify { expect(model.new(column: 'hello').column).to eq('hello') }
|
11
|
-
specify { expect(model.new(column: []).column).to eq([]) }
|
12
|
-
specify { expect(model.new(column: Descendant.new).column).to be_a(Descendant) }
|
13
|
-
specify { expect(model.new(column: Object.new).column).to be_a(Object) }
|
14
|
-
specify { expect(model.new(column: nil).column).to be_nil }
|
15
|
-
|
16
|
-
context do
|
17
|
-
before { stub_class(:descendant2, Descendant) }
|
18
|
-
let(:model) { stub_model { attribute :column, Descendant } }
|
19
|
-
|
20
|
-
specify { expect(model.new(column: 'hello').column).to be_nil }
|
21
|
-
specify { expect(model.new(column: []).column).to be_nil }
|
22
|
-
specify { expect(model.new(column: Descendant.new).column).to be_a(Descendant) }
|
23
|
-
specify { expect(model.new(column: Descendant2.new).column).to be_a(Descendant2) }
|
24
|
-
specify { expect(model.new(column: Object.new).column).to be_nil }
|
25
|
-
specify { expect(model.new(column: nil).column).to be_nil }
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
context 'string' do
|
30
|
-
let(:model) { stub_model { attribute :column, String } }
|
31
|
-
|
32
|
-
specify { expect(model.new(column: 'hello').column).to eq('hello') }
|
33
|
-
specify { expect(model.new(column: 123).column).to eq('123') }
|
34
|
-
specify { expect(model.new(column: nil).column).to be_nil }
|
35
|
-
end
|
36
|
-
|
37
|
-
context 'integer' do
|
38
|
-
let(:model) { stub_model { attribute :column, Integer } }
|
39
|
-
|
40
|
-
specify { expect(model.new(column: 'hello').column).to be_nil }
|
41
|
-
specify { expect(model.new(column: '123hello').column).to be_nil }
|
42
|
-
specify { expect(model.new(column: '123').column).to eq(123) }
|
43
|
-
specify { expect(model.new(column: '123.5').column).to eq(123) }
|
44
|
-
specify { expect(model.new(column: 123).column).to eq(123) }
|
45
|
-
specify { expect(model.new(column: 123.5).column).to eq(123) }
|
46
|
-
specify { expect(model.new(column: nil).column).to be_nil }
|
47
|
-
specify { expect(model.new(column: [123]).column).to be_nil }
|
48
|
-
end
|
49
|
-
|
50
|
-
context 'float' do
|
51
|
-
let(:model) { stub_model { attribute :column, Float } }
|
52
|
-
|
53
|
-
specify { expect(model.new(column: 'hello').column).to be_nil }
|
54
|
-
specify { expect(model.new(column: '123hello').column).to be_nil }
|
55
|
-
specify { expect(model.new(column: '123').column).to eq(123.0) }
|
56
|
-
specify { expect(model.new(column: '123.').column).to be_nil }
|
57
|
-
specify { expect(model.new(column: '123.5').column).to eq(123.5) }
|
58
|
-
specify { expect(model.new(column: 123).column).to eq(123.0) }
|
59
|
-
specify { expect(model.new(column: 123.5).column).to eq(123.5) }
|
60
|
-
specify { expect(model.new(column: nil).column).to be_nil }
|
61
|
-
specify { expect(model.new(column: [123.5]).column).to be_nil }
|
62
|
-
end
|
63
|
-
|
64
|
-
context 'big_decimal' do
|
65
|
-
let(:model) { stub_model { attribute :column, BigDecimal } }
|
66
|
-
|
67
|
-
specify { expect(model.new(column: 'hello').column).to be_nil }
|
68
|
-
specify { expect(model.new(column: '123hello').column).to be_nil }
|
69
|
-
specify { expect(model.new(column: '123').column).to eq(BigDecimal('123.0')) }
|
70
|
-
specify { expect(model.new(column: '123.').column).to be_nil }
|
71
|
-
specify { expect(model.new(column: '123.5').column).to eq(BigDecimal('123.5')) }
|
72
|
-
specify { expect(model.new(column: 123).column).to eq(BigDecimal('123.0')) }
|
73
|
-
specify { expect(model.new(column: 123.5).column).to eq(BigDecimal('123.5')) }
|
74
|
-
specify { expect(model.new(column: nil).column).to be_nil }
|
75
|
-
specify { expect(model.new(column: [123.5]).column).to be_nil }
|
76
|
-
end
|
77
|
-
|
78
|
-
context 'boolean' do
|
79
|
-
let(:model) { stub_model { attribute :column, Boolean } }
|
80
|
-
|
81
|
-
specify { expect(model.new(column: 'hello').column).to be_nil }
|
82
|
-
specify { expect(model.new(column: 'true').column).to eq(true) }
|
83
|
-
specify { expect(model.new(column: 'false').column).to eq(false) }
|
84
|
-
specify { expect(model.new(column: '1').column).to eq(true) }
|
85
|
-
specify { expect(model.new(column: '0').column).to eq(false) }
|
86
|
-
specify { expect(model.new(column: true).column).to eq(true) }
|
87
|
-
specify { expect(model.new(column: false).column).to eq(false) }
|
88
|
-
specify { expect(model.new(column: 1).column).to eq(true) }
|
89
|
-
specify { expect(model.new(column: 0).column).to eq(false) }
|
90
|
-
specify { expect(model.new(column: nil).column).to be_nil }
|
91
|
-
specify { expect(model.new(column: [123]).column).to be_nil }
|
92
|
-
end
|
93
|
-
|
94
|
-
context 'array' do
|
95
|
-
let(:model) { stub_model { attribute :column, Array } }
|
96
|
-
|
97
|
-
specify { expect(model.new(column: [1, 2, 3]).column).to eq([1, 2, 3]) }
|
98
|
-
specify { expect(model.new(column: 'hello, world').column).to eq(%w[hello world]) }
|
99
|
-
specify { expect(model.new(column: 10).column).to be_nil }
|
100
|
-
end
|
101
|
-
|
102
|
-
# rubocop:disable Style/DateTime
|
103
|
-
context 'date' do
|
104
|
-
let(:model) { stub_model { attribute :column, Date } }
|
105
|
-
let(:date) { Date.new(2013, 6, 13) }
|
106
|
-
|
107
|
-
specify { expect(model.new(column: nil).column).to be_nil }
|
108
|
-
specify { expect(model.new(column: '2013-06-13').column).to eq(date) }
|
109
|
-
specify { expect(model.new(column: '2013-55-55').column).to be_nil }
|
110
|
-
specify { expect(model.new(column: 'blablabla').column).to be_nil }
|
111
|
-
specify { expect(model.new(column: DateTime.new(2013, 6, 13, 23, 13)).column).to eq(date) }
|
112
|
-
specify { expect(model.new(column: Time.new(2013, 6, 13, 23, 13)).column).to eq(date) }
|
113
|
-
specify { expect(model.new(column: Date.new(2013, 6, 13)).column).to eq(date) }
|
114
|
-
end
|
115
|
-
|
116
|
-
context 'datetime' do
|
117
|
-
let(:model) { stub_model { attribute :column, DateTime } }
|
118
|
-
let(:datetime) { DateTime.new(2013, 6, 13, 23, 13) }
|
119
|
-
|
120
|
-
specify { expect(model.new(column: nil).column).to be_nil }
|
121
|
-
specify { expect(model.new(column: '2013-06-13 23:13').column).to eq(datetime) }
|
122
|
-
specify { expect(model.new(column: '2013-55-55 55:55').column).to be_nil }
|
123
|
-
specify { expect(model.new(column: 'blablabla').column).to be_nil }
|
124
|
-
specify { expect(model.new(column: Date.new(2013, 6, 13)).column).to eq(DateTime.new(2013, 6, 13, 0, 0)) }
|
125
|
-
specify { expect(model.new(column: Time.utc(2013, 6, 13, 23, 13).utc).column).to eq(DateTime.new(2013, 6, 13, 23, 13)) }
|
126
|
-
specify { expect(model.new(column: DateTime.new(2013, 6, 13, 23, 13)).column).to eq(DateTime.new(2013, 6, 13, 23, 13)) }
|
127
|
-
end
|
128
|
-
|
129
|
-
context 'time' do
|
130
|
-
let(:model) { stub_model { attribute :column, Time } }
|
131
|
-
|
132
|
-
specify { expect(model.new(column: nil).column).to be_nil }
|
133
|
-
specify { expect(model.new(column: '2013-06-13 23:13').column).to eq('2013-06-13 23:13'.to_time) }
|
134
|
-
specify { expect(model.new(column: '2013-55-55 55:55').column).to be_nil }
|
135
|
-
specify { expect(model.new(column: 'blablabla').column).to be_nil }
|
136
|
-
specify { expect(model.new(column: Date.new(2013, 6, 13)).column).to eq(Time.new(2013, 6, 13, 0, 0)) }
|
137
|
-
specify { expect(model.new(column: DateTime.new(2013, 6, 13, 19, 13)).column).to eq(DateTime.new(2013, 6, 13, 19, 13).to_time) }
|
138
|
-
specify { expect(model.new(column: Time.new(2013, 6, 13, 23, 13)).column).to eq(Time.new(2013, 6, 13, 23, 13)) }
|
139
|
-
|
140
|
-
context 'Time.zone set' do
|
141
|
-
around { |example| Time.use_zone('Bangkok', &example) }
|
142
|
-
|
143
|
-
specify { expect(model.new(column: nil).column).to be_nil }
|
144
|
-
specify { expect(model.new(column: '2013-06-13 23:13').column).to eq(Time.zone.parse('2013-06-13 23:13')) }
|
145
|
-
specify { expect(model.new(column: '2013-55-55 55:55').column).to be_nil }
|
146
|
-
specify { expect(model.new(column: 'blablabla').column).to be_nil }
|
147
|
-
specify { expect(model.new(column: Date.new(2013, 6, 13)).column).to eq(Time.new(2013, 6, 13, 0, 0)) }
|
148
|
-
specify { expect(model.new(column: DateTime.new(2013, 6, 13, 19, 13)).column).to eq(DateTime.new(2013, 6, 13, 19, 13).to_time) }
|
149
|
-
specify { expect(model.new(column: Time.new(2013, 6, 13, 23, 13)).column).to eq(Time.new(2013, 6, 13, 23, 13)) }
|
150
|
-
end
|
151
|
-
end
|
152
|
-
# rubocop:enable Style/DateTime
|
153
|
-
|
154
|
-
context 'time_zone' do
|
155
|
-
let(:model) { stub_model { attribute :column, ActiveSupport::TimeZone } }
|
156
|
-
|
157
|
-
specify { expect(model.new(column: nil).column).to be_nil }
|
158
|
-
specify { expect(model.new(column: Object.new).column).to be_nil }
|
159
|
-
specify { expect(model.new(column: Time.now).column).to be_nil }
|
160
|
-
specify { expect(model.new(column: 'blablabla').column).to be_nil }
|
161
|
-
specify { expect(model.new(column: TZInfo::Timezone.all.first).column).to be_a ActiveSupport::TimeZone }
|
162
|
-
specify { expect(model.new(column: 'Moscow').column).to be_a ActiveSupport::TimeZone }
|
163
|
-
specify { expect(model.new(column: '+4').column).to be_a ActiveSupport::TimeZone }
|
164
|
-
specify { expect(model.new(column: '-3').column).to be_a ActiveSupport::TimeZone }
|
165
|
-
specify { expect(model.new(column: '3600').column).to be_a ActiveSupport::TimeZone }
|
166
|
-
specify { expect(model.new(column: '-7200').column).to be_a ActiveSupport::TimeZone }
|
167
|
-
specify { expect(model.new(column: 4).column).to be_a ActiveSupport::TimeZone }
|
168
|
-
specify { expect(model.new(column: -3).column).to be_a ActiveSupport::TimeZone }
|
169
|
-
specify { expect(model.new(column: 3600).column).to be_a ActiveSupport::TimeZone }
|
170
|
-
specify { expect(model.new(column: -7200).column).to be_a ActiveSupport::TimeZone }
|
171
|
-
end
|
172
|
-
|
173
|
-
context 'uuid' do
|
174
|
-
let(:model) { stub_model { attribute :column, Granite::Form::UUID } }
|
175
|
-
let(:uuid) { Granite::Form::UUID.random_create }
|
176
|
-
let(:uuid_tools) { UUIDTools::UUID.random_create }
|
177
|
-
|
178
|
-
specify { expect(uuid.as_json).to eq(uuid.to_s) }
|
179
|
-
specify { expect(uuid.to_json).to eq("\"#{uuid}\"") }
|
180
|
-
specify { expect(uuid.to_param).to eq(uuid.to_s) }
|
181
|
-
specify { expect(uuid.to_query(:key)).to eq("key=#{uuid}") }
|
182
|
-
|
183
|
-
specify { expect(model.new(column: nil).column).to be_nil }
|
184
|
-
specify { expect(model.new(column: Object.new).column).to be_nil }
|
185
|
-
specify { expect(model.new(column: uuid_tools).column).to be_a Granite::Form::UUID }
|
186
|
-
specify { expect(model.new(column: uuid_tools).column).to eq(uuid_tools) }
|
187
|
-
specify { expect(model.new(column: uuid).column).to eq(uuid) }
|
188
|
-
specify { expect(model.new(column: uuid.to_s).column).to eq(uuid) }
|
189
|
-
specify { expect(model.new(column: uuid.to_i).column).to eq(uuid) }
|
190
|
-
specify { expect(model.new(column: uuid.hexdigest).column).to eq(uuid) }
|
191
|
-
specify { expect(model.new(column: uuid.raw).column).to eq(uuid) }
|
192
|
-
end
|
193
|
-
end
|