yaaf 0.1.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -4
  3. data/lib/yaaf/form.rb +24 -17
  4. data/lib/yaaf/version.rb +1 -1
  5. metadata +11 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f78d26d774f33785958812a5d3d449b5aa882c297f07e19436410d01bfb5157a
4
- data.tar.gz: cfaa5d69b5041de56aad4b8b32059dff8512fdf0931165f2260320abc6d7ec53
3
+ metadata.gz: edacd5be520dbceecc7d62c627a384750ad5bb624c5dafb07ea97bf61f97cf82
4
+ data.tar.gz: 6b929d37737f2367d9a2076bf696426b542020fc74de2197003f8285a9409b20
5
5
  SHA512:
6
- metadata.gz: 05a43329b36959802bd0b1f81fc6d5d039a4090cd7fbba5d7fca5cf8b6e2c93779270fbe3ead65246367f22e5d23b917d8776a9e8e8859f40c2ed4d468c9ce35
7
- data.tar.gz: 0cbfcbf3253f87f190364da712a9fda48f4354263a419d6c01ce571897454f27b8b8e0c616f04e7a0534ede4a5525d1d6c0bf590f18b2ec2db06556ad29d3f2f
6
+ metadata.gz: cf2757197732d9955a3ad943e99b22cea8603fe6f7193f57aa15aa3eeeac83acd2fe0be2889e0e80c5dd23d043b32194566141ec261d2ce360e098c671932176
7
+ data.tar.gz: 5b91ac0986535bd34e3a7b7cfe78326683b1ff345ae866c09ba1da1144385a6246737b2ca09612cdeabbed82ccba5448bebfd844f8219e550799971e9219bbfe
data/README.md CHANGED
@@ -39,7 +39,7 @@ Form Objects is a design pattern that allows us to:
39
39
  3. Keep business logic validations out of models
40
40
 
41
41
  There are some other form objects gems but we felt none of them provided us all the features that we expected:
42
- 1. Form objects that behaves like Rails models
42
+ 1. Form objects that behave like Rails models
43
43
  2. Simple to use and to understand the implementation (no magic)
44
44
  3. Easy to customize
45
45
  4. Gem is well tested and maintained
@@ -50,7 +50,7 @@ If you want to learn more about Form Objects you can check out [these great arti
50
50
 
51
51
  ### Why YAAF?
52
52
 
53
- - It is [64 lines long](https://github.com/rootstrap/yaaf/blob/master/lib/yaaf/form.rb#L64). As you can imagine, we did no magic in such a few lines of code, we just leveraged Rails modules in order to provide our form objects with a Rails-like behavior. You can review the code, it's easy to understand.
53
+ - It is [71 lines long](https://github.com/rootstrap/yaaf/blob/master/lib/yaaf/form.rb#L71). As you can imagine, we did no magic in such a few lines of code, we just leveraged Rails modules in order to provide our form objects with a Rails-like behavior. You can review the code, it's easy to understand.
54
54
 
55
55
  - It provides a similar API to `ActiveModel` models so you can treat them interchangeably.
56
56
 
@@ -113,7 +113,7 @@ class RegistrationsController < ApplicationController
113
113
  registration_form = RegistrationForm.new(user_attributes: user_params)
114
114
 
115
115
  if registration_form.save
116
- redirect_to registration.user
116
+ redirect_to registration_form.user
117
117
  else
118
118
  render :new
119
119
  end
@@ -231,7 +231,7 @@ end
231
231
 
232
232
  ### Callbacks
233
233
 
234
- `YAAF` form objects support validations the same way as `ActiveModel` models. For example:
234
+ `YAAF` form objects support callbacks the same way as `ActiveModel` models. For example:
235
235
 
236
236
  ```ruby
237
237
  class RegistrationForm < YAAF::Form
data/lib/yaaf/form.rb CHANGED
@@ -8,36 +8,39 @@ module YAAF
8
8
  include ::ActiveRecord::Transactions
9
9
  define_model_callbacks :save
10
10
 
11
+ delegate :transaction, to: ::ActiveRecord::Base
12
+
11
13
  validate :validate_models
12
14
 
13
15
  def save(options = {})
14
- unless options[:validate] == false
15
- return false if invalid?
16
- end
16
+ save_form(options)
17
+ rescue ActiveRecord::RecordInvalid,
18
+ ActiveRecord::RecordNotSaved,
19
+ ActiveModel::ValidationError
17
20
 
18
- run_callbacks :commit do
19
- save_in_transaction(options)
20
- end
21
-
22
- true
21
+ false
23
22
  end
24
23
 
25
24
  def save!(options = {})
26
- save(options) || raise(ActiveRecord::RecordNotSaved.new('Failed to save the form', self))
25
+ save_form(options)
27
26
  end
28
27
 
29
28
  private
30
29
 
31
30
  attr_accessor :models
32
31
 
33
- def promote_errors(model)
34
- model.errors.each do |attribute, message|
35
- errors.add(attribute, message)
32
+ def save_form(options)
33
+ validate! unless options[:validate] == false
34
+
35
+ run_callbacks :commit do
36
+ save_in_transaction(options)
36
37
  end
38
+
39
+ true
37
40
  end
38
41
 
39
42
  def save_in_transaction(options)
40
- ::ActiveRecord::Base.transaction do
43
+ transaction do
41
44
  run_callbacks :save do
42
45
  save_models(options)
43
46
  end
@@ -47,13 +50,17 @@ module YAAF
47
50
  end
48
51
 
49
52
  def save_models(options)
50
- models.map { |model| model.save!(options) }
53
+ options.merge!(validate: false)
54
+
55
+ models.map { |model| model.save!(**options) }
51
56
  end
52
57
 
53
58
  def validate_models
54
- models.each do |model|
55
- promote_errors(model) if model.invalid?
56
- end
59
+ models.each { |model| promote_errors(model) if model.invalid? }
60
+ end
61
+
62
+ def promote_errors(model)
63
+ errors.merge!(model.errors)
57
64
  end
58
65
 
59
66
  def handle_transaction_rollback(exception)
data/lib/yaaf/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module YAAF
4
- VERSION = '0.1.1'
4
+ VERSION = '2.1.0'
5
5
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaaf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Manuel Ramallo
8
8
  - Santiago Bartesaghi
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-04-11 00:00:00.000000000 Z
12
+ date: 2021-09-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -17,7 +17,7 @@ dependencies:
17
17
  requirements:
18
18
  - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: '4'
20
+ version: '5.2'
21
21
  - - "<"
22
22
  - !ruby/object:Gem::Version
23
23
  version: '7'
@@ -27,7 +27,7 @@ dependencies:
27
27
  requirements:
28
28
  - - ">="
29
29
  - !ruby/object:Gem::Version
30
- version: '4'
30
+ version: '5.2'
31
31
  - - "<"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '7'
@@ -37,7 +37,7 @@ dependencies:
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '4'
40
+ version: '5.2'
41
41
  - - "<"
42
42
  - !ruby/object:Gem::Version
43
43
  version: '7'
@@ -47,7 +47,7 @@ dependencies:
47
47
  requirements:
48
48
  - - ">="
49
49
  - !ruby/object:Gem::Version
50
- version: '4'
50
+ version: '5.2'
51
51
  - - "<"
52
52
  - !ruby/object:Gem::Version
53
53
  version: '7'
@@ -149,7 +149,7 @@ dependencies:
149
149
  - - "~>"
150
150
  - !ruby/object:Gem::Version
151
151
  version: 1.4.2
152
- description:
152
+ description:
153
153
  email:
154
154
  - juan.ramallo@rootstrap.com
155
155
  executables: []
@@ -167,7 +167,7 @@ licenses:
167
167
  metadata:
168
168
  homepage_uri: https://github.com/rootstrap/yaaf
169
169
  source_code_uri: https://github.com/rootstrap/yaaf
170
- post_install_message:
170
+ post_install_message:
171
171
  rdoc_options: []
172
172
  require_paths:
173
173
  - lib
@@ -182,8 +182,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
182
182
  - !ruby/object:Gem::Version
183
183
  version: '0'
184
184
  requirements: []
185
- rubygems_version: 3.1.0.pre2
186
- signing_key:
185
+ rubygems_version: 3.1.6
186
+ signing_key:
187
187
  specification_version: 4
188
188
  summary: Easing the form object pattern in Rails applications.
189
189
  test_files: []