granite-form 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +13 -0
  3. data/.github/workflows/ci.yml +35 -0
  4. data/.github/workflows/main.yml +29 -0
  5. data/.gitignore +21 -0
  6. data/.rspec +2 -0
  7. data/.rubocop.yml +64 -0
  8. data/.rubocop_todo.yml +48 -0
  9. data/Appraisals +8 -0
  10. data/CHANGELOG.md +73 -0
  11. data/Gemfile +8 -0
  12. data/Guardfile +77 -0
  13. data/LICENSE +22 -0
  14. data/README.md +429 -0
  15. data/Rakefile +6 -0
  16. data/gemfiles/rails.4.2.gemfile +15 -0
  17. data/gemfiles/rails.5.0.gemfile +15 -0
  18. data/gemfiles/rails.5.1.gemfile +15 -0
  19. data/gemfiles/rails.5.2.gemfile +15 -0
  20. data/gemfiles/rails.6.0.gemfile +14 -0
  21. data/gemfiles/rails.6.1.gemfile +14 -0
  22. data/gemfiles/rails.7.0.gemfile +14 -0
  23. data/granite-form.gemspec +31 -0
  24. data/lib/granite/form/active_record/associations.rb +57 -0
  25. data/lib/granite/form/active_record/nested_attributes.rb +20 -0
  26. data/lib/granite/form/base.rb +15 -0
  27. data/lib/granite/form/config.rb +42 -0
  28. data/lib/granite/form/errors.rb +111 -0
  29. data/lib/granite/form/extensions.rb +36 -0
  30. data/lib/granite/form/model/associations/base.rb +97 -0
  31. data/lib/granite/form/model/associations/collection/embedded.rb +14 -0
  32. data/lib/granite/form/model/associations/collection/proxy.rb +35 -0
  33. data/lib/granite/form/model/associations/embeds_any.rb +19 -0
  34. data/lib/granite/form/model/associations/embeds_many.rb +152 -0
  35. data/lib/granite/form/model/associations/embeds_one.rb +112 -0
  36. data/lib/granite/form/model/associations/nested_attributes.rb +215 -0
  37. data/lib/granite/form/model/associations/persistence_adapters/active_record/referenced_proxy.rb +33 -0
  38. data/lib/granite/form/model/associations/persistence_adapters/active_record.rb +68 -0
  39. data/lib/granite/form/model/associations/persistence_adapters/base.rb +55 -0
  40. data/lib/granite/form/model/associations/references_any.rb +43 -0
  41. data/lib/granite/form/model/associations/references_many.rb +113 -0
  42. data/lib/granite/form/model/associations/references_one.rb +88 -0
  43. data/lib/granite/form/model/associations/reflections/base.rb +92 -0
  44. data/lib/granite/form/model/associations/reflections/embeds_any.rb +52 -0
  45. data/lib/granite/form/model/associations/reflections/embeds_many.rb +17 -0
  46. data/lib/granite/form/model/associations/reflections/embeds_one.rb +19 -0
  47. data/lib/granite/form/model/associations/reflections/references_any.rb +65 -0
  48. data/lib/granite/form/model/associations/reflections/references_many.rb +30 -0
  49. data/lib/granite/form/model/associations/reflections/references_one.rb +32 -0
  50. data/lib/granite/form/model/associations/reflections/singular.rb +37 -0
  51. data/lib/granite/form/model/associations/validations.rb +41 -0
  52. data/lib/granite/form/model/associations.rb +120 -0
  53. data/lib/granite/form/model/attributes/attribute.rb +75 -0
  54. data/lib/granite/form/model/attributes/base.rb +134 -0
  55. data/lib/granite/form/model/attributes/collection.rb +19 -0
  56. data/lib/granite/form/model/attributes/dictionary.rb +28 -0
  57. data/lib/granite/form/model/attributes/localized.rb +44 -0
  58. data/lib/granite/form/model/attributes/reference_many.rb +21 -0
  59. data/lib/granite/form/model/attributes/reference_one.rb +52 -0
  60. data/lib/granite/form/model/attributes/reflections/attribute.rb +61 -0
  61. data/lib/granite/form/model/attributes/reflections/base.rb +62 -0
  62. data/lib/granite/form/model/attributes/reflections/collection.rb +12 -0
  63. data/lib/granite/form/model/attributes/reflections/dictionary.rb +15 -0
  64. data/lib/granite/form/model/attributes/reflections/localized.rb +45 -0
  65. data/lib/granite/form/model/attributes/reflections/reference_many.rb +12 -0
  66. data/lib/granite/form/model/attributes/reflections/reference_one.rb +49 -0
  67. data/lib/granite/form/model/attributes/reflections/represents.rb +56 -0
  68. data/lib/granite/form/model/attributes/represents.rb +67 -0
  69. data/lib/granite/form/model/attributes.rb +204 -0
  70. data/lib/granite/form/model/callbacks.rb +72 -0
  71. data/lib/granite/form/model/conventions.rb +40 -0
  72. data/lib/granite/form/model/dirty.rb +84 -0
  73. data/lib/granite/form/model/lifecycle.rb +309 -0
  74. data/lib/granite/form/model/localization.rb +26 -0
  75. data/lib/granite/form/model/persistence.rb +59 -0
  76. data/lib/granite/form/model/primary.rb +59 -0
  77. data/lib/granite/form/model/representation.rb +101 -0
  78. data/lib/granite/form/model/scopes.rb +118 -0
  79. data/lib/granite/form/model/validations/associated.rb +22 -0
  80. data/lib/granite/form/model/validations/nested.rb +56 -0
  81. data/lib/granite/form/model/validations.rb +29 -0
  82. data/lib/granite/form/model.rb +33 -0
  83. data/lib/granite/form/railtie.rb +9 -0
  84. data/lib/granite/form/undefined_class.rb +11 -0
  85. data/lib/granite/form/version.rb +5 -0
  86. data/lib/granite/form.rb +163 -0
  87. data/spec/lib/granite/form/active_record/associations_spec.rb +211 -0
  88. data/spec/lib/granite/form/active_record/nested_attributes_spec.rb +15 -0
  89. data/spec/lib/granite/form/config_spec.rb +66 -0
  90. data/spec/lib/granite/form/model/associations/embeds_many_spec.rb +706 -0
  91. data/spec/lib/granite/form/model/associations/embeds_one_spec.rb +533 -0
  92. data/spec/lib/granite/form/model/associations/nested_attributes_spec.rb +119 -0
  93. data/spec/lib/granite/form/model/associations/persistence_adapters/active_record_spec.rb +58 -0
  94. data/spec/lib/granite/form/model/associations/references_many_spec.rb +572 -0
  95. data/spec/lib/granite/form/model/associations/references_one_spec.rb +445 -0
  96. data/spec/lib/granite/form/model/associations/reflections/embeds_any_spec.rb +42 -0
  97. data/spec/lib/granite/form/model/associations/reflections/embeds_many_spec.rb +145 -0
  98. data/spec/lib/granite/form/model/associations/reflections/embeds_one_spec.rb +117 -0
  99. data/spec/lib/granite/form/model/associations/reflections/references_many_spec.rb +303 -0
  100. data/spec/lib/granite/form/model/associations/reflections/references_one_spec.rb +287 -0
  101. data/spec/lib/granite/form/model/associations/validations_spec.rb +137 -0
  102. data/spec/lib/granite/form/model/associations_spec.rb +198 -0
  103. data/spec/lib/granite/form/model/attributes/attribute_spec.rb +186 -0
  104. data/spec/lib/granite/form/model/attributes/base_spec.rb +97 -0
  105. data/spec/lib/granite/form/model/attributes/collection_spec.rb +72 -0
  106. data/spec/lib/granite/form/model/attributes/dictionary_spec.rb +100 -0
  107. data/spec/lib/granite/form/model/attributes/localized_spec.rb +103 -0
  108. data/spec/lib/granite/form/model/attributes/reflections/attribute_spec.rb +72 -0
  109. data/spec/lib/granite/form/model/attributes/reflections/base_spec.rb +56 -0
  110. data/spec/lib/granite/form/model/attributes/reflections/collection_spec.rb +37 -0
  111. data/spec/lib/granite/form/model/attributes/reflections/dictionary_spec.rb +43 -0
  112. data/spec/lib/granite/form/model/attributes/reflections/localized_spec.rb +37 -0
  113. data/spec/lib/granite/form/model/attributes/reflections/represents_spec.rb +70 -0
  114. data/spec/lib/granite/form/model/attributes/represents_spec.rb +85 -0
  115. data/spec/lib/granite/form/model/attributes_spec.rb +350 -0
  116. data/spec/lib/granite/form/model/callbacks_spec.rb +337 -0
  117. data/spec/lib/granite/form/model/conventions_spec.rb +11 -0
  118. data/spec/lib/granite/form/model/dirty_spec.rb +84 -0
  119. data/spec/lib/granite/form/model/lifecycle_spec.rb +356 -0
  120. data/spec/lib/granite/form/model/persistence_spec.rb +46 -0
  121. data/spec/lib/granite/form/model/primary_spec.rb +84 -0
  122. data/spec/lib/granite/form/model/representation_spec.rb +139 -0
  123. data/spec/lib/granite/form/model/scopes_spec.rb +86 -0
  124. data/spec/lib/granite/form/model/typecasting_spec.rb +193 -0
  125. data/spec/lib/granite/form/model/validations/associated_spec.rb +102 -0
  126. data/spec/lib/granite/form/model/validations/nested_spec.rb +164 -0
  127. data/spec/lib/granite/form/model/validations_spec.rb +31 -0
  128. data/spec/lib/granite/form/model_spec.rb +10 -0
  129. data/spec/lib/granite/form_spec.rb +11 -0
  130. data/spec/shared/nested_attribute_examples.rb +332 -0
  131. data/spec/spec_helper.rb +50 -0
  132. data/spec/support/model_helpers.rb +10 -0
  133. data/spec/support/muffle_helper.rb +7 -0
  134. metadata +403 -0
@@ -0,0 +1,186 @@
1
+ require 'spec_helper'
2
+
3
+ describe Granite::Form::Model::Attributes::Attribute do
4
+ before { stub_model(:dummy) }
5
+
6
+ def attribute(*args)
7
+ options = args.extract_options!
8
+ Dummy.add_attribute(Granite::Form::Model::Attributes::Reflections::Attribute, :field, {type: Object}.merge(options))
9
+ Dummy.new.attribute(:field)
10
+ end
11
+
12
+ describe '#read' do
13
+ let(:field) { attribute(type: String, normalizer: ->(v) { v ? v.strip : v }, default: :world, enum: %w[hello 42 world]) }
14
+
15
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq('world') }
16
+ specify { expect(field.tap { |r| r.write(:world) }.read).to eq('world') }
17
+ specify { expect(field.tap { |r| r.write('hello') }.read).to eq('hello') }
18
+ specify { expect(field.tap { |r| r.write(' hello ') }.read).to eq(nil) }
19
+ specify { expect(field.tap { |r| r.write(42) }.read).to eq('42') }
20
+ specify { expect(field.tap { |r| r.write(43) }.read).to eq(nil) }
21
+ specify { expect(field.tap { |r| r.write('') }.read).to eq(nil) }
22
+
23
+ context ':readonly' do
24
+ specify { expect(attribute(readonly: true, default: :world).tap { |r| r.write('string') }.read).to eq(:world) }
25
+ end
26
+ end
27
+
28
+ describe '#read_before_type_cast' do
29
+ let(:field) { attribute(type: String, normalizer: ->(v) { v.strip }, default: :world, enum: %w[hello 42 world]) }
30
+
31
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq(:world) }
32
+ specify { expect(field.tap { |r| r.write(:world) }.read_before_type_cast).to eq(:world) }
33
+ specify { expect(field.tap { |r| r.write('hello') }.read_before_type_cast).to eq('hello') }
34
+ specify { expect(field.tap { |r| r.write(42) }.read_before_type_cast).to eq(42) }
35
+ specify { expect(field.tap { |r| r.write(43) }.read_before_type_cast).to eq(43) }
36
+ specify { expect(field.tap { |r| r.write('') }.read_before_type_cast).to eq('') }
37
+
38
+ context ':readonly' do
39
+ specify { expect(attribute(readonly: true, default: :world).tap { |r| r.write('string') }.read_before_type_cast).to eq(:world) }
40
+ end
41
+ end
42
+
43
+ describe '#default' do
44
+ before { allow_any_instance_of(Dummy).to receive_messages(value: 42) }
45
+
46
+ specify { expect(attribute.default).to eq(nil) }
47
+ specify { expect(attribute(default: 'hello').default).to eq('hello') }
48
+ specify { expect(attribute(default: -> { value }).default).to eq(42) }
49
+ specify { expect(attribute(default: ->(object) { object.value }).default).to eq(42) }
50
+ specify { expect(attribute(default: ->(*args) { args.first.value }).default).to eq(42) }
51
+ end
52
+
53
+ describe '#defaultize' do
54
+ specify { expect(attribute.defaultize(nil)).to be_nil }
55
+ specify { expect(attribute(default: 'hello').defaultize(nil)).to eq('hello') }
56
+ specify { expect(attribute(default: 'hello').defaultize('world')).to eq('world') }
57
+ specify { expect(attribute(default: false, type: Boolean).defaultize(nil)).to eq(false) }
58
+ end
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
+ describe '#enum' do
106
+ before { allow_any_instance_of(Dummy).to receive_messages(value: 1..5) }
107
+
108
+ specify { expect(attribute.enum).to eq([].to_set) }
109
+ specify { expect(attribute(enum: []).enum).to eq([].to_set) }
110
+ specify { expect(attribute(enum: 'hello').enum).to eq(['hello'].to_set) }
111
+ specify { expect(attribute(enum: %w[hello world]).enum).to eq(%w[hello world].to_set) }
112
+ specify { expect(attribute(enum: [1..5]).enum).to eq([1..5].to_set) }
113
+ specify { expect(attribute(enum: 1..5).enum).to eq((1..5).to_a.to_set) }
114
+ specify { expect(attribute(enum: -> { 1..5 }).enum).to eq((1..5).to_a.to_set) }
115
+ specify { expect(attribute(enum: -> { 'hello' }).enum).to eq(['hello'].to_set) }
116
+ specify { expect(attribute(enum: -> { ['hello', 42] }).enum).to eq(['hello', 42].to_set) }
117
+ specify { expect(attribute(enum: -> { value }).enum).to eq((1..5).to_a.to_set) }
118
+ specify { expect(attribute(enum: ->(object) { object.value }).enum).to eq((1..5).to_a.to_set) }
119
+ end
120
+
121
+ describe '#enumerize' do
122
+ specify { expect(attribute.enumerize('anything')).to eq('anything') }
123
+ specify { expect(attribute(enum: ['hello', 42]).enumerize('hello')).to eq('hello') }
124
+ specify { expect(attribute(enum: ['hello', 42]).enumerize('world')).to eq(nil) }
125
+ specify { expect(attribute(enum: -> { 'hello' }).enumerize('hello')).to eq('hello') }
126
+ specify { expect(attribute(enum: -> { 1..5 }).enumerize(2)).to eq(2) }
127
+ specify { expect(attribute(enum: -> { 1..5 }).enumerize(42)).to eq(nil) }
128
+ end
129
+
130
+ describe '#normalize' do
131
+ specify { expect(attribute.normalize(' hello ')).to eq(' hello ') }
132
+ specify { expect(attribute(normalizer: ->(v) { v.strip }).normalize(' hello ')).to eq('hello') }
133
+ specify { expect(attribute(normalizer: [->(v) { v.strip }, ->(v) { v.first(4) }]).normalize(' hello ')).to eq('hell') }
134
+ specify { expect(attribute(normalizer: [->(v) { v.first(4) }, ->(v) { v.strip }]).normalize(' hello ')).to eq('hel') }
135
+
136
+ context do
137
+ before { allow_any_instance_of(Dummy).to receive_messages(value: 'value') }
138
+ let(:other) { 'other' }
139
+
140
+ specify { expect(attribute(normalizer: ->(_v) { value }).normalize(' hello ')).to eq('value') }
141
+ specify { expect(attribute(normalizer: ->(_v, object) { object.value }).normalize(' hello ')).to eq('value') }
142
+ specify { expect(attribute(normalizer: ->(_v, _object) { other }).normalize(' hello ')).to eq('other') }
143
+ end
144
+
145
+ context 'integration' do
146
+ before do
147
+ allow(Granite::Form).to receive_messages(config: Granite::Form::Config.send(:new))
148
+ Granite::Form.normalizer(:strip) { |value, _, _| value.strip }
149
+ Granite::Form.normalizer(:trim) do |value, options, _attribute|
150
+ value.first(length || options[:length] || 2)
151
+ end
152
+ Granite::Form.normalizer(:reset) do |value, _options, attribute|
153
+ empty = value.respond_to?(:empty?) ? value.empty? : value.nil?
154
+ empty ? attribute.default : value
155
+ end
156
+ end
157
+
158
+ let(:length) { nil }
159
+
160
+ specify { expect(attribute(normalizer: :strip).normalize(' hello ')).to eq('hello') }
161
+ specify { expect(attribute(normalizer: %i[strip trim]).normalize(' hello ')).to eq('he') }
162
+ specify { expect(attribute(normalizer: %i[trim strip]).normalize(' hello ')).to eq('h') }
163
+ specify { expect(attribute(normalizer: [:strip, {trim: {length: 4}}]).normalize(' hello ')).to eq('hell') }
164
+ specify { expect(attribute(normalizer: {strip: {}, trim: {length: 4}}).normalize(' hello ')).to eq('hell') }
165
+ specify do
166
+ expect(attribute(normalizer: [:strip, {trim: {length: 4}}, ->(v) { v.last(2) }])
167
+ .normalize(' hello ')).to eq('ll')
168
+ end
169
+ specify { expect(attribute(normalizer: :reset).normalize('')).to eq(nil) }
170
+ specify { expect(attribute(normalizer: %i[strip reset]).normalize(' ')).to eq(nil) }
171
+ specify { expect(attribute(normalizer: :reset, default: '!!!').normalize(nil)).to eq('!!!') }
172
+ specify { expect(attribute(normalizer: :reset, default: '!!!').normalize('')).to eq('!!!') }
173
+
174
+ context do
175
+ let(:length) { 3 }
176
+
177
+ specify { expect(attribute(normalizer: [:strip, {trim: {length: 4}}]).normalize(' hello ')).to eq('hel') }
178
+ specify { expect(attribute(normalizer: {strip: {}, trim: {length: 4}}).normalize(' hello ')).to eq('hel') }
179
+ specify do
180
+ expect(attribute(normalizer: [:strip, {trim: {length: 4}}, ->(v) { v.last(2) }])
181
+ .normalize(' hello ')).to eq('el')
182
+ end
183
+ end
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ describe Granite::Form::Model::Attributes::Base do
4
+ before { stub_model(:dummy) }
5
+
6
+ def attribute(*args)
7
+ options = args.extract_options!
8
+ Dummy.add_attribute(Granite::Form::Model::Attributes::Reflections::Base, :field, options)
9
+ Dummy.new.attribute(:field)
10
+ end
11
+
12
+ describe '#read' do
13
+ let(:field) { attribute(type: String, normalizer: ->(v) { v ? v.strip : v }, default: :world, enum: %w[hello 42 world]) }
14
+ let(:object) { Object.new }
15
+
16
+ specify { expect(field.tap { |r| r.write(nil) }.read).to be_nil }
17
+ specify { expect(field.tap { |r| r.write('') }.read).to eq('') }
18
+ specify { expect(field.tap { |r| r.write(:world) }.read).to eq(:world) }
19
+ specify { expect(field.tap { |r| r.write(object) }.read).to eq(object) }
20
+
21
+ context ':readonly' do
22
+ specify { expect(attribute(readonly: true).tap { |r| r.write('string') }.read).to be_nil }
23
+ end
24
+ end
25
+
26
+ describe '#read_before_type_cast' do
27
+ let(:field) { attribute(type: String, normalizer: ->(v) { v.strip }, default: :world, enum: %w[hello 42 world]) }
28
+ let(:object) { Object.new }
29
+
30
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to be_nil }
31
+ specify { expect(field.tap { |r| r.write('') }.read_before_type_cast).to eq('') }
32
+ specify { expect(field.tap { |r| r.write(:world) }.read_before_type_cast).to eq(:world) }
33
+ specify { expect(field.tap { |r| r.write(object) }.read_before_type_cast).to eq(object) }
34
+
35
+ context ':readonly' do
36
+ specify { expect(attribute(readonly: true).tap { |r| r.write('string') }.read_before_type_cast).to be_nil }
37
+ end
38
+ end
39
+
40
+ describe '#came_from_user?' do
41
+ let(:field) { attribute(type: String, default: 'world') }
42
+ let(:object) { Object.new }
43
+
44
+ specify { expect(field.came_from_user?).to eq(false) }
45
+ specify { expect(field.tap { |r| r.write('value') }.came_from_user?).to eq(true) }
46
+ end
47
+
48
+ describe '#came_from_default?' do
49
+ let(:field) { attribute(type: String, default: 'world') }
50
+ let(:object) { Object.new }
51
+
52
+ specify { expect(field.came_from_default?).to eq(true) }
53
+ specify { expect(field.tap { |r| r.write('value') }.came_from_default?).to eq(false) }
54
+ end
55
+
56
+ describe '#value_present?' do
57
+ let(:field) { attribute }
58
+
59
+ specify { expect(field.tap { |r| r.write(0) }).to be_value_present }
60
+ specify { expect(field.tap { |r| r.write(42) }).to be_value_present }
61
+ specify { expect(field.tap { |r| r.write(true) }).to be_value_present }
62
+ specify { expect(field.tap { |r| r.write(false) }).to be_value_present }
63
+ specify { expect(field.tap { |r| r.write(nil) }).not_to be_value_present }
64
+ specify { expect(field.tap { |r| r.write('') }).not_to be_value_present }
65
+ specify { expect(field.tap { |r| r.write(:world) }).to be_value_present }
66
+ specify { expect(field.tap { |r| r.write(Object.new) }).to be_value_present }
67
+ specify { expect(field.tap { |r| r.write([]) }).not_to be_value_present }
68
+ specify { expect(field.tap { |r| r.write([42]) }).to be_value_present }
69
+ specify { expect(field.tap { |r| r.write({}) }).not_to be_value_present }
70
+ specify { expect(field.tap { |r| r.write(hello: 42) }).to be_value_present }
71
+ end
72
+
73
+ describe '#query' do
74
+ let(:field) { attribute }
75
+
76
+ specify { expect(field.tap { |r| r.write(0) }.query).to be(false) }
77
+ specify { expect(field.tap { |r| r.write(42) }.query).to be(true) }
78
+ specify { expect(field.tap { |r| r.write(true) }.query).to be(true) }
79
+ specify { expect(field.tap { |r| r.write(false) }.query).to be(false) }
80
+ specify { expect(field.tap { |r| r.write(nil) }.query).to be(false) }
81
+ specify { expect(field.tap { |r| r.write('') }.query).to be(false) }
82
+ specify { expect(field.tap { |r| r.write(:world) }.query).to be(true) }
83
+ specify { expect(field.tap { |r| r.write(Object.new) }.query).to be(true) }
84
+ specify { expect(field.tap { |r| r.write([]) }.query).to be(false) }
85
+ specify { expect(field.tap { |r| r.write([42]) }.query).to be(true) }
86
+ specify { expect(field.tap { |r| r.write({}) }.query).to be(false) }
87
+ specify { expect(field.tap { |r| r.write(hello: 42) }.query).to be(true) }
88
+ end
89
+
90
+ describe '#readonly?' do
91
+ specify { expect(attribute).not_to be_readonly }
92
+ specify { expect(attribute(readonly: false)).not_to be_readonly }
93
+ specify { expect(attribute(readonly: true)).to be_readonly }
94
+ specify { expect(attribute(readonly: -> { false })).not_to be_readonly }
95
+ specify { expect(attribute(readonly: -> { true })).to be_readonly }
96
+ end
97
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe Granite::Form::Model::Attributes::Collection do
4
+ before { stub_model(:dummy) }
5
+
6
+ def attribute(*args)
7
+ options = args.extract_options!
8
+ Dummy.add_attribute(Granite::Form::Model::Attributes::Reflections::Collection, :field, options)
9
+ Dummy.new.attribute(:field)
10
+ end
11
+
12
+ describe '#read' do
13
+ let(:field) { attribute(type: String, normalizer: ->(v) { v.uniq }, default: :world, enum: %w[hello 42]) }
14
+
15
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq([]) }
16
+ specify { expect(field.tap { |r| r.write([nil]) }.read).to eq([nil]) }
17
+ specify { expect(field.tap { |r| r.write('hello') }.read).to eq(['hello']) }
18
+ specify { expect(field.tap { |r| r.write([42]) }.read).to eq(['42']) }
19
+ specify { expect(field.tap { |r| r.write([43]) }.read).to eq([nil]) }
20
+ specify { expect(field.tap { |r| r.write([43, 44]) }.read).to eq([nil]) }
21
+ specify { expect(field.tap { |r| r.write(['']) }.read).to eq([nil]) }
22
+ specify { expect(field.tap { |r| r.write(['hello', 42]) }.read).to eq(%w[hello 42]) }
23
+ specify { expect(field.tap { |r| r.write(['hello', false]) }.read).to eq(['hello', nil]) }
24
+
25
+ context do
26
+ let(:field) { attribute(type: String, normalizer: ->(v) { v.uniq }, default: :world) }
27
+
28
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq([]) }
29
+ specify { expect(field.tap { |r| r.write([nil, nil]) }.read).to eq(['world']) }
30
+ specify { expect(field.tap { |r| r.write('hello') }.read).to eq(['hello']) }
31
+ specify { expect(field.tap { |r| r.write([42]) }.read).to eq(['42']) }
32
+ specify { expect(field.tap { |r| r.write(['']) }.read).to eq(['']) }
33
+ end
34
+ end
35
+
36
+ describe '#read_before_type_cast' do
37
+ let(:field) { attribute(type: String, default: :world, enum: %w[hello 42]) }
38
+
39
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq([]) }
40
+ specify { expect(field.tap { |r| r.write([nil]) }.read_before_type_cast).to eq([:world]) }
41
+ specify { expect(field.tap { |r| r.write('hello') }.read_before_type_cast).to eq(['hello']) }
42
+ specify { expect(field.tap { |r| r.write([42]) }.read_before_type_cast).to eq([42]) }
43
+ specify { expect(field.tap { |r| r.write([43]) }.read_before_type_cast).to eq([43]) }
44
+ specify { expect(field.tap { |r| r.write([43, 44]) }.read_before_type_cast).to eq([43, 44]) }
45
+ specify { expect(field.tap { |r| r.write(['']) }.read_before_type_cast).to eq(['']) }
46
+ specify { expect(field.tap { |r| r.write(['hello', 42]) }.read_before_type_cast).to eq(['hello', 42]) }
47
+ specify { expect(field.tap { |r| r.write(['hello', false]) }.read_before_type_cast).to eq(['hello', false]) }
48
+
49
+ context do
50
+ let(:field) { attribute(type: String, default: :world) }
51
+
52
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq([]) }
53
+ specify { expect(field.tap { |r| r.write([nil, nil]) }.read_before_type_cast).to eq(%i[world world]) }
54
+ specify { expect(field.tap { |r| r.write('hello') }.read_before_type_cast).to eq(['hello']) }
55
+ specify { expect(field.tap { |r| r.write([42]) }.read_before_type_cast).to eq([42]) }
56
+ specify { expect(field.tap { |r| r.write(['']) }.read_before_type_cast).to eq(['']) }
57
+ end
58
+ end
59
+
60
+ context 'integration' do
61
+ before do
62
+ stub_model(:post) do
63
+ collection :tags, String
64
+ end
65
+ end
66
+
67
+ specify { expect(Post.new(tags: ['hello', 42]).tags).to eq(%w[hello 42]) }
68
+ specify { expect(Post.new(tags: ['hello', 42]).tags_before_type_cast).to eq(['hello', 42]) }
69
+ specify { expect(Post.new.tags?).to eq(false) }
70
+ specify { expect(Post.new(tags: ['hello', 42]).tags?).to eq(true) }
71
+ end
72
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ describe Granite::Form::Model::Attributes::Dictionary do
4
+ before { stub_model(:dummy) }
5
+
6
+ def attribute(*args)
7
+ options = args.extract_options!
8
+ Dummy.add_attribute(Granite::Form::Model::Attributes::Reflections::Dictionary, :field, options)
9
+ Dummy.new.attribute(:field)
10
+ end
11
+
12
+ describe '#read' do
13
+ let(:field) do
14
+ attribute(type: String,
15
+ normalizer: ->(val) { val.delete_if { |_, v| v.nil? } },
16
+ default: :world, enum: %w[hello 42])
17
+ end
18
+
19
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
20
+ specify { expect(field.tap { |r| r.write({}) }.read).to eq({}) }
21
+ specify { expect(field.tap { |r| r.write(a: nil) }.read).to eq({}) }
22
+ specify { expect(field.tap { |r| r.write(a: '') }.read).to eq({}) }
23
+ specify { expect(field.tap { |r| r.write(a: 1) }.read).to eq({}) }
24
+ specify { expect(field.tap { |r| r.write(a: 42) }.read).to eq('a' => '42') }
25
+ specify { expect(field.tap { |r| r.write(a: 'hello', b: '42') }.read).to eq('a' => 'hello', 'b' => '42') }
26
+ specify { expect(field.tap { |r| r.write(a: 'hello', b: '1') }.read).to eq('a' => 'hello') }
27
+ specify { expect(field.tap { |r| r.write(a: 'hello', x: '42') }.read).to eq('a' => 'hello', 'x' => '42') }
28
+
29
+ context do
30
+ let(:field) do
31
+ attribute(type: String,
32
+ normalizer: ->(val) { val.delete_if { |_, v| v.nil? } },
33
+ default: :world)
34
+ end
35
+
36
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
37
+ specify { expect(field.tap { |r| r.write({}) }.read).to eq({}) }
38
+ specify { expect(field.tap { |r| r.write(a: 1) }.read).to eq('a' => '1') }
39
+ specify { expect(field.tap { |r| r.write(a: nil, b: nil) }.read).to eq('a' => 'world', 'b' => 'world') }
40
+ specify { expect(field.tap { |r| r.write(a: '') }.read).to eq('a' => '') }
41
+ end
42
+
43
+ context 'with :keys' do
44
+ let(:field) { attribute(type: String, keys: ['a', :b]) }
45
+
46
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
47
+ specify { expect(field.tap { |r| r.write({}) }.read).to eq({}) }
48
+ specify { expect(field.tap { |r| r.write(a: 1, 'b' => 2, c: 3, 'd' => 4) }.read).to eq('a' => '1', 'b' => '2') }
49
+ specify { expect(field.tap { |r| r.write(a: 1, c: 3, 'd' => 4) }.read).to eq('a' => '1') }
50
+ end
51
+ end
52
+
53
+ describe '#read_before_type_cast' do
54
+ let(:field) { attribute(type: String, default: :world, enum: %w[hello 42]) }
55
+
56
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
57
+ specify { expect(field.tap { |r| r.write({}) }.read_before_type_cast).to eq({}) }
58
+ specify { expect(field.tap { |r| r.write(a: 1) }.read_before_type_cast).to eq('a' => 1) }
59
+ specify { expect(field.tap { |r| r.write(a: nil) }.read_before_type_cast).to eq('a' => :world) }
60
+ specify { expect(field.tap { |r| r.write(a: '') }.read_before_type_cast).to eq('a' => '') }
61
+ specify { expect(field.tap { |r| r.write(a: 42) }.read_before_type_cast).to eq('a' => 42) }
62
+ specify { expect(field.tap { |r| r.write(a: 'hello', b: '42') }.read_before_type_cast).to eq('a' => 'hello', 'b' => '42') }
63
+ specify { expect(field.tap { |r| r.write(a: 'hello', b: '1') }.read_before_type_cast).to eq('a' => 'hello', 'b' => '1') }
64
+ specify { expect(field.tap { |r| r.write(a: 'hello', x: '42') }.read_before_type_cast).to eq('a' => 'hello', 'x' => '42') }
65
+
66
+ context do
67
+ let(:field) { attribute(type: String, default: :world) }
68
+
69
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
70
+ specify { expect(field.tap { |r| r.write({}) }.read_before_type_cast).to eq({}) }
71
+ specify { expect(field.tap { |r| r.write(a: 1) }.read_before_type_cast).to eq('a' => 1) }
72
+ specify { expect(field.tap { |r| r.write(a: nil, b: nil) }.read_before_type_cast).to eq('a' => :world, 'b' => :world) }
73
+ specify { expect(field.tap { |r| r.write(a: '') }.read_before_type_cast).to eq('a' => '') }
74
+ end
75
+
76
+ context 'with :keys' do
77
+ let(:field) { attribute(type: String, keys: ['a', :b]) }
78
+
79
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
80
+ specify { expect(field.tap { |r| r.write({}) }.read_before_type_cast).to eq({}) }
81
+ specify do
82
+ expect(field.tap { |r| r.write(a: 1, 'b' => 2, c: 3, 'd' => 4) }.read_before_type_cast)
83
+ .to eq('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4)
84
+ end
85
+ end
86
+ end
87
+
88
+ context 'integration' do
89
+ before do
90
+ stub_model(:post) do
91
+ dictionary :options, type: String
92
+ end
93
+ end
94
+
95
+ specify { expect(Post.new(options: {a: 'hello', b: 42}).options).to eq('a' => 'hello', 'b' => '42') }
96
+ specify { expect(Post.new(options: {a: 'hello', b: 42}).options_before_type_cast).to eq('a' => 'hello', 'b' => 42) }
97
+ specify { expect(Post.new.options?).to eq(false) }
98
+ specify { expect(Post.new(options: {a: 'hello', b: 42}).options?).to eq(true) }
99
+ end
100
+ end
@@ -0,0 +1,103 @@
1
+ require 'spec_helper'
2
+
3
+ describe Granite::Form::Model::Attributes::Localized do
4
+ before { stub_model(:dummy) }
5
+
6
+ def attribute(*args)
7
+ options = args.extract_options!
8
+ Dummy.add_attribute(Granite::Form::Model::Attributes::Reflections::Localized, :field, options)
9
+ described_class.new('field', Dummy.new)
10
+ end
11
+
12
+ describe '#read' do
13
+ let(:field) { attribute(type: String, default: :world, enum: %w[hello 42]) }
14
+
15
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
16
+ specify { expect(field.tap { |r| r.write(en: 'hello') }.read).to eq('en' => 'hello') }
17
+ specify { expect(field.tap { |r| r.write(en: 42) }.read).to eq('en' => '42') }
18
+ specify { expect(field.tap { |r| r.write(en: 43) }.read).to eq('en' => nil) }
19
+ specify { expect(field.tap { |r| r.write(en: '') }.read).to eq('en' => nil) }
20
+ specify { expect(field.tap { |r| r.write(en: nil) }.read).to eq('en' => nil) }
21
+ specify { expect(field.tap { |r| r.write(en: 'hello', ru: 42) }.read).to eq('en' => 'hello', 'ru' => '42') }
22
+
23
+ context do
24
+ let(:field) { attribute(type: String, default: :world) }
25
+
26
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
27
+ specify { expect(field.tap { |r| r.write(en: 'hello') }.read).to eq('en' => 'hello') }
28
+ specify { expect(field.tap { |r| r.write(en: 42) }.read).to eq('en' => '42') }
29
+ specify { expect(field.tap { |r| r.write(en: '') }.read).to eq('en' => '') }
30
+ specify { expect(field.tap { |r| r.write(en: nil) }.read).to eq('en' => 'world') }
31
+ end
32
+ end
33
+
34
+ describe '#read_before_type_cast' do
35
+ let(:field) { attribute(type: String, default: :world, enum: %w[hello 42]) }
36
+
37
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
38
+ specify { expect(field.tap { |r| r.write(en: 'hello') }.read_before_type_cast).to eq('en' => 'hello') }
39
+ specify { expect(field.tap { |r| r.write(en: 42) }.read_before_type_cast).to eq('en' => 42) }
40
+ specify { expect(field.tap { |r| r.write(en: 43) }.read_before_type_cast).to eq('en' => 43) }
41
+ specify { expect(field.tap { |r| r.write(en: '') }.read_before_type_cast).to eq('en' => '') }
42
+ specify { expect(field.tap { |r| r.write(en: nil) }.read_before_type_cast).to eq('en' => :world) }
43
+ specify { expect(field.tap { |r| r.write(en: 'hello', ru: 42) }.read_before_type_cast).to eq('en' => 'hello', 'ru' => 42) }
44
+
45
+ context do
46
+ let(:field) { attribute(type: String, default: :world) }
47
+
48
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
49
+ specify { expect(field.tap { |r| r.write(en: 'hello') }.read_before_type_cast).to eq('en' => 'hello') }
50
+ specify { expect(field.tap { |r| r.write(en: 42) }.read_before_type_cast).to eq('en' => 42) }
51
+ specify { expect(field.tap { |r| r.write(en: '') }.read_before_type_cast).to eq('en' => '') }
52
+ specify { expect(field.tap { |r| r.write(en: nil) }.read_before_type_cast).to eq('en' => :world) }
53
+ end
54
+ end
55
+
56
+ context 'integration' do
57
+ let(:klass) do
58
+ Class.new do
59
+ include Granite::Form::Model
60
+ include Granite::Form::Model::Localization
61
+
62
+ localized :name, type: String
63
+ end
64
+ end
65
+ let(:translations) { {en: 'Hello', ru: 'Привет'} }
66
+ before { I18n.locale = :en }
67
+
68
+ describe '#name_translations' do
69
+ subject { klass.new name_translations: translations }
70
+ its(:name_translations) { should == translations.stringify_keys }
71
+ its(:name) { should == translations[:en] }
72
+ end
73
+
74
+ describe '#name' do
75
+ subject { klass.new name: 'Hello' }
76
+ its(:name_translations) { should == {'en' => 'Hello'} }
77
+ its(:name) { should == 'Hello' }
78
+ end
79
+
80
+ describe '#name_before_type_cast' do
81
+ let(:object) { Object.new }
82
+ subject { klass.new name: object }
83
+ its(:name_before_type_cast) { should == object }
84
+ end
85
+
86
+ context 'fallbacks' do
87
+ subject { klass.new name_translations: {ru: 'Привет'} }
88
+ context do
89
+ its(:name) { should be_nil }
90
+ end
91
+
92
+ context do
93
+ before do
94
+ require 'i18n/backend/fallbacks'
95
+ I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
96
+ I18n.fallbacks[:en] = [:ru]
97
+ end
98
+ after { I18n.fallbacks = false }
99
+ its(:name) { should == 'Привет' }
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe Granite::Form::Model::Attributes::Reflections::Attribute do
4
+ def reflection(options = {})
5
+ described_class.new(:field, options)
6
+ end
7
+
8
+ describe '.build' do
9
+ before { stub_class(:target) }
10
+
11
+ specify { expect(described_class.build(Class.new, Target, :field, String).type).to eq(String) }
12
+ specify { expect(described_class.build(Class.new, Target, :field) {}.defaultizer).to be_a(Proc) }
13
+ specify do
14
+ described_class.build(Class.new, Target, :field)
15
+
16
+ expect(Target).to be_method_defined(:field)
17
+ expect(Target).to be_method_defined(:field=)
18
+ expect(Target).to be_method_defined(:field?)
19
+ expect(Target).to be_method_defined(:field_before_type_cast)
20
+ expect(Target).to be_method_defined(:field_default)
21
+ expect(Target).to be_method_defined(:field_values)
22
+ end
23
+ end
24
+
25
+ describe '#generate_methods' do
26
+ before { stub_class(:target) }
27
+
28
+ specify do
29
+ described_class.generate_methods(:field_alias, Target)
30
+
31
+ expect(Target).to be_method_defined(:field_alias)
32
+ expect(Target).to be_method_defined(:field_alias=)
33
+ expect(Target).to be_method_defined(:field_alias?)
34
+ expect(Target).to be_method_defined(:field_alias_before_type_cast)
35
+ expect(Target).to be_method_defined(:field_alias_default)
36
+ expect(Target).to be_method_defined(:field_alias_values)
37
+ end
38
+ end
39
+
40
+ describe '#defaultizer' do
41
+ specify { expect(reflection.defaultizer).to be_nil }
42
+ specify { expect(reflection(default: 42).defaultizer).to eq(42) }
43
+ specify { expect(reflection(default: -> {}).defaultizer).to be_a Proc }
44
+ end
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
+ describe '#enumerizer' do
59
+ specify { expect(reflection.enumerizer).to be_nil }
60
+ specify { expect(reflection(enum: 42).enumerizer).to eq(42) }
61
+ specify { expect(reflection(enum: -> {}).enumerizer).to be_a Proc }
62
+ specify { expect(reflection(in: 42).enumerizer).to eq(42) }
63
+ specify { expect(reflection(in: -> {}).enumerizer).to be_a Proc }
64
+ specify { expect(reflection(enum: 42, in: -> {}).enumerizer).to eq(42) }
65
+ end
66
+
67
+ describe '#normalizers' do
68
+ specify { expect(reflection.normalizers).to eq([]) }
69
+ specify { expect(reflection(normalizer: -> {}).normalizers).to be_a Array }
70
+ specify { expect(reflection(normalizer: -> {}).normalizers.first).to be_a Proc }
71
+ end
72
+ end