formatic 0.2.12 → 0.4.0

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: a7e3e2484fd61fe3a436e68d059689803fc339553b243c95ccfcd6f0dd4f7a40
4
- data.tar.gz: cd26ab984cb09ef2552a2cf2efb86edfa89d6839edc12dee8fa2c4479c7b746f
3
+ metadata.gz: 2e8931dc635b9f82d078e614fe974173add25c068d5269c28531fe947c3be596
4
+ data.tar.gz: e73abb6b3045ca45d0546a17967e60f6aec03489a48381240447a3e0a3d63bc4
5
5
  SHA512:
6
- metadata.gz: 9dd0bc7504620621b5bc3104fc335aba51268d495f8e143b53d9e348ed26736bd47ea02c4a683ce379583be5f9fc89e1c8715b0ecd06244ca84983357e9a3a5f
7
- data.tar.gz: d1b8430e512ca12718da806cb9d5ab65750944d45aa03bcd5b4f9bebb8b15171c491c9f15529d6a71a54216bb7a2e3f6d2cb43cec96c7d38f11d9ea4c0e71659
6
+ metadata.gz: e4e0ec1a1dc71da29321efee225c7a3ca3aa4dc4fd15bd885ff8d80a0e903cd62879ddd5078d742e9ca9a66a2efb1517db0b3855dc8ef2ac843fa51d145abe81
7
+ data.tar.gz: 6bd89feb80068a835b453ef11af4df9635f3094347f090c45f3e21b3029f7f3b44dbe52b1ceebbf403a664d5b8f942d86f1f09493eabaf129f1d1b55f053a048
data/CHANGELOG.md CHANGED
@@ -1,4 +1,10 @@
1
- ## [Unreleased]
1
+ ## [0.4.0] 2026-08-05
2
+
3
+ - `Formatic::File` now raises at render time when `attribute_name` does not exist on the model, so typos like `header_logo_for_light` (instead of the model's `header_logo_for_dark`) surface immediately instead of silently dropping the upload. The check is skipped when a manual `value:` is passed, the form builder has no object, or the object is not an ActiveModel/ActiveRecord record.
4
+
5
+ ## [0.3.0] 2026-08-04
6
+
7
+ - Make VS Code snippet symlinking atomic so concurrent app boots no longer race into `Errno::EEXIST`
2
8
 
3
9
  ## [0.2.12] 2026-07-24
4
10
 
@@ -27,6 +27,12 @@ module Formatic
27
27
  </div>
28
28
 
29
29
  <%= f.file_field attribute_name, class: "js-formatic-file__input", direct_upload:, multiple:, accept:, data: %>
30
+
31
+ <%- if (file = current_file) -%>
32
+ <div class="c-formatic-file__current">
33
+ <%= link_to 'Download', file, target: :_blank %>
34
+ </div>
35
+ <%- end -%>
30
36
  </div>
31
37
  <% end %>
32
38
  <% end %>
@@ -36,8 +42,27 @@ module Formatic
36
42
  { entries: entries_json }.merge(manual_data)
37
43
  end
38
44
 
45
+ def before_render
46
+ validate_attribute_name!
47
+ end
48
+
39
49
  private
40
50
 
51
+ def validate_attribute_name!
52
+ # A manually passed value means `attribute_name` is just a param name,
53
+ # not necessarily a method on the model.
54
+ return if manual_value != :_fetch_from_record
55
+ return if f.nil? || f.object.nil?
56
+
57
+ # Only enforce this for real ActiveModel/ActiveRecord records.
58
+ # Custom objects (e.g. slugs) are not expected to respond to it.
59
+ return unless f.object.respond_to?(:model_name)
60
+ return if f.object.respond_to?(attribute_name)
61
+
62
+ raise ArgumentError,
63
+ "attribute `#{attribute_name}` does not exist on #{f.object.class}."
64
+ end
65
+
41
66
  def attachments
42
67
  return [] if value.blank?
43
68
 
@@ -66,5 +91,15 @@ module Formatic
66
91
  }
67
92
  end.to_json
68
93
  end
94
+
95
+ # The currently attached file, if there is exactly one.
96
+ # Multiple files are not shown here, see `Formatic::Files`.
97
+ def current_file
98
+ return if multiple
99
+ return unless attachments.size == 1
100
+
101
+ attachment = attachments.first
102
+ attachment if attachment.present?
103
+ end
69
104
  end
70
105
  end
@@ -28,8 +28,7 @@ module Formatic
28
28
  target = vscode_dir.join('formatic.code-snippets')
29
29
  source = Engine.root.join('vscode/formatic.code-snippets')
30
30
 
31
- FileUtils.rm_f(target.to_s)
32
- FileUtils.ln_s(source, target, force: true)
31
+ VscodeSnippets.install(target, source)
33
32
  end
34
33
  end
35
34
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Formatic
4
- VERSION = '0.2.12'
4
+ VERSION = '0.4.0'
5
5
  end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tmpdir'
4
+
5
+ module Formatic
6
+ # Installs the gem's VS Code snippets as a symlink inside the host
7
+ # application's `.vscode` directory.
8
+ #
9
+ # This runs in every process that boots the host application (Spring
10
+ # preloader, Puma workers, rake tasks, ...), so those processes race each
11
+ # other. A non-atomic remove-then-create (`FileUtils.ln_s(force: true)`)
12
+ # lets two processes both pass the "does it exist?" check before either
13
+ # creates the link, so one of them dies with `Errno::EEXIST` on
14
+ # `symlink(2)`.
15
+ #
16
+ # To stay race-free the link is created under a temporary name in the
17
+ # system temp directory and then `rename(2)`-ed over the target, which
18
+ # atomically replaces whatever is already there.
19
+ #
20
+ # NOTE: `Formatic::File` (a ViewComponent) shadows `File` inside this
21
+ # module, so we refer to Ruby's file class as `::File`.
22
+ module VscodeSnippets
23
+ class << self
24
+ def install(target, source)
25
+ target = target.to_s
26
+ source = source.to_s
27
+
28
+ return if already_linked?(target, source)
29
+
30
+ install_atomically(target, source)
31
+ end
32
+
33
+ private
34
+
35
+ def install_atomically(target, source)
36
+ tmp = ::File.join(Dir.tmpdir, "formatic-#{Process.pid}-#{Thread.current.object_id}.tmp")
37
+ FileUtils.rm_f(tmp)
38
+ ::File.symlink(source, tmp)
39
+ ::File.rename(tmp, target)
40
+ rescue Errno::EXDEV
41
+ replace(target, source)
42
+ ensure
43
+ FileUtils.rm_f(tmp) if tmp
44
+ end
45
+
46
+ def already_linked?(target, source)
47
+ ::File.symlink?(target) && ::File.readlink(target) == source
48
+ end
49
+
50
+ def replace(target, source)
51
+ FileUtils.rm_f(target)
52
+ ::File.symlink(source, target)
53
+ end
54
+ end
55
+ end
56
+ end
data/lib/formatic.rb CHANGED
@@ -9,6 +9,7 @@ require 'dry/initializer'
9
9
  require 'formatic/version'
10
10
  require 'formatic/css'
11
11
  require 'formatic/safe_join'
12
+ require 'formatic/vscode_snippets'
12
13
  require 'formatic/templates/select'
13
14
  require 'formatic/templates/wrapper'
14
15
  require 'formatic/choices/countries'
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: 0.2.12
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - halo
@@ -198,6 +198,7 @@ files:
198
198
  - lib/formatic/templates/select.rb
199
199
  - lib/formatic/templates/wrapper.rb
200
200
  - lib/formatic/version.rb
201
+ - lib/formatic/vscode_snippets.rb
201
202
  - lib/formatic/wrappers/alternative_attribute_name.rb
202
203
  - lib/formatic/wrappers/error_messages.rb
203
204
  - lib/formatic/wrappers/required.rb