tramway 2.2.5.4 → 2.2.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7951f9f78870d2dc7a6d12d1125fffe641251d68de9039231c44be35d3be322e
4
- data.tar.gz: 2665a1a1d5b8187d9b7a7ec2d19ff75a56e41028195c3eac13c603e85affee5e
3
+ metadata.gz: da9fb27f70518c08c00dc9b65368f3425460df6ed0b9cb7506ea1fd4140cbd64
4
+ data.tar.gz: 32ec241b81c1e6120b410e62a8a22932720721be1ed34b73c64bf43fa63e5a5d
5
5
  SHA512:
6
- metadata.gz: 0d0ca70e859144c58767deca5c540faa6e472a7ada951089ef3aa735f3eeb969e99daa47b0aad7bd679e237255f7ef384e8eecbd01cc2fcf970a11330de4e808
7
- data.tar.gz: cd3ad4af73eb6a2b3720da8cd0ecee12155a98f53e2017d7c3c4327bea120f1fa03d8e44344844f4315420b8fd61462cd369b8162d4ccbf76d162c0c82741782
6
+ metadata.gz: 2279a03d5e72a3e5a943b22ce54b11dfab5cf6970c59e59541fe0f6be3209cef4c6b2547917ff5bcbfea1e7488a482f645b9e0b720e7f6b0daef1962d3f62b85
7
+ data.tar.gz: a310b92c15be5e0bcedeab69e2bf7e512f183581acae65e9b50f4c7464129346b86e83f56b34954d86b3d7d9f50bfd7230745747974b9f1e95ffc6884db2d5fd
data/README.md CHANGED
@@ -654,9 +654,28 @@ end
654
654
  * `with:` - a proc with a normalization
655
655
  * `apply_on_nil` - by default is `false`. When `true` Tramway Form applies normalization on `nil` values
656
656
 
657
+ ### Validations
658
+
659
+ Tramway Form supports `validates` method to add form-level validations before saving.
660
+
661
+ ```ruby
662
+ class UserForm < Tramway::BaseForm
663
+ properties :email, :role
664
+
665
+ validates :email, format: { with: /@/ }, allow_nil: true
666
+ validates :role, presence: true
667
+ end
668
+ ```
669
+
670
+ `validates` uses the standard ActiveModel/ActiveRecord validation options (for example `presence: true`,
671
+ `format:`, `length:`, `allow_nil`, `allow_blank`, etc.), so `with:` is optional unless a validator
672
+ requires it (like `format`).
673
+
674
+ When validations fail, `submit` returns `false` and adds errors to the form object, and `submit!` raises `ActiveRecord::RecordInvalid`.
675
+
657
676
  ### Form inheritance
658
677
 
659
- Tramway Form supports inheritance of `properties`, `normalizations`, and `fields`.
678
+ Tramway Form supports inheritance of `properties`, `normalizations`, `validations`, and `fields`.
660
679
 
661
680
  **Example**
662
681
 
@@ -665,6 +684,7 @@ class UserForm < TramwayForm
665
684
  properties :email, :password
666
685
 
667
686
  normalizes :email, with: ->(value) { value.strip.downcase }
687
+ validates :email, format: { with: /@/ }, allow_nil: true
668
688
 
669
689
  fields email: :email,
670
690
  password: :password
@@ -676,6 +696,7 @@ end
676
696
 
677
697
  AdminForm.properties # returns [:email, :password, :permissions]
678
698
  AdminForm.normalizations # contains the normalization of :email
699
+ AdminForm.validators_on(:email) # includes the validation of :email
679
700
  AdminForm.fields # { email: :email, password: :password }
680
701
  ```
681
702
 
data/docs/AGENTS.md CHANGED
@@ -106,6 +106,9 @@ end
106
106
  ### Rule 2
107
107
  Normalize input with `normalizes` (from Tramway) for attributes like email, phone, etc. Don't use `normalizes` in model unless it requested explicitly.
108
108
 
109
+ ### Rule 2.1
110
+ When you need form-level validation, use Tramway Form `validates` on the form object (ActiveModel/ActiveRecord validation options like `presence: true` work, and `with:` is optional unless a validator requires it). Keep data integrity validations in the model unless the request explicitly needs form-only logic.
111
+
109
112
  ### Rule 3
110
113
  Use Tramway Navbar for navigation
111
114
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'tramway/forms/properties'
4
4
  require 'tramway/forms/normalizations'
5
+ require 'tramway/forms/validations'
5
6
  require 'tramway/forms/fields'
6
7
  require 'tramway/duck_typing'
7
8
 
@@ -12,6 +13,7 @@ module Tramway
12
13
  include Tramway::Forms::Properties
13
14
  include Tramway::Forms::Fields
14
15
  include Tramway::Forms::Normalizations
16
+ include Tramway::Forms::Validations
15
17
  include Tramway::DuckTyping::ActiveRecordCompatibility
16
18
 
17
19
  attr_reader :object, :initial_object
@@ -27,6 +29,7 @@ module Tramway
27
29
  def inherited(subclass)
28
30
  __initialize_properties subclass
29
31
  __initialize_normalizations subclass
32
+ __initialize_validations subclass
30
33
  __initialize_fields subclass
31
34
 
32
35
  super
@@ -36,6 +39,8 @@ module Tramway
36
39
  def submit(params)
37
40
  __submit params
38
41
 
42
+ return false if object.errors.any?
43
+
39
44
  object.save.tap do
40
45
  __object
41
46
  end
@@ -44,6 +49,8 @@ module Tramway
44
49
  def submit!(params)
45
50
  __submit params
46
51
 
52
+ raise ActiveRecord::RecordInvalid, object if object.errors.any?
53
+
47
54
  object.save!.tap do
48
55
  __object
49
56
  end
@@ -71,7 +78,10 @@ module Tramway
71
78
  private
72
79
 
73
80
  def __submit(params)
74
- __apply_properties __apply_normalizations params
81
+ normalized_params = __apply_normalizations(params)
82
+
83
+ __apply_properties normalized_params
84
+ __apply_validations normalized_params
75
85
  end
76
86
 
77
87
  def __object
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model'
4
+
5
+ module Tramway
6
+ module Forms
7
+ # Provides form validations for Tramway forms
8
+ module Validations
9
+ def self.included(base)
10
+ base.include ActiveModel::Validations
11
+ base.extend ClassMethods
12
+ end
13
+
14
+ # A collection of methods that would be using in users forms
15
+ module ClassMethods
16
+ def __initialize_validations(_subclass); end
17
+ end
18
+
19
+ # rubocop:disable Naming/PredicateMethod
20
+ def __apply_validations(_params)
21
+ valid?
22
+ end
23
+ # rubocop:enable Naming/PredicateMethod
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tramway
4
- VERSION = '2.2.5.4'
4
+ VERSION = '2.2.6'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tramway
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.5.4
4
+ version: 2.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - kalashnikovisme
@@ -263,6 +263,7 @@ files:
263
263
  - lib/tramway/forms/fields.rb
264
264
  - lib/tramway/forms/normalizations.rb
265
265
  - lib/tramway/forms/properties.rb
266
+ - lib/tramway/forms/validations.rb
266
267
  - lib/tramway/helpers/component_helper.rb
267
268
  - lib/tramway/helpers/decorate_helper.rb
268
269
  - lib/tramway/helpers/form_helper.rb