klods-ruby 1.2.0 → 1.3.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: 842444db671201072610617f2e5923c7e988af12bbd0bb8762dee48875ef2e0e
4
- data.tar.gz: d0433ca5b0ba0d8eda5c22ac9dd98833d40aa178e15d8e922348cdba0dc6aad2
3
+ metadata.gz: fa5215d6f8d8d6fdce3f82098a17b2dd9fc5f4047a62bac0a1dfeeca7a2cd62f
4
+ data.tar.gz: d31d4138d47a2c3677abbfb83393b6fb64f1724ee6c81276d410a41c2ccfe58f
5
5
  SHA512:
6
- metadata.gz: df4da704c8e48bb6213248f878e8aa80dc784afd4d6f2d107de9550a7c927131c3f60744278beef48919c0e8deb1e6ff557720b382d7d81da12acbc8b10057ba
7
- data.tar.gz: 7a7d724a0801c7bf427e4358f2226f6577dab8182ecf5919f05b160219b11d829fc7e29afc0d8b43924a5a47b3d869241b84a37b246dd44aa5abe558cd4993c9
6
+ metadata.gz: '0516894ed0e82205daa3c7df426842938bbc30281a4c1267a998405736a0e1fcd1844e383d653c1c4236e85974d48216b40ba893ea1a6c19ed0ddd733c707aa0'
7
+ data.tar.gz: 7fe64308e30edcda9caf2415b08afd4c1b489e35f8bd12c5162a61996aafce0a496f258207f8d0fb2e5c232b31e52fccbc323e82e97718a11ea9686ae24148ca
@@ -0,0 +1,101 @@
1
+ module Klods
2
+ class FormBuilder < ActionView::Helpers::FormBuilder
3
+ # Renders a fully wired klods input field: wrapper div, label, input, and
4
+ # optional help/error text. For textarea use f.klods_textarea; for select use f.klods_select.
5
+ #
6
+ # = f.klods_field :email, label: "Email", type: :email, autocomplete: "email"
7
+ # = f.klods_field :password, label: "Password", type: :password, required: true
8
+ # = f.klods_field :name, label: "Name", help: "Your display name"
9
+ def klods_field(method, label: nil, type: :text, help: nil, required: false, **options)
10
+ render_klods_wrapper(method, label: label, help: help, required: required) do |aria|
11
+ send(input_helper_for(type), method, class: "klods-input", **aria, **options)
12
+ end
13
+ end
14
+
15
+ # Renders a klods textarea field with wrapper, label, and error/help wiring.
16
+ #
17
+ # = f.klods_textarea :bio, label: "Bio", help: "Tell us about yourself"
18
+ def klods_textarea(method, label: nil, help: nil, required: false, **options)
19
+ render_klods_wrapper(method, label: label, help: help, required: required) do |aria|
20
+ render_textarea_input(method, class: "klods-textarea", **aria, **options)
21
+ end
22
+ end
23
+
24
+ # Renders a klods select field with wrapper, label, and error/help wiring.
25
+ # choices is an array of ["Label", value] pairs (or plain values).
26
+ #
27
+ # = f.klods_select :role, [["Admin", "admin"], ["User", "user"]], label: "Role"
28
+ def klods_select(method, choices = [], label: nil, help: nil, required: false, **options)
29
+ render_klods_wrapper(method, label: label, help: help, required: required) do |aria|
30
+ select_el = render_select_input(method, choices, class: "klods-select", **aria, **options)
31
+ @template.content_tag(:div, select_el, class: "klods-select-wrapper")
32
+ end
33
+ end
34
+
35
+ # Renders a submit button styled as a primary klods button.
36
+ #
37
+ # = f.klods_submit "Save"
38
+ # = f.klods_submit "Sign up", class: "klods-button--wide" # extra class merged in
39
+ def klods_submit(label = nil, **options)
40
+ extra = options.delete(:class)
41
+ options[:class] = ["klods-button", "klods-button--primary", extra].compact.join(" ")
42
+ submit(label, **options)
43
+ end
44
+
45
+ private
46
+
47
+ def render_klods_wrapper(method, label:, help:, required:, &input_block)
48
+ errors = Array(object&.errors&.[](method))
49
+ error_msg = errors.first
50
+ is_invalid = !error_msg.nil?
51
+
52
+ id = field_id(method)
53
+ help_id = "#{id}-help" if help
54
+ error_id = "#{id}-error" if is_invalid
55
+
56
+ aria = {}
57
+ aria["aria-describedby"] = is_invalid ? error_id : help_id if error_id || help_id
58
+ aria["aria-invalid"] = "true" if is_invalid
59
+
60
+ field_cls = ["klods-field", ("klods-field--invalid" if is_invalid)].compact.join(" ")
61
+ label_cls = ["klods-label", ("klods-label--required" if required)].compact.join(" ")
62
+
63
+ @template.content_tag(:div, class: field_cls) do
64
+ label(method, label || method.to_s.humanize, class: label_cls) +
65
+ input_block.call(aria) +
66
+ ((help && !is_invalid) ? @template.content_tag(:p, help, id: help_id, class: "klods-help") : "".html_safe) +
67
+ (error_msg ? @template.content_tag(:p, error_msg, id: error_id, class: "klods-error", role: "alert") : "".html_safe)
68
+ end
69
+ end
70
+
71
+ def input_helper_for(type)
72
+ {
73
+ email: :email_field,
74
+ password: :password_field,
75
+ tel: :phone_field,
76
+ url: :url_field,
77
+ number: :number_field,
78
+ date: :date_field,
79
+ time: :time_field,
80
+ search: :search_field
81
+ }.fetch(type, :text_field)
82
+ end
83
+
84
+ # Rails 8 FormBuilder#text_area calls @template.textarea(...), which is shadowed
85
+ # by the klods textarea builder. Build the textarea directly to avoid the conflict.
86
+ def render_textarea_input(method, **options)
87
+ name = @object_name.present? ? "#{@object_name}[#{method}]" : method.to_s
88
+ value = object&.public_send(method).to_s
89
+ @template.content_tag(:textarea, value, name: name, id: field_id(method), **options)
90
+ end
91
+
92
+ # FormBuilder#select calls @template.select(...), which is shadowed by the klods
93
+ # select builder. Build the select directly using options_for_select + content_tag.
94
+ def render_select_input(method, choices, **options)
95
+ name = @object_name.present? ? "#{@object_name}[#{method}]" : method.to_s
96
+ selected = object&.public_send(method)
97
+ options_html = @template.options_for_select(choices, selected)
98
+ @template.content_tag(:select, options_html, name: name, id: field_id(method), **options)
99
+ end
100
+ end
101
+ end
data/lib/klods/railtie.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require "klods/form_builder"
2
+
1
3
  module Klods
2
4
  class Railtie < Rails::Railtie
3
5
  initializer "klods.include_helpers" do
data/lib/klods/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Klods
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klods-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Drue Wilding
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-06-27 00:00:00.000000000 Z
11
+ date: 2026-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: actionview
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '7.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '7.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '7.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '7.0'
41
69
  description:
42
70
  email:
43
71
  - drue@wilding.dk
@@ -69,6 +97,7 @@ files:
69
97
  - lib/klods/components/toast.rb
70
98
  - lib/klods/components/tooltip.rb
71
99
  - lib/klods/core.rb
100
+ - lib/klods/form_builder.rb
72
101
  - lib/klods/html.rb
73
102
  - lib/klods/icons.rb
74
103
  - lib/klods/layout.rb