yaaf 0.2.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3a1722cf68a3ac8c7aa91f75205ff4885bae7a94fbf8764c94c2b0466959174a
4
- data.tar.gz: d35126b6b21dbd8e57fa67090ba8802323f4adbd61514cb50b0002adb48e2987
3
+ metadata.gz: 1f6365d22e17a82c62a4b7ee12928c35056003f5e3ab0ac6e03c83c47e01ab18
4
+ data.tar.gz: adf77ecf7ccf550b4c11420fd062eb0e2c544a375658cd55d4afbf71c4b328a4
5
5
  SHA512:
6
- metadata.gz: 64ef092463adeacde6d6a4dd42c660ae005255de36ec6df071575d08086029dd6bd62bd212eba4add212eb437c402edb8f083758e3b1a7d4a61e628ec978d94b
7
- data.tar.gz: 26cda08a6ef4ad7dc741ade6ddce45e77720ecf979a9e788492c471ba486ed09b8a4791e81acaf3ac3ed8da875b5483928145ceec5e69c04060cdcb47f94ff41
6
+ metadata.gz: 9a8d5eaf80ad70bc1dcd3404caa4d23ac96a034e5e851867967a01a79a641b84da050706a365b59bfaa72e2b3340559c13b12e1bc3d992633edbcbb7d4717b44
7
+ data.tar.gz: d410ed1067bdeb314f6096017d08edc690405714055005109680d86321b3c67de16d9e3a436e16fee64c79dd3343e523d2a3009b56655936f57d8cf6a45715e7
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
 
@@ -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,42 +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
17
-
18
- run_callbacks :commit do
19
- save_in_transaction(options)
20
- end
16
+ save_form(options)
17
+ rescue ActiveRecord::RecordInvalid,
18
+ ActiveRecord::RecordNotSaved,
19
+ ActiveModel::ValidationError
21
20
 
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
- if rails_version_less_than_6_1?
35
- model.errors.each do |attribute, message|
36
- errors.add(attribute, message)
37
- end
38
- else
39
- model.errors.each do |model_error|
40
- errors.add(model_error.attribute, model_error.message)
41
- end
32
+ def save_form(options)
33
+ validate! unless options[:validate] == false
34
+
35
+ run_callbacks :commit do
36
+ save_in_transaction(options)
42
37
  end
38
+
39
+ true
43
40
  end
44
41
 
45
42
  def save_in_transaction(options)
46
- ::ActiveRecord::Base.transaction do
43
+ transaction do
47
44
  run_callbacks :save do
48
45
  save_models(options)
49
46
  end
@@ -53,23 +50,22 @@ module YAAF
53
50
  end
54
51
 
55
52
  def save_models(options)
56
- models.map { |model| model.save!(options) }
53
+ options.merge!(validate: false)
54
+
55
+ models.map { |model| model.save!(**options) }
57
56
  end
58
57
 
59
58
  def validate_models
60
- models.each do |model|
61
- promote_errors(model) if model.invalid?
62
- end
59
+ models.each { |model| promote_errors(model) if model.invalid? }
60
+ end
61
+
62
+ def promote_errors(model)
63
+ errors.merge!(model.errors)
63
64
  end
64
65
 
65
66
  def handle_transaction_rollback(exception)
66
67
  run_callbacks :rollback
67
68
  raise exception
68
69
  end
69
-
70
- def rails_version_less_than_6_1?
71
- ActiveModel::VERSION::MAJOR < 6 ||
72
- ActiveModel::VERSION::MAJOR == 6 && ActiveModel::VERSION::MINOR.zero?
73
- end
74
70
  end
75
71
  end
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.2.0'
4
+ VERSION = '2.2.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaaf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Manuel Ramallo
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-01-28 00:00:00.000000000 Z
12
+ date: 2021-12-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -17,40 +17,28 @@ dependencies:
17
17
  requirements:
18
18
  - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: '4'
21
- - - "<"
22
- - !ruby/object:Gem::Version
23
- version: '7'
20
+ version: '5.2'
24
21
  type: :runtime
25
22
  prerelease: false
26
23
  version_requirements: !ruby/object:Gem::Requirement
27
24
  requirements:
28
25
  - - ">="
29
26
  - !ruby/object:Gem::Version
30
- version: '4'
31
- - - "<"
32
- - !ruby/object:Gem::Version
33
- version: '7'
27
+ version: '5.2'
34
28
  - !ruby/object:Gem::Dependency
35
29
  name: activerecord
36
30
  requirement: !ruby/object:Gem::Requirement
37
31
  requirements:
38
32
  - - ">="
39
33
  - !ruby/object:Gem::Version
40
- version: '4'
41
- - - "<"
42
- - !ruby/object:Gem::Version
43
- version: '7'
34
+ version: '5.2'
44
35
  type: :runtime
45
36
  prerelease: false
46
37
  version_requirements: !ruby/object:Gem::Requirement
47
38
  requirements:
48
39
  - - ">="
49
40
  - !ruby/object:Gem::Version
50
- version: '4'
51
- - - "<"
52
- - !ruby/object:Gem::Version
53
- version: '7'
41
+ version: '5.2'
54
42
  - !ruby/object:Gem::Dependency
55
43
  name: database_cleaner-active_record
56
44
  requirement: !ruby/object:Gem::Requirement
@@ -167,6 +155,8 @@ licenses:
167
155
  metadata:
168
156
  homepage_uri: https://github.com/rootstrap/yaaf
169
157
  source_code_uri: https://github.com/rootstrap/yaaf
158
+ bug_tracker_uri: https://github.com/rootstrap/yaaf/issues
159
+ changelog_uri: https://github.com/rootstrap/yaaf/releases
170
160
  post_install_message:
171
161
  rdoc_options: []
172
162
  require_paths:
@@ -182,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
182
172
  - !ruby/object:Gem::Version
183
173
  version: '0'
184
174
  requirements: []
185
- rubygems_version: 3.2.0.rc.1
175
+ rubygems_version: 3.1.6
186
176
  signing_key:
187
177
  specification_version: 4
188
178
  summary: Easing the form object pattern in Rails applications.