mini_form 0.2.3 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.github/workflows/ci.yml +33 -0
- data/.rubocop.yml +17 -15
- data/CHANGELOG.md +8 -1
- data/Gemfile +2 -0
- data/README.md +21 -2
- data/Rakefile +2 -0
- data/lib/mini_form/errors.rb +4 -1
- data/lib/mini_form/model.rb +10 -1
- data/lib/mini_form/nested_validator.rb +11 -0
- data/lib/mini_form/version.rb +3 -1
- data/lib/mini_form.rb +2 -0
- data/mini_form.gemspec +4 -4
- data/spec/mini_form/nested_validator_spec.rb +20 -16
- data/spec/mini_form_spec.rb +123 -79
- data/spec/spec_helper.rb +2 -0
- metadata +23 -24
- data/.travis.yml +0 -12
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 05110a58fa226872117674c65d40a1312b20f368d2c3091abcf1f1d76ca8556f
|
|
4
|
+
data.tar.gz: 5046659d5998b2786b304bb47e0190dd21605863be6482106c9bb259453847e7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8ebdb289c2f4a3c975be13ae53e52032ff33130658cdf9b580373f9cc988d60eb8e62732280c5c71889aeac7021c50857957bfd459b689e27f8716c45e142c64
|
|
7
|
+
data.tar.gz: '078b887c19a8049f34ff73cbe6bc1a686f5b9219ee7e499c6176de023c80ad283529d4c5b56b3a673688b08ce175f0ca6d893e02034b39f9ca3d5a10b97ed3be'
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ master ]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [ master ]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
|
|
14
|
+
strategy:
|
|
15
|
+
matrix:
|
|
16
|
+
ruby-version: ['2.6', '2.7', '3.0', '3.1']
|
|
17
|
+
|
|
18
|
+
steps:
|
|
19
|
+
- uses: actions/checkout@v3
|
|
20
|
+
- name: Set up Ruby ${{ matrix.ruby-version }}
|
|
21
|
+
uses: ruby/setup-ruby@0a29871fe2b0200a17a4497bae54fe5df0d973aa # v1.115.3
|
|
22
|
+
with:
|
|
23
|
+
ruby-version: ${{ matrix.ruby-version }}
|
|
24
|
+
|
|
25
|
+
- name: Install dependencies
|
|
26
|
+
run: bundle install
|
|
27
|
+
|
|
28
|
+
- name: Run linters
|
|
29
|
+
run: bundle exec rubocop
|
|
30
|
+
|
|
31
|
+
- name: Run tests
|
|
32
|
+
run: bundle exec rspec spec
|
|
33
|
+
|
data/.rubocop.yml
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
1
|
require: rubocop-rspec
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
Enabled: true
|
|
3
|
+
AllCops:
|
|
5
4
|
Exclude:
|
|
6
|
-
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
- mini_form.gemspec
|
|
6
|
+
|
|
7
|
+
SuggestExtensions: false
|
|
8
|
+
NewCops: disable
|
|
9
9
|
|
|
10
10
|
# Disables "Module definition is too long"
|
|
11
|
-
ModuleLength:
|
|
11
|
+
Metrics/ModuleLength:
|
|
12
12
|
Enabled: false
|
|
13
13
|
|
|
14
14
|
# Disables "Line is too long"
|
|
15
|
-
LineLength:
|
|
15
|
+
Layout/LineLength:
|
|
16
16
|
Enabled: false
|
|
17
17
|
|
|
18
18
|
# Disables "Method is too long"
|
|
19
|
-
MethodLength:
|
|
19
|
+
Metrics/MethodLength:
|
|
20
20
|
Enabled: false
|
|
21
21
|
|
|
22
22
|
# Disables "Assignment Branch Condition size for included is too high"
|
|
23
|
-
AbcSize:
|
|
23
|
+
Metrics/AbcSize:
|
|
24
24
|
Enabled: false
|
|
25
25
|
|
|
26
26
|
# Disables "Block has too many lines"
|
|
27
|
-
BlockLength:
|
|
27
|
+
Metrics/BlockLength:
|
|
28
28
|
Enabled: false
|
|
29
29
|
|
|
30
30
|
# Disables "Missing top-level class documentation comment"
|
|
31
|
-
Documentation:
|
|
31
|
+
Style/Documentation:
|
|
32
32
|
Enabled: false
|
|
33
33
|
|
|
34
34
|
# Disables "Use each_with_object instead of inject"
|
|
@@ -43,10 +43,6 @@ Style/CollectionMethods:
|
|
|
43
43
|
Style/PercentLiteralDelimiters:
|
|
44
44
|
Enabled: false
|
|
45
45
|
|
|
46
|
-
# Disables "Use tr instead of gsubs"
|
|
47
|
-
Performance/StringReplacement:
|
|
48
|
-
Enabled: false
|
|
49
|
-
|
|
50
46
|
# Disables "Example has too many expectations"
|
|
51
47
|
RSpec/MultipleExpectations:
|
|
52
48
|
Enabled: false
|
|
@@ -54,3 +50,9 @@ RSpec/MultipleExpectations:
|
|
|
54
50
|
# Disables "Example has too many lines"
|
|
55
51
|
RSpec/ExampleLength:
|
|
56
52
|
Enabled: false
|
|
53
|
+
|
|
54
|
+
Style/HashSyntax:
|
|
55
|
+
Enabled: false
|
|
56
|
+
|
|
57
|
+
Lint/MissingSuper:
|
|
58
|
+
Enabled: false
|
data/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## Version 0.2.
|
|
3
|
+
## Version 0.2.5
|
|
4
|
+
|
|
5
|
+
* Added `main_model` (@rstankov)
|
|
6
|
+
|
|
7
|
+
## Version 0.2.4
|
|
4
8
|
|
|
9
|
+
* Rails 6+ support (@emilov)
|
|
10
|
+
|
|
11
|
+
## Version 0.2.3
|
|
5
12
|
|
|
6
13
|
* Fix handling delegated attributes prefixes in `attribute_names` (@rstankov)
|
|
7
14
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
[](http://badge.fury.io/rb/mini_form)
|
|
2
2
|
[](https://codeclimate.com/github/RStankov/MiniForm)
|
|
3
|
-
[](http://travis-ci.org/RStankov/MiniForm)
|
|
4
3
|
[](https://coveralls.io/r/RStankov/MiniForm)
|
|
5
4
|
|
|
6
5
|
# MiniForm
|
|
@@ -194,6 +193,26 @@ class SignUpForm
|
|
|
194
193
|
end
|
|
195
194
|
```
|
|
196
195
|
|
|
196
|
+
### Using in forms
|
|
197
|
+
|
|
198
|
+
Using `main_model` will delegate `id`, `to_param`, `persisted?` and `new_record?` to the model. Allowing you to use it in forms.
|
|
199
|
+
|
|
200
|
+
```ruby
|
|
201
|
+
class SignUpForm
|
|
202
|
+
include MiniForm::Model
|
|
203
|
+
|
|
204
|
+
main_model :user
|
|
205
|
+
|
|
206
|
+
def initialize
|
|
207
|
+
@user = User.new(account: @account)
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
```eruby
|
|
213
|
+
<% form_for SignUpForm.new %>
|
|
214
|
+
```
|
|
215
|
+
|
|
197
216
|
### Delegating model attributes
|
|
198
217
|
|
|
199
218
|
```ruby
|
|
@@ -203,7 +222,7 @@ class SignUpForm
|
|
|
203
222
|
model :user, attributes: %i(name email), read: %i(id)
|
|
204
223
|
|
|
205
224
|
def initialize
|
|
206
|
-
@user
|
|
225
|
+
@user = User.new(account: @account)
|
|
207
226
|
end
|
|
208
227
|
end
|
|
209
228
|
```
|
data/Rakefile
CHANGED
data/lib/mini_form/errors.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module MiniForm
|
|
2
4
|
class InvalidForm < StandardError
|
|
3
5
|
attr_reader :errors
|
|
@@ -5,7 +7,8 @@ module MiniForm
|
|
|
5
7
|
def initialize(object)
|
|
6
8
|
@errors = object.errors
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
arr_obj = errors.respond_to?(:attribute_names) ? errors.attribute_names : errors.keys
|
|
11
|
+
super "Form validation failed for: #{arr_obj.join(', ')}"
|
|
9
12
|
end
|
|
10
13
|
end
|
|
11
14
|
end
|
data/lib/mini_form/model.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'active_support/all'
|
|
2
4
|
require 'active_model'
|
|
3
5
|
|
|
@@ -73,6 +75,7 @@ module MiniForm
|
|
|
73
75
|
|
|
74
76
|
def update!(attributes = {})
|
|
75
77
|
raise InvalidForm, self unless update attributes
|
|
78
|
+
|
|
76
79
|
self
|
|
77
80
|
end
|
|
78
81
|
|
|
@@ -156,7 +159,7 @@ module MiniForm
|
|
|
156
159
|
end
|
|
157
160
|
end
|
|
158
161
|
|
|
159
|
-
def model(name, attributes: [], read: [], prefix: nil, allow_nil: nil, save: false) # rubocop:disable ParameterLists
|
|
162
|
+
def model(name, attributes: [], read: [], prefix: nil, allow_nil: nil, save: false) # rubocop:disable Metrics/ParameterLists
|
|
160
163
|
attr_accessor name
|
|
161
164
|
|
|
162
165
|
attributes(*attributes, delegate: name, prefix: prefix, allow_nil: allow_nil) unless attributes.empty?
|
|
@@ -167,6 +170,12 @@ module MiniForm
|
|
|
167
170
|
|
|
168
171
|
models_to_save << name if save
|
|
169
172
|
end
|
|
173
|
+
|
|
174
|
+
def main_model(model_name, **args)
|
|
175
|
+
delegate :id, :persisted?, :to_param, :new_record?, to: model_name
|
|
176
|
+
|
|
177
|
+
model model_name, **args
|
|
178
|
+
end
|
|
170
179
|
end
|
|
171
180
|
end
|
|
172
181
|
end
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'active_model'
|
|
2
4
|
|
|
3
5
|
module MiniForm
|
|
@@ -5,6 +7,15 @@ module MiniForm
|
|
|
5
7
|
def validate_each(record, _, relation)
|
|
6
8
|
return if relation.valid?
|
|
7
9
|
|
|
10
|
+
if record.errors.respond_to?(:merge!)
|
|
11
|
+
# Rails 6.1+ where accessing ActiveModel::Errors as a hash has been
|
|
12
|
+
# deprecated and the errors array is frozen. For this reason we use the new
|
|
13
|
+
# method merge!() which appends the errors as NestedErrors to the array. "This is the way."
|
|
14
|
+
record.errors.merge!(relation.errors)
|
|
15
|
+
return
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Rails < 6.1
|
|
8
19
|
relation.errors.each do |name, value|
|
|
9
20
|
record.errors.add name, value
|
|
10
21
|
end
|
data/lib/mini_form/version.rb
CHANGED
data/lib/mini_form.rb
CHANGED
data/mini_form.gemspec
CHANGED
|
@@ -20,10 +20,10 @@ Gem::Specification.new do |spec|
|
|
|
20
20
|
|
|
21
21
|
spec.add_dependency 'activemodel', '>= 4.0'
|
|
22
22
|
|
|
23
|
-
spec.add_development_dependency 'bundler', '
|
|
24
|
-
spec.add_development_dependency 'rake', '
|
|
25
|
-
spec.add_development_dependency 'rspec', '
|
|
26
|
-
spec.add_development_dependency 'rspec-mocks', '
|
|
23
|
+
spec.add_development_dependency 'bundler', '>= 2.1.4'
|
|
24
|
+
spec.add_development_dependency 'rake', '>= 12.3.3'
|
|
25
|
+
spec.add_development_dependency 'rspec', '3.12.0'
|
|
26
|
+
spec.add_development_dependency 'rspec-mocks', '3.12.0'
|
|
27
27
|
spec.add_development_dependency 'coveralls'
|
|
28
28
|
spec.add_development_dependency 'rubocop'
|
|
29
29
|
spec.add_development_dependency 'rubocop-rspec'
|
|
@@ -1,28 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'spec_helper'
|
|
2
4
|
|
|
3
|
-
module
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
include ActiveModel::Model
|
|
5
|
+
module SpecSupport
|
|
6
|
+
class Person
|
|
7
|
+
include ActiveModel::Model
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
attr_accessor :name
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
validates :name, presence: true
|
|
12
|
+
end
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
class Record
|
|
15
|
+
include ActiveModel::Validations
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
attr_accessor :user
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
end
|
|
19
|
+
def initialize(user)
|
|
20
|
+
@user = user
|
|
21
21
|
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
let(:
|
|
25
|
+
module MiniForm
|
|
26
|
+
describe NestedValidator do
|
|
27
|
+
let(:validator) { described_class.new(attributes: [:user]) }
|
|
28
|
+
let(:user) { SpecSupport::Person.new }
|
|
29
|
+
let(:record) { SpecSupport::Record.new(user) }
|
|
26
30
|
|
|
27
31
|
it 'copies errors from submodel to model' do
|
|
28
32
|
validator.validate(record)
|
data/spec/mini_form_spec.rb
CHANGED
|
@@ -1,42 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'spec_helper'
|
|
2
4
|
|
|
3
|
-
module
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
module SpecSupport
|
|
6
|
+
class User
|
|
7
|
+
include ActiveModel::Model
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
include ActiveModel::Model
|
|
9
|
+
attr_accessor :id, :name, :age
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
validates :name, presence: true
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
def to_param
|
|
14
|
+
"user-#{id}"
|
|
13
15
|
end
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
def persisted?
|
|
18
|
+
id.present?
|
|
19
|
+
end
|
|
17
20
|
|
|
18
|
-
|
|
21
|
+
def new_record?
|
|
22
|
+
!persisted?
|
|
19
23
|
end
|
|
24
|
+
end
|
|
20
25
|
|
|
21
|
-
|
|
22
|
-
|
|
26
|
+
class Example
|
|
27
|
+
include MiniForm::Model
|
|
23
28
|
|
|
24
|
-
|
|
29
|
+
attributes :name, :price
|
|
30
|
+
end
|
|
25
31
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
attributes :name, delegate: :user, prefix: 'full'
|
|
32
|
+
class ExampleWithDelegate
|
|
33
|
+
include MiniForm::Model
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
35
|
+
attr_reader :user
|
|
36
|
+
|
|
37
|
+
attributes :name, delegate: :user
|
|
38
|
+
attributes :id, delegate: :user, prefix: true
|
|
39
|
+
attributes :name, delegate: :user, prefix: 'full'
|
|
40
|
+
|
|
41
|
+
def initialize(user)
|
|
42
|
+
@user = user
|
|
33
43
|
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
class ExampleWithModel
|
|
47
|
+
include MiniForm::Model
|
|
48
|
+
|
|
49
|
+
model :user, attributes: %i(name), read: %i(id)
|
|
50
|
+
|
|
51
|
+
def initialize(user)
|
|
52
|
+
self.user = user
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
class ExampleForUpdate
|
|
57
|
+
include MiniForm::Model
|
|
58
|
+
|
|
59
|
+
attributes :name
|
|
60
|
+
|
|
61
|
+
validates :name, presence: true
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
class ExampleForSave
|
|
65
|
+
include MiniForm::Model
|
|
66
|
+
|
|
67
|
+
model :user, attributes: %i(name), save: true
|
|
68
|
+
|
|
69
|
+
def initialize(user:)
|
|
70
|
+
self.user = user
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
class ExampleFormModel
|
|
75
|
+
include MiniForm::Model
|
|
76
|
+
|
|
77
|
+
main_model :user
|
|
78
|
+
|
|
79
|
+
def initialize(user:)
|
|
80
|
+
self.user = user
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
module MiniForm
|
|
86
|
+
describe Model do
|
|
87
|
+
let(:user) { SpecSupport::User.new id: 1, name: 'name', age: 28 }
|
|
34
88
|
|
|
35
89
|
describe 'acts as ActiveModel' do
|
|
36
90
|
include ActiveModel::Lint::Tests
|
|
37
91
|
|
|
38
92
|
before do
|
|
39
|
-
@model = Example.new
|
|
93
|
+
@model = SpecSupport::Example.new
|
|
40
94
|
end
|
|
41
95
|
|
|
42
96
|
def assert(condition, message = nil)
|
|
@@ -51,6 +105,10 @@ module MiniForm
|
|
|
51
105
|
expect(value).to eq(expected_value), message
|
|
52
106
|
end
|
|
53
107
|
|
|
108
|
+
def assert_respond_to(klass, method, message = nil)
|
|
109
|
+
expect(klass).to respond_to(method), message
|
|
110
|
+
end
|
|
111
|
+
|
|
54
112
|
ActiveModel::Lint::Tests.public_instance_methods.map(&:to_s).grep(/^test/).each do |method|
|
|
55
113
|
example(method.gsub('_', ' ')) { send method }
|
|
56
114
|
end
|
|
@@ -75,24 +133,24 @@ module MiniForm
|
|
|
75
133
|
|
|
76
134
|
describe '.attributes' do
|
|
77
135
|
it 'generates getters' do
|
|
78
|
-
object = Example.new name: 'value'
|
|
136
|
+
object = SpecSupport::Example.new name: 'value'
|
|
79
137
|
expect(object.name).to eq 'value'
|
|
80
138
|
end
|
|
81
139
|
|
|
82
140
|
it 'generates setters' do
|
|
83
|
-
object = Example.new
|
|
141
|
+
object = SpecSupport::Example.new
|
|
84
142
|
object.name = 'value'
|
|
85
143
|
|
|
86
144
|
expect(object.name).to eq 'value'
|
|
87
145
|
end
|
|
88
146
|
|
|
89
147
|
it 'can delegate getter' do
|
|
90
|
-
object = ExampleWithDelegate.new user
|
|
148
|
+
object = SpecSupport::ExampleWithDelegate.new user
|
|
91
149
|
expect(object.name).to eq user.name
|
|
92
150
|
end
|
|
93
151
|
|
|
94
152
|
it 'can delegate setter' do
|
|
95
|
-
object = ExampleWithDelegate.new user
|
|
153
|
+
object = SpecSupport::ExampleWithDelegate.new user
|
|
96
154
|
|
|
97
155
|
object.name = 'New Name'
|
|
98
156
|
|
|
@@ -102,71 +160,75 @@ module MiniForm
|
|
|
102
160
|
end
|
|
103
161
|
|
|
104
162
|
describe '.model' do
|
|
105
|
-
ExampleWithModel = Class.new do
|
|
106
|
-
include Model
|
|
107
|
-
|
|
108
|
-
model :user, attributes: %i(name), read: %i(id)
|
|
109
|
-
|
|
110
|
-
def initialize(user)
|
|
111
|
-
self.user = user
|
|
112
|
-
end
|
|
113
|
-
end
|
|
114
|
-
|
|
115
163
|
it 'generates model accessors' do
|
|
116
|
-
object = ExampleWithModel.new user
|
|
164
|
+
object = SpecSupport::ExampleWithModel.new user
|
|
117
165
|
expect(object.user).to eq user
|
|
118
166
|
end
|
|
119
167
|
|
|
120
168
|
it 'can delegate only a reader' do
|
|
121
|
-
object = ExampleWithModel.new user
|
|
169
|
+
object = SpecSupport::ExampleWithModel.new user
|
|
122
170
|
|
|
123
171
|
expect(object).not_to respond_to :id=
|
|
124
172
|
expect(object.id).to eq user.id
|
|
125
173
|
end
|
|
126
174
|
|
|
127
175
|
it 'can delegate model attributes' do
|
|
128
|
-
object = ExampleWithModel.new user
|
|
176
|
+
object = SpecSupport::ExampleWithModel.new user
|
|
129
177
|
expect(object.name).to eq user.name
|
|
130
178
|
end
|
|
131
179
|
|
|
132
180
|
it 'performs nested validation for model' do
|
|
133
|
-
user = User.new
|
|
134
|
-
object = ExampleWithModel.new user
|
|
181
|
+
user = SpecSupport::User.new
|
|
182
|
+
object = SpecSupport::ExampleWithModel.new user
|
|
135
183
|
|
|
136
184
|
expect(object).not_to be_valid
|
|
137
185
|
expect(object.errors[:name]).to be_present
|
|
138
186
|
end
|
|
139
187
|
end
|
|
140
188
|
|
|
189
|
+
describe '.main_model' do
|
|
190
|
+
it 'delegates Rails form attributes to the model' do
|
|
191
|
+
user = SpecSupport::User.new
|
|
192
|
+
object = SpecSupport::ExampleFormModel.new(user: user)
|
|
193
|
+
|
|
194
|
+
expect(object).to have_attributes(
|
|
195
|
+
id: user.id,
|
|
196
|
+
to_param: user.to_param,
|
|
197
|
+
persisted?: user.persisted?,
|
|
198
|
+
new_record?: user.new_record?
|
|
199
|
+
)
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
141
203
|
describe '.attributes_names' do
|
|
142
204
|
it 'returns attribute names' do
|
|
143
|
-
expect(Example.attribute_names).to eq %i(name price)
|
|
205
|
+
expect(SpecSupport::Example.attribute_names).to eq %i(name price)
|
|
144
206
|
end
|
|
145
207
|
|
|
146
208
|
it 'can handle prefixes' do
|
|
147
|
-
expect(ExampleWithDelegate.attribute_names).to include :user_id
|
|
148
|
-
expect(ExampleWithDelegate.attribute_names).to include :full_name
|
|
209
|
+
expect(SpecSupport::ExampleWithDelegate.attribute_names).to include :user_id
|
|
210
|
+
expect(SpecSupport::ExampleWithDelegate.attribute_names).to include :full_name
|
|
149
211
|
end
|
|
150
212
|
end
|
|
151
213
|
|
|
152
214
|
describe '#initialize' do
|
|
153
215
|
it 'can be called with no arguments' do
|
|
154
|
-
expect { Example.new }.not_to raise_error
|
|
216
|
+
expect { SpecSupport::Example.new }.not_to raise_error
|
|
155
217
|
end
|
|
156
218
|
|
|
157
219
|
it 'assign the passed attributes' do
|
|
158
|
-
object = Example.new price: '$5'
|
|
220
|
+
object = SpecSupport::Example.new price: '$5'
|
|
159
221
|
|
|
160
222
|
expect(object.price).to eq '$5'
|
|
161
223
|
end
|
|
162
224
|
|
|
163
225
|
it 'ignores invalid attributes' do
|
|
164
|
-
expect { Example.new invalid: 'attribute' }.not_to raise_error
|
|
226
|
+
expect { SpecSupport::Example.new invalid: 'attribute' }.not_to raise_error
|
|
165
227
|
end
|
|
166
228
|
|
|
167
229
|
it 'handles HashWithIndifferentAccess hashes' do
|
|
168
230
|
hash = ActiveSupport::HashWithIndifferentAccess.new 'price' => '$5'
|
|
169
|
-
object = Example.new hash
|
|
231
|
+
object = SpecSupport::Example.new hash
|
|
170
232
|
|
|
171
233
|
expect(object.price).to eq '$5'
|
|
172
234
|
end
|
|
@@ -174,7 +236,7 @@ module MiniForm
|
|
|
174
236
|
|
|
175
237
|
describe '#attributes' do
|
|
176
238
|
it 'returns attributes' do
|
|
177
|
-
object = Example.new name: 'iPhone', price: '$5'
|
|
239
|
+
object = SpecSupport::Example.new name: 'iPhone', price: '$5'
|
|
178
240
|
expect(object.attributes).to eq name: 'iPhone', price: '$5'
|
|
179
241
|
end
|
|
180
242
|
end
|
|
@@ -182,14 +244,14 @@ module MiniForm
|
|
|
182
244
|
['attributes=', 'assign_attributes'].each do |method_name|
|
|
183
245
|
describe "##{method_name}" do
|
|
184
246
|
it 'sets attributes' do
|
|
185
|
-
object = Example.new
|
|
247
|
+
object = SpecSupport::Example.new
|
|
186
248
|
object.public_send method_name, name: 'iPhone', price: '$5'
|
|
187
249
|
|
|
188
250
|
expect(object.attributes).to eq name: 'iPhone', price: '$5'
|
|
189
251
|
end
|
|
190
252
|
|
|
191
253
|
it 'ignores not listed attributes' do
|
|
192
|
-
object = Example.new
|
|
254
|
+
object = SpecSupport::Example.new
|
|
193
255
|
object.public_send method_name, invalid: 'value'
|
|
194
256
|
|
|
195
257
|
expect(object.attributes).to eq name: nil, price: nil
|
|
@@ -198,38 +260,20 @@ module MiniForm
|
|
|
198
260
|
end
|
|
199
261
|
|
|
200
262
|
describe '#update' do
|
|
201
|
-
ExampleForUpdate = Class.new do
|
|
202
|
-
include Model
|
|
203
|
-
|
|
204
|
-
attributes :name
|
|
205
|
-
|
|
206
|
-
validates :name, presence: true
|
|
207
|
-
end
|
|
208
|
-
|
|
209
|
-
ExampleForSave = Class.new do
|
|
210
|
-
include Model
|
|
211
|
-
|
|
212
|
-
model :user, attributes: %i(name), save: true
|
|
213
|
-
|
|
214
|
-
def initialize(user:)
|
|
215
|
-
self.user = user
|
|
216
|
-
end
|
|
217
|
-
end
|
|
218
|
-
|
|
219
263
|
it 'updates attributes' do
|
|
220
|
-
object = ExampleForUpdate.new name: 'value'
|
|
264
|
+
object = SpecSupport::ExampleForUpdate.new name: 'value'
|
|
221
265
|
|
|
222
266
|
expect { object.update(name: 'new value') }.to change { object.name }.to 'new value'
|
|
223
267
|
end
|
|
224
268
|
|
|
225
269
|
it 'returns true when validations pass' do
|
|
226
|
-
object = ExampleForUpdate.new name: 'value'
|
|
270
|
+
object = SpecSupport::ExampleForUpdate.new name: 'value'
|
|
227
271
|
|
|
228
272
|
expect(object.update).to eq true
|
|
229
273
|
end
|
|
230
274
|
|
|
231
275
|
it 'calls "perfom" method when validation pass' do
|
|
232
|
-
object = ExampleForUpdate.new name: 'value'
|
|
276
|
+
object = SpecSupport::ExampleForUpdate.new name: 'value'
|
|
233
277
|
|
|
234
278
|
allow(object).to receive(:perform)
|
|
235
279
|
|
|
@@ -239,7 +283,7 @@ module MiniForm
|
|
|
239
283
|
end
|
|
240
284
|
|
|
241
285
|
it 'calls "save" for the model' do
|
|
242
|
-
object = ExampleForSave.new user: user
|
|
286
|
+
object = SpecSupport::ExampleForSave.new user: user
|
|
243
287
|
|
|
244
288
|
allow(user).to receive(:save!)
|
|
245
289
|
|
|
@@ -249,7 +293,7 @@ module MiniForm
|
|
|
249
293
|
end
|
|
250
294
|
|
|
251
295
|
it 'supports update callbacks' do
|
|
252
|
-
object = ExampleForUpdate.new name: 'value'
|
|
296
|
+
object = SpecSupport::ExampleForUpdate.new name: 'value'
|
|
253
297
|
|
|
254
298
|
allow(object).to receive(:before_update)
|
|
255
299
|
allow(object).to receive(:after_update)
|
|
@@ -261,7 +305,7 @@ module MiniForm
|
|
|
261
305
|
end
|
|
262
306
|
|
|
263
307
|
it 'supports legacy assig callbacks' do
|
|
264
|
-
object = ExampleForUpdate.new
|
|
308
|
+
object = SpecSupport::ExampleForUpdate.new
|
|
265
309
|
|
|
266
310
|
allow(object).to receive(:before_assigment)
|
|
267
311
|
allow(object).to receive(:after_assigment)
|
|
@@ -273,7 +317,7 @@ module MiniForm
|
|
|
273
317
|
end
|
|
274
318
|
|
|
275
319
|
it 'supports assign callbacks' do
|
|
276
|
-
object = ExampleForUpdate.new
|
|
320
|
+
object = SpecSupport::ExampleForUpdate.new
|
|
277
321
|
|
|
278
322
|
allow(object).to receive(:before_assignment)
|
|
279
323
|
allow(object).to receive(:after_assignment)
|
|
@@ -285,13 +329,13 @@ module MiniForm
|
|
|
285
329
|
end
|
|
286
330
|
|
|
287
331
|
it 'returns false when validations fail' do
|
|
288
|
-
object = ExampleForUpdate.new name: nil
|
|
332
|
+
object = SpecSupport::ExampleForUpdate.new name: nil
|
|
289
333
|
|
|
290
334
|
expect(object.update).to eq false
|
|
291
335
|
end
|
|
292
336
|
|
|
293
337
|
it 'does not call "perfom" method when validation fail' do
|
|
294
|
-
object = ExampleForUpdate.new name: nil
|
|
338
|
+
object = SpecSupport::ExampleForUpdate.new name: nil
|
|
295
339
|
|
|
296
340
|
allow(object).to receive(:perform)
|
|
297
341
|
|
|
@@ -303,12 +347,12 @@ module MiniForm
|
|
|
303
347
|
|
|
304
348
|
describe '#update!' do
|
|
305
349
|
it 'returns self' do
|
|
306
|
-
object = Example.new
|
|
350
|
+
object = SpecSupport::Example.new
|
|
307
351
|
expect(object.update!).to eq object
|
|
308
352
|
end
|
|
309
353
|
|
|
310
354
|
it 'calls update with given arguments' do
|
|
311
|
-
object = Example.new
|
|
355
|
+
object = SpecSupport::Example.new
|
|
312
356
|
|
|
313
357
|
allow(object).to receive(:update).and_return true
|
|
314
358
|
|
|
@@ -318,7 +362,7 @@ module MiniForm
|
|
|
318
362
|
end
|
|
319
363
|
|
|
320
364
|
it 'raises error when update fails' do
|
|
321
|
-
object = Example.new
|
|
365
|
+
object = SpecSupport::Example.new
|
|
322
366
|
|
|
323
367
|
allow(object).to receive(:update).and_return false
|
|
324
368
|
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
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.5
|
|
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: 2024-04-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activemodel
|
|
@@ -28,58 +28,58 @@ dependencies:
|
|
|
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
|
|
58
58
|
requirements:
|
|
59
|
-
- -
|
|
59
|
+
- - '='
|
|
60
60
|
- !ruby/object:Gem::Version
|
|
61
|
-
version:
|
|
61
|
+
version: 3.12.0
|
|
62
62
|
type: :development
|
|
63
63
|
prerelease: false
|
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
|
65
65
|
requirements:
|
|
66
|
-
- -
|
|
66
|
+
- - '='
|
|
67
67
|
- !ruby/object:Gem::Version
|
|
68
|
-
version:
|
|
68
|
+
version: 3.12.0
|
|
69
69
|
- !ruby/object:Gem::Dependency
|
|
70
70
|
name: rspec-mocks
|
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
|
72
72
|
requirements:
|
|
73
|
-
- -
|
|
73
|
+
- - '='
|
|
74
74
|
- !ruby/object:Gem::Version
|
|
75
|
-
version:
|
|
75
|
+
version: 3.12.0
|
|
76
76
|
type: :development
|
|
77
77
|
prerelease: false
|
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
|
79
79
|
requirements:
|
|
80
|
-
- -
|
|
80
|
+
- - '='
|
|
81
81
|
- !ruby/object:Gem::Version
|
|
82
|
-
version:
|
|
82
|
+
version: 3.12.0
|
|
83
83
|
- !ruby/object:Gem::Dependency
|
|
84
84
|
name: coveralls
|
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -129,11 +129,11 @@ executables: []
|
|
|
129
129
|
extensions: []
|
|
130
130
|
extra_rdoc_files: []
|
|
131
131
|
files:
|
|
132
|
+
- ".github/workflows/ci.yml"
|
|
132
133
|
- ".gitignore"
|
|
133
134
|
- ".projections.json"
|
|
134
135
|
- ".rspec"
|
|
135
136
|
- ".rubocop.yml"
|
|
136
|
-
- ".travis.yml"
|
|
137
137
|
- CHANGELOG.md
|
|
138
138
|
- Gemfile
|
|
139
139
|
- LICENSE.txt
|
|
@@ -152,7 +152,7 @@ homepage: https://github.com/RStankov/MiniForm
|
|
|
152
152
|
licenses:
|
|
153
153
|
- MIT
|
|
154
154
|
metadata: {}
|
|
155
|
-
post_install_message:
|
|
155
|
+
post_install_message:
|
|
156
156
|
rdoc_options: []
|
|
157
157
|
require_paths:
|
|
158
158
|
- lib
|
|
@@ -167,9 +167,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
167
167
|
- !ruby/object:Gem::Version
|
|
168
168
|
version: '0'
|
|
169
169
|
requirements: []
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
signing_key:
|
|
170
|
+
rubygems_version: 3.3.7
|
|
171
|
+
signing_key:
|
|
173
172
|
specification_version: 4
|
|
174
173
|
summary: Easy to use form objects in Rails projects
|
|
175
174
|
test_files:
|