formatic 1.1.0 → 1.1.1

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: bacda9dab30b798b5daa10214bf41e1b5bfcf44994a6eb14849baaa7a9e7ff43
4
- data.tar.gz: 1448e8ea03b9e3bd67ef245bbd62d4ed91ef30ccb06e4489b13fc6b569b13f9f
3
+ metadata.gz: 98c67bc57011a7251698dda266f02417c028940b3f26da081033432699890585
4
+ data.tar.gz: 170490ce0bc8ca66b7787fd49e8beebb0ddc4f7bed7a06aa13a924d8203769fd
5
5
  SHA512:
6
- metadata.gz: 0089f1069cc342792ed9143466f3f8149caccac1b05fde544926e74f7814cbc94d4a97542559f77bfca3117385c1b8696c8cbff3174db27cd7032314551fa66c
7
- data.tar.gz: a718cfcc220c4ad91d7405972ebbf0581755153987d9940b7360ef47b79db723edd8c112c30b01c2c9c85895432529cafabf924fb5b2c562b7c3409a6f51f73f
6
+ metadata.gz: 53b72f449418da96e416029b8d16a9f4e51b53d666118940aa80f5cbfba4693d811ce75d1760f95a9bf7b4c6d43422c3881256db9a95b376cc141252c8633bfa
7
+ data.tar.gz: e021cd554c21ee18c6e8adb957ec791944cb3013a7de18683f5bb1600785c1671fe80c30397c4b97a0c3e5f07e6a1a6a6e8c8f571b31b62715a25074eb0c0505
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## [1.1.1] - 2026-11-26
2
+
3
+ - Required `belongs_to` guesses as required again: `Formatic::Select` no longer renders a blank option for mandatory associations
4
+ - The required-guess reads the `belongs_to` reflection (`optional:` / `belongs_to_required_by_default`) instead of relying on the conditional presence validator edge Rails emits
5
+
1
6
  ## [1.1.0] - 2026-11-20
2
7
 
3
8
  - New `Formatic::Password` field: native `<input type="password">` concealment with an eye reveal toggle (CSS icons, no emoji, no i18n strings)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Formatic
4
- VERSION = '1.1.0'
4
+ VERSION = '1.1.1'
5
5
  end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Formatic
4
+ module Wrappers
5
+ class Required
6
+ # Reads the `belongs_to` reflection for the attribute.
7
+ # Edge Rails attaches a conditional presence validator to a required `belongs_to` association.
8
+ # The ActiveRecord validator carries an `:if` option that validator checks reject.
9
+ # The reflection still marks the association as required.
10
+ class BelongsTo
11
+ include Calls
12
+
13
+ option :object
14
+ option :attribute_name
15
+
16
+ def call
17
+ return false unless reflection
18
+
19
+ return explicit unless explicit.nil?
20
+
21
+ !!default_required
22
+ end
23
+
24
+ private
25
+
26
+ def reflection
27
+ @reflection ||= find_reflection
28
+ end
29
+
30
+ def find_reflection
31
+ return unless object.class.respond_to?(:reflect_on_association)
32
+
33
+ association_names.each do |association_name|
34
+ candidate_reflection = object.class.reflect_on_association(association_name)
35
+ next unless candidate_reflection.respond_to?(:macro)
36
+ return candidate_reflection if candidate_reflection.macro == :belongs_to
37
+ end
38
+
39
+ nil
40
+ end
41
+
42
+ def association_names
43
+ candidate_names = [attribute_name.to_sym]
44
+ alternative_name = ::Formatic::Wrappers::AlternativeAttributeName.call(attribute_name)
45
+ candidate_names << alternative_name if alternative_name && alternative_name != attribute_name.to_sym
46
+ candidate_names
47
+ end
48
+
49
+ def explicit
50
+ optional = association_options[:optional]
51
+ return false if optional
52
+ return true if optional == false
53
+
54
+ nil
55
+ end
56
+
57
+ def association_options
58
+ @association_options ||= reflection.respond_to?(:options) ? reflection.options || {} : {}
59
+ end
60
+
61
+ def default_required
62
+ return unless object.class.respond_to?(:belongs_to_required_by_default)
63
+
64
+ !!object.class.belongs_to_required_by_default
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Formatic
4
+ module Wrappers
5
+ class Required
6
+ # Examines the presence validators of a record.
7
+ # Validators with `:if`, `:unless`, or `:on` never mark the attribute as required.
8
+ class Validation
9
+ include Calls
10
+
11
+ option :object
12
+ option :attribute_name
13
+
14
+ def call
15
+ return false if validators.empty?
16
+
17
+ validators.any? { applicable?(it) }
18
+ end
19
+
20
+ private
21
+
22
+ # All applicable validatiors of this attribute.
23
+ def validators
24
+ @validators ||= ::Formatic::Wrappers::Validators.call(object:, attribute_name:)
25
+ end
26
+
27
+ def applicable?(validator)
28
+ validator.options.keys.exclude?(:if) &&
29
+ validator.options.keys.exclude?(:unless) &&
30
+ validator.options.keys.exclude?(:on) &&
31
+ validator.kind == :presence
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -15,23 +15,23 @@ module Formatic
15
15
  def call
16
16
  return true if manual_required == true
17
17
  return false if manual_required == false
18
- return false if validators.empty?
18
+ return true if belongs_to_required?
19
19
 
20
- validators.any? { applicable?(it) }
20
+ validation_required?
21
21
  end
22
22
 
23
23
  private
24
24
 
25
- # All applicable validatiors of this attribute.
26
- def validators
27
- @validators ||= ::Formatic::Wrappers::Validators.call(object:, attribute_name:)
25
+ # Edge Rails attaches a conditional presence validator to a required `belongs_to` association.
26
+ # The ActiveRecord validator carries an `:if` option.
27
+ # `Validation` rejects validators with `:if`, `:unless`, or `:on`.
28
+ # `BelongsTo` reads the `belongs_to` reflection directly.
29
+ def belongs_to_required?
30
+ ::Formatic::Wrappers::Required::BelongsTo.call(object:, attribute_name:)
28
31
  end
29
32
 
30
- def applicable?(validator)
31
- validator.options.keys.exclude?(:if) &&
32
- validator.options.keys.exclude?(:unless) &&
33
- validator.options.keys.exclude?(:on) &&
34
- validator.kind == :presence
33
+ def validation_required?
34
+ ::Formatic::Wrappers::Required::Validation.call(object:, attribute_name:)
35
35
  end
36
36
  end
37
37
  end
data/lib/formatic.rb CHANGED
@@ -19,6 +19,8 @@ require 'formatic/choices'
19
19
  require 'formatic/wrappers/alternative_attribute_name'
20
20
  require 'formatic/wrappers/error_messages'
21
21
  require 'formatic/wrappers/required'
22
+ require 'formatic/wrappers/required/belongs_to'
23
+ require 'formatic/wrappers/required/validation'
22
24
  require 'formatic/wrappers/translate'
23
25
  require 'formatic/wrappers/validators'
24
26
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formatic
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - halo
@@ -247,6 +247,8 @@ files:
247
247
  - lib/formatic/wrappers/alternative_attribute_name.rb
248
248
  - lib/formatic/wrappers/error_messages.rb
249
249
  - lib/formatic/wrappers/required.rb
250
+ - lib/formatic/wrappers/required/belongs_to.rb
251
+ - lib/formatic/wrappers/required/validation.rb
250
252
  - lib/formatic/wrappers/translate.rb
251
253
  - lib/formatic/wrappers/validators.rb
252
254
  - vscode/formatic.code-snippets