mini_form 0.2.0 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.projections.json +8 -0
- data/.rubocop.yml +34 -2
- data/.travis.yml +5 -3
- data/CHANGELOG.md +17 -0
- data/README.md +6 -6
- data/Rakefile +1 -1
- data/lib/mini_form/errors.rb +2 -1
- data/lib/mini_form/model.rb +30 -7
- data/lib/mini_form/nested_validator.rb +9 -0
- data/lib/mini_form/version.rb +1 -1
- data/mini_form.gemspec +6 -6
- data/spec/mini_form_spec.rb +43 -8
- metadata +15 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4fae1bbe2eb9f254e0315e13364a0d9a19a87515061f066a37fd89689be9c657
|
4
|
+
data.tar.gz: 34c5ac1537cf058c0c67cdaa7ef4c88dd7aaaef63412cbabddbad5e13be73435
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ada501cc629ff6fd8701162248d095868bf2a7a9ac0e7e23f74caae5d08d1f8cbd31d9202fd47f52283cbb3117cd8c0d1a2705b9c074a09a6c95cb622ac2e4ee
|
7
|
+
data.tar.gz: 8cb38e2bf06594be100078b2d2b0ee10c4d5a356808ba4249fc2f63f060478619c97dc8d776004d20c5f52cb2b1dfef5b93099ca9307379c7921006a40bfa0ee
|
data/.gitignore
CHANGED
data/.projections.json
ADDED
data/.rubocop.yml
CHANGED
@@ -1,16 +1,32 @@
|
|
1
1
|
require: rubocop-rspec
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
Rails:
|
4
|
+
Enabled: true
|
5
5
|
Exclude:
|
6
6
|
- example/db/**/*
|
7
7
|
- example/config/**/*
|
8
8
|
- search_object.gemspec
|
9
9
|
|
10
|
+
# Disables "Module definition is too long"
|
11
|
+
ModuleLength:
|
12
|
+
Enabled: false
|
13
|
+
|
10
14
|
# Disables "Line is too long"
|
11
15
|
LineLength:
|
12
16
|
Enabled: false
|
13
17
|
|
18
|
+
# Disables "Method is too long"
|
19
|
+
MethodLength:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
# Disables "Assignment Branch Condition size for included is too high"
|
23
|
+
AbcSize:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
# Disables "Block has too many lines"
|
27
|
+
BlockLength:
|
28
|
+
Enabled: false
|
29
|
+
|
14
30
|
# Disables "Missing top-level class documentation comment"
|
15
31
|
Documentation:
|
16
32
|
Enabled: false
|
@@ -22,3 +38,19 @@ Style/EachWithObject:
|
|
22
38
|
# Disables "Prefer reduce over inject."
|
23
39
|
Style/CollectionMethods:
|
24
40
|
Enabled: false
|
41
|
+
|
42
|
+
# Disables "%i-literals should be delimited by [ and ]."
|
43
|
+
Style/PercentLiteralDelimiters:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
# Disables "Use tr instead of gsubs"
|
47
|
+
Performance/StringReplacement:
|
48
|
+
Enabled: false
|
49
|
+
|
50
|
+
# Disables "Example has too many expectations"
|
51
|
+
RSpec/MultipleExpectations:
|
52
|
+
Enabled: false
|
53
|
+
|
54
|
+
# Disables "Example has too many lines"
|
55
|
+
RSpec/ExampleLength:
|
56
|
+
Enabled: false
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,22 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## Version 0.2.4
|
4
|
+
|
5
|
+
* Rails 6+ support (@emilov)
|
6
|
+
|
7
|
+
## Version 0.2.3
|
8
|
+
|
9
|
+
* Fix handling delegated attributes prefixes in `attribute_names` (@rstankov)
|
10
|
+
|
11
|
+
## Version 0.2.2
|
12
|
+
|
13
|
+
* Bump activemodel requirements (@vestimir)
|
14
|
+
|
15
|
+
## Version 0.2.1
|
16
|
+
|
17
|
+
* Fix typo in `assignment` callbaks (@ayrton)
|
18
|
+
* Alias `assignment` to `assigment` for backwards compatibility (@ayrton)
|
19
|
+
|
3
20
|
## Version 0.2.0
|
4
21
|
|
5
22
|
* Don't expose model name on `model`. _(security fix)_
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
[![Gem Version](https://badge.fury.io/rb/mini_form.
|
2
|
-
[![Code Climate](https://codeclimate.com/github/RStankov/MiniForm.
|
3
|
-
[![Build Status](https://secure.travis-ci.org/RStankov/MiniForm.
|
4
|
-
[![Code coverage](https://coveralls.io/repos/RStankov/MiniForm/badge.
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/mini_form.svg)](http://badge.fury.io/rb/mini_form)
|
2
|
+
[![Code Climate](https://codeclimate.com/github/RStankov/MiniForm.svg)](https://codeclimate.com/github/RStankov/MiniForm)
|
3
|
+
[![Build Status](https://secure.travis-ci.org/RStankov/MiniForm.svg)](http://travis-ci.org/RStankov/MiniForm)
|
4
|
+
[![Code coverage](https://coveralls.io/repos/RStankov/MiniForm/badge.svg?branch=master)](https://coveralls.io/r/RStankov/MiniForm)
|
5
5
|
|
6
6
|
# MiniForm
|
7
7
|
|
@@ -267,11 +267,11 @@ form.id = 42 # => raises `NoMethodError`
|
|
267
267
|
<td>Meant to be overwritten.</td>
|
268
268
|
</tr>
|
269
269
|
<tr>
|
270
|
-
<td>#
|
270
|
+
<td>#before_assignment</td>
|
271
271
|
<td>Meant to be overwritten.</td>
|
272
272
|
</tr>
|
273
273
|
<tr>
|
274
|
-
<td>#
|
274
|
+
<td>#after_assignment</td>
|
275
275
|
<td>Meant to be overwritten.</td>
|
276
276
|
</tr>
|
277
277
|
<tr>
|
data/Rakefile
CHANGED
data/lib/mini_form/errors.rb
CHANGED
@@ -5,7 +5,8 @@ module MiniForm
|
|
5
5
|
def initialize(object)
|
6
6
|
@errors = object.errors
|
7
7
|
|
8
|
-
|
8
|
+
arr_obj = errors.respond_to?(:attribute_names) ? errors.attribute_names : errors.keys
|
9
|
+
super "Form validation failed for: #{arr_obj.join(', ')}"
|
9
10
|
end
|
10
11
|
end
|
11
12
|
end
|
data/lib/mini_form/model.rb
CHANGED
@@ -3,7 +3,7 @@ require 'active_model'
|
|
3
3
|
|
4
4
|
module MiniForm
|
5
5
|
module Model
|
6
|
-
def self.included(base)
|
6
|
+
def self.included(base)
|
7
7
|
base.class_eval do
|
8
8
|
include ActiveModel::Validations
|
9
9
|
include ActiveModel::Validations::Callbacks
|
@@ -15,11 +15,18 @@ module MiniForm
|
|
15
15
|
extend ClassMethods
|
16
16
|
|
17
17
|
define_model_callbacks :update
|
18
|
+
define_model_callbacks :assignment
|
19
|
+
|
20
|
+
# For backwards compatibility purpose
|
18
21
|
define_model_callbacks :assigment
|
19
22
|
|
20
23
|
before_update :before_update
|
21
24
|
after_update :after_update
|
22
25
|
|
26
|
+
before_assignment :before_assignment
|
27
|
+
after_assignment :after_assignment
|
28
|
+
|
29
|
+
# For backwards compatibility purpose
|
23
30
|
before_assigment :before_assigment
|
24
31
|
after_assigment :after_assigment
|
25
32
|
end
|
@@ -34,14 +41,16 @@ module MiniForm
|
|
34
41
|
end
|
35
42
|
|
36
43
|
def attributes=(attributes)
|
37
|
-
run_callbacks :
|
38
|
-
|
39
|
-
|
44
|
+
run_callbacks :assignment do
|
45
|
+
run_callbacks :assigment do
|
46
|
+
attributes.slice(*self.class.attribute_names).each do |name, value|
|
47
|
+
public_send "#{name}=", value
|
48
|
+
end
|
40
49
|
end
|
41
50
|
end
|
42
51
|
end
|
43
52
|
|
44
|
-
|
53
|
+
alias assign_attributes attributes=
|
45
54
|
|
46
55
|
def attributes
|
47
56
|
Hash[self.class.attribute_names.map { |name| [name, public_send(name)] }]
|
@@ -63,7 +72,7 @@ module MiniForm
|
|
63
72
|
end
|
64
73
|
|
65
74
|
def update!(attributes = {})
|
66
|
-
|
75
|
+
raise InvalidForm, self unless update attributes
|
67
76
|
self
|
68
77
|
end
|
69
78
|
|
@@ -94,6 +103,14 @@ module MiniForm
|
|
94
103
|
# noop
|
95
104
|
end
|
96
105
|
|
106
|
+
def before_assignment
|
107
|
+
# noop
|
108
|
+
end
|
109
|
+
|
110
|
+
def after_assignment
|
111
|
+
# noop
|
112
|
+
end
|
113
|
+
|
97
114
|
def before_assigment
|
98
115
|
# noop
|
99
116
|
end
|
@@ -123,7 +140,13 @@ module MiniForm
|
|
123
140
|
end
|
124
141
|
|
125
142
|
def attributes(*attributes, delegate: nil, prefix: nil, allow_nil: nil)
|
126
|
-
|
143
|
+
if prefix
|
144
|
+
attribute_names.push(*attributes.map do |name|
|
145
|
+
:"#{prefix == true ? delegate : prefix}_#{name}"
|
146
|
+
end)
|
147
|
+
else
|
148
|
+
attribute_names.push(*attributes)
|
149
|
+
end
|
127
150
|
|
128
151
|
if delegate.nil?
|
129
152
|
attr_accessor(*attributes)
|
@@ -5,6 +5,15 @@ module MiniForm
|
|
5
5
|
def validate_each(record, _, relation)
|
6
6
|
return if relation.valid?
|
7
7
|
|
8
|
+
if record.errors.respond_to?(:merge!)
|
9
|
+
# Rails 6.1+ where accessing ActiveModel::Errors as a hash has been
|
10
|
+
# deprecated and the errors array is frozen. For this reason we use the new
|
11
|
+
# method merge!() which appends the errors as NestedErrors to the array. "This is the way."
|
12
|
+
record.errors.merge!(relation.errors)
|
13
|
+
return
|
14
|
+
end
|
15
|
+
|
16
|
+
# Rails < 6.1
|
8
17
|
relation.errors.each do |name, value|
|
9
18
|
record.errors.add name, value
|
10
19
|
end
|
data/lib/mini_form/version.rb
CHANGED
data/mini_form.gemspec
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
# coding: utf-8
|
2
1
|
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require 'mini_form/version'
|
5
5
|
|
@@ -14,14 +14,14 @@ Gem::Specification.new do |spec|
|
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
17
|
-
spec.executables = spec.files.grep(
|
18
|
-
spec.test_files = spec.files.grep(
|
17
|
+
spec.executables = spec.files.grep(%r{^bin\/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(spec)\/})
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.add_dependency 'activemodel', '
|
21
|
+
spec.add_dependency 'activemodel', '>= 4.0'
|
22
22
|
|
23
|
-
spec.add_development_dependency 'bundler', '
|
24
|
-
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'bundler', '>= 2.1.4'
|
24
|
+
spec.add_development_dependency 'rake', '>= 12.3.3'
|
25
25
|
spec.add_development_dependency 'rspec', '~> 2.14'
|
26
26
|
spec.add_development_dependency 'rspec-mocks', '>= 2.12.3'
|
27
27
|
spec.add_development_dependency 'coveralls'
|
data/spec/mini_form_spec.rb
CHANGED
@@ -24,6 +24,8 @@ module MiniForm
|
|
24
24
|
attr_reader :user
|
25
25
|
|
26
26
|
attributes :name, delegate: :user
|
27
|
+
attributes :id, delegate: :user, prefix: true
|
28
|
+
attributes :name, delegate: :user, prefix: 'full'
|
27
29
|
|
28
30
|
def initialize(user)
|
29
31
|
@user = user
|
@@ -49,6 +51,10 @@ module MiniForm
|
|
49
51
|
expect(value).to eq(expected_value), message
|
50
52
|
end
|
51
53
|
|
54
|
+
def assert_respond_to(klass, method, message = nil)
|
55
|
+
expect(klass).to respond_to(method), message
|
56
|
+
end
|
57
|
+
|
52
58
|
ActiveModel::Lint::Tests.public_instance_methods.map(&:to_s).grep(/^test/).each do |method|
|
53
59
|
example(method.gsub('_', ' ')) { send method }
|
54
60
|
end
|
@@ -140,6 +146,11 @@ module MiniForm
|
|
140
146
|
it 'returns attribute names' do
|
141
147
|
expect(Example.attribute_names).to eq %i(name price)
|
142
148
|
end
|
149
|
+
|
150
|
+
it 'can handle prefixes' do
|
151
|
+
expect(ExampleWithDelegate.attribute_names).to include :user_id
|
152
|
+
expect(ExampleWithDelegate.attribute_names).to include :full_name
|
153
|
+
end
|
143
154
|
end
|
144
155
|
|
145
156
|
describe '#initialize' do
|
@@ -204,7 +215,7 @@ module MiniForm
|
|
204
215
|
|
205
216
|
model :user, attributes: %i(name), save: true
|
206
217
|
|
207
|
-
def initialize(user:
|
218
|
+
def initialize(user:)
|
208
219
|
self.user = user
|
209
220
|
end
|
210
221
|
end
|
@@ -224,9 +235,11 @@ module MiniForm
|
|
224
235
|
it 'calls "perfom" method when validation pass' do
|
225
236
|
object = ExampleForUpdate.new name: 'value'
|
226
237
|
|
227
|
-
|
238
|
+
allow(object).to receive(:perform)
|
228
239
|
|
229
240
|
object.update
|
241
|
+
|
242
|
+
expect(object).to have_received(:perform)
|
230
243
|
end
|
231
244
|
|
232
245
|
it 'calls "save" for the model' do
|
@@ -242,19 +255,37 @@ module MiniForm
|
|
242
255
|
it 'supports update callbacks' do
|
243
256
|
object = ExampleForUpdate.new name: 'value'
|
244
257
|
|
245
|
-
|
246
|
-
|
258
|
+
allow(object).to receive(:before_update)
|
259
|
+
allow(object).to receive(:after_update)
|
247
260
|
|
248
261
|
object.update
|
262
|
+
|
263
|
+
expect(object).to have_received(:before_update)
|
264
|
+
expect(object).to have_received(:after_update)
|
265
|
+
end
|
266
|
+
|
267
|
+
it 'supports legacy assig callbacks' do
|
268
|
+
object = ExampleForUpdate.new
|
269
|
+
|
270
|
+
allow(object).to receive(:before_assigment)
|
271
|
+
allow(object).to receive(:after_assigment)
|
272
|
+
|
273
|
+
object.update name: 'value'
|
274
|
+
|
275
|
+
expect(object).to have_received(:before_assigment)
|
276
|
+
expect(object).to have_received(:after_assigment)
|
249
277
|
end
|
250
278
|
|
251
279
|
it 'supports assign callbacks' do
|
252
280
|
object = ExampleForUpdate.new
|
253
281
|
|
254
|
-
|
255
|
-
|
282
|
+
allow(object).to receive(:before_assignment)
|
283
|
+
allow(object).to receive(:after_assignment)
|
256
284
|
|
257
285
|
object.update name: 'value'
|
286
|
+
|
287
|
+
expect(object).to have_received(:before_assignment)
|
288
|
+
expect(object).to have_received(:after_assignment)
|
258
289
|
end
|
259
290
|
|
260
291
|
it 'returns false when validations fail' do
|
@@ -266,9 +297,11 @@ module MiniForm
|
|
266
297
|
it 'does not call "perfom" method when validation fail' do
|
267
298
|
object = ExampleForUpdate.new name: nil
|
268
299
|
|
269
|
-
|
300
|
+
allow(object).to receive(:perform)
|
270
301
|
|
271
302
|
object.update
|
303
|
+
|
304
|
+
expect(object).not_to have_received(:perform)
|
272
305
|
end
|
273
306
|
end
|
274
307
|
|
@@ -281,9 +314,11 @@ module MiniForm
|
|
281
314
|
it 'calls update with given arguments' do
|
282
315
|
object = Example.new
|
283
316
|
|
284
|
-
|
317
|
+
allow(object).to receive(:update).and_return true
|
285
318
|
|
286
319
|
object.update! :attributes
|
320
|
+
|
321
|
+
expect(object).to have_received(:update).with(:attributes)
|
287
322
|
end
|
288
323
|
|
289
324
|
it 'raises error when update fails' do
|
metadata
CHANGED
@@ -1,57 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mini_form
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Radoslav Stankov
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '4.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '4.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 2.1.4
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 2.1.4
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 12.3.3
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 12.3.3
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -130,6 +130,7 @@ extensions: []
|
|
130
130
|
extra_rdoc_files: []
|
131
131
|
files:
|
132
132
|
- ".gitignore"
|
133
|
+
- ".projections.json"
|
133
134
|
- ".rspec"
|
134
135
|
- ".rubocop.yml"
|
135
136
|
- ".travis.yml"
|
@@ -151,7 +152,7 @@ homepage: https://github.com/RStankov/MiniForm
|
|
151
152
|
licenses:
|
152
153
|
- MIT
|
153
154
|
metadata: {}
|
154
|
-
post_install_message:
|
155
|
+
post_install_message:
|
155
156
|
rdoc_options: []
|
156
157
|
require_paths:
|
157
158
|
- lib
|
@@ -166,9 +167,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
166
167
|
- !ruby/object:Gem::Version
|
167
168
|
version: '0'
|
168
169
|
requirements: []
|
169
|
-
|
170
|
-
|
171
|
-
signing_key:
|
170
|
+
rubygems_version: 3.1.4
|
171
|
+
signing_key:
|
172
172
|
specification_version: 4
|
173
173
|
summary: Easy to use form objects in Rails projects
|
174
174
|
test_files:
|