tramway 2.2.5.4 → 2.2.7

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: 908989b8908005e2022039beec8b7b47c4a7200166c77fe33c2dd27891d2fe58
4
+ data.tar.gz: b57f2f3717522d10885d7ba83464590ff21ce87fa24af5c46deb85b0fac806cd
5
5
  SHA512:
6
- metadata.gz: 0d0ca70e859144c58767deca5c540faa6e472a7ada951089ef3aa735f3eeb969e99daa47b0aad7bd679e237255f7ef384e8eecbd01cc2fcf970a11330de4e808
7
- data.tar.gz: cd3ad4af73eb6a2b3720da8cd0ecee12155a98f53e2017d7c3c4327bea120f1fa03d8e44344844f4315420b8fd61462cd369b8162d4ccbf76d162c0c82741782
6
+ metadata.gz: 7824108d8dfe9a0c3da312a26d12a84fe1df9424550c6edb29b864634109cc793e883a9f13ff90a7ab40d2e9b833cc6068194055410837cb554dd0e467f802f7
7
+ data.tar.gz: befb3fe2ad460b34f002ffd58b01ab4c1f9996ff5f402e714bb54b6fa013091004b55e7ed8b558b551dd017bba4609c01072370eae034b77c51f978a8703fa4e
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
 
@@ -18,21 +18,24 @@ class TailwindComponent < Tramway::BaseComponent
18
18
  select_input: 'text-sm px-2 py-1',
19
19
  file_button: 'text-sm px-3 py-1',
20
20
  submit_button: 'text-sm px-3 py-1',
21
- multiselect_input: 'text-sm px-2 py-1 h-10'
21
+ multiselect_input: 'text-sm px-2 py-1 h-10',
22
+ checkbox_input: 'h-4 w-4'
22
23
  },
23
24
  medium: {
24
25
  text_input: 'text-base px-3 py-2',
25
26
  select_input: 'text-base px-3 py-2',
26
27
  file_button: 'text-base px-4 py-2',
27
28
  submit_button: 'text-base px-4 py-2',
28
- multiselect_input: 'text-base px-2 py-1 h-12'
29
+ multiselect_input: 'text-base px-2 py-1 h-12',
30
+ checkbox_input: 'h-5 w-5'
29
31
  },
30
32
  large: {
31
33
  text_input: 'text-xl px-4 py-3',
32
34
  select_input: 'text-xl px-4 py-3',
33
35
  file_button: 'text-xl px-5 py-3',
34
36
  submit_button: 'text-xl px-5 py-3',
35
- multiselect_input: 'text-xl px-3 py-2 h-15'
37
+ multiselect_input: 'text-xl px-3 py-2 h-15',
38
+ checkbox_input: 'h-6 w-6'
36
39
  }
37
40
  }.freeze
38
41
 
@@ -67,6 +70,13 @@ class TailwindComponent < Tramway::BaseComponent
67
70
  )
68
71
  end
69
72
 
73
+ def checkbox_base_classes
74
+ theme_classes(
75
+ classic: 'rounded-full border border-gray-700 bg-gray-900 text-gray-100 shadow-inner focus:outline-none ' \
76
+ 'focus:ring-2 focus:ring-gray-600'
77
+ )
78
+ end
79
+
70
80
  def form_label_classes
71
81
  theme_classes(
72
82
  classic: 'block text-sm font-semibold mb-2 text-gray-200'
@@ -68,6 +68,10 @@ module Tailwinds
68
68
  render(Tailwinds::Form::FileFieldComponent.new(input:, **default_options(attribute, sanitized_options)), &)
69
69
  end
70
70
 
71
+ def check_box(attribute, **, &)
72
+ common_field(:checkbox, :check_box, attribute, **, &)
73
+ end
74
+
71
75
  def select(attribute, collection, **options, &)
72
76
  sanitized_options = sanitize_options(options)
73
77
 
@@ -0,0 +1,7 @@
1
+ .flex.items-start.gap-2.mb-4
2
+ - classes = "#{size_class(:checkbox_input)} #{checkbox_base_classes}"
3
+ = @input.call @attribute, **@options.merge(class: classes)
4
+ - if @label
5
+ %div
6
+ = component('tailwinds/form/label', for: @for) do
7
+ = @label
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tailwinds
4
+ module Form
5
+ # Tailwind-styled checkbox field
6
+ class CheckboxComponent < TailwindComponent
7
+ end
8
+ end
9
+ end
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
 
@@ -422,6 +425,24 @@ and in a controller
422
425
  end
423
426
  ```
424
427
 
428
+ ### Rule 27
429
+ Don't create scopes for enumerated values, use `scope: :shallow` of the enumerize gem.
430
+
431
+ Do this
432
+
433
+ ```
434
+ enumerize :role, in: [:admin, :default], scope: :shallow
435
+ ```
436
+
437
+ Instead of this
438
+
439
+ ```
440
+ scope :for_role, -> (role) { where role: role }
441
+ ```
442
+
443
+ ### Rule 28
444
+ In case, you need to make a one link in tramway_table on each row. Use `tramway_row href: your_link` instead of putting the like inside a cell.
445
+
425
446
  ## Controller Patterns
426
447
 
427
448
  - Keep actions short and explicit with guard clauses.
@@ -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
@@ -23,8 +23,10 @@ module Tramway
23
23
 
24
24
  def field_name(field_data)
25
25
  case field_data.to_sym
26
- when :text_area, :select, :multiselect
26
+ when :text_area, :select, :multiselect, :check_box
27
27
  field_data
28
+ when :checkbox
29
+ :check_box
28
30
  else
29
31
  "#{field_data}_field"
30
32
  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.7'
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.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - kalashnikovisme
@@ -156,6 +156,8 @@ files:
156
156
  - app/components/tailwinds/flash_component.html.haml
157
157
  - app/components/tailwinds/flash_component.rb
158
158
  - app/components/tailwinds/form/builder.rb
159
+ - app/components/tailwinds/form/checkbox_component.html.haml
160
+ - app/components/tailwinds/form/checkbox_component.rb
159
161
  - app/components/tailwinds/form/date_field_component.html.haml
160
162
  - app/components/tailwinds/form/date_field_component.rb
161
163
  - app/components/tailwinds/form/datetime_field_component.html.haml
@@ -263,6 +265,7 @@ files:
263
265
  - lib/tramway/forms/fields.rb
264
266
  - lib/tramway/forms/normalizations.rb
265
267
  - lib/tramway/forms/properties.rb
268
+ - lib/tramway/forms/validations.rb
266
269
  - lib/tramway/helpers/component_helper.rb
267
270
  - lib/tramway/helpers/decorate_helper.rb
268
271
  - lib/tramway/helpers/form_helper.rb