klods-ruby 1.3.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: e69d689812ff62a2d4e4e81169ca4d572eee1c7036af0b17e5b6ad66b6c73c2d
4
- data.tar.gz: 0cc52609f3b1fe61aeb480e1a289269dbec20e1a50690bb95b8c645ead2b7506
3
+ metadata.gz: fa5215d6f8d8d6fdce3f82098a17b2dd9fc5f4047a62bac0a1dfeeca7a2cd62f
4
+ data.tar.gz: d31d4138d47a2c3677abbfb83393b6fb64f1724ee6c81276d410a41c2ccfe58f
5
5
  SHA512:
6
- metadata.gz: 1e64fd8cf5a7927ae1edd8486e4fe21ed63fe56db8b53f6021984914c7f1add7dfa44aceb61c258d1b23f4faa64430578a7ebce392a2822170b722344f18a7a0
7
- data.tar.gz: 6e2dcccafe102221366b77f3ea062957fde302f4729b9c1475ee9c361b4f96210c40e2b3f6f62479c7e4084132aba6beb2020840ce521b9b30a38473c182fb44
6
+ metadata.gz: '0516894ed0e82205daa3c7df426842938bbc30281a4c1267a998405736a0e1fcd1844e383d653c1c4236e85974d48216b40ba893ea1a6c19ed0ddd733c707aa0'
7
+ data.tar.gz: 7fe64308e30edcda9caf2415b08afd4c1b489e35f8bd12c5162a61996aafce0a496f258207f8d0fb2e5c232b31e52fccbc323e82e97718a11ea9686ae24148ca
@@ -1,13 +1,50 @@
1
1
  module Klods
2
2
  class FormBuilder < ActionView::Helpers::FormBuilder
3
- # Renders a fully wired klods form field: wrapper div, label, input, and
4
- # optional help/error text. Error state is read from object.errors automatically.
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
5
  #
6
6
  # = f.klods_field :email, label: "Email", type: :email, autocomplete: "email"
7
7
  # = f.klods_field :password, label: "Password", type: :password, required: true
8
- # = f.klods_field :bio, label: "Bio", type: :textarea
9
8
  # = f.klods_field :name, label: "Name", help: "Your display name"
10
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)
11
48
  errors = Array(object&.errors&.[](method))
12
49
  error_msg = errors.first
13
50
  is_invalid = !error_msg.nil?
@@ -25,24 +62,12 @@ module Klods
25
62
 
26
63
  @template.content_tag(:div, class: field_cls) do
27
64
  label(method, label || method.to_s.humanize, class: label_cls) +
28
- send(input_helper_for(type), method, class: "klods-input", **aria, **options) +
65
+ input_block.call(aria) +
29
66
  ((help && !is_invalid) ? @template.content_tag(:p, help, id: help_id, class: "klods-help") : "".html_safe) +
30
67
  (error_msg ? @template.content_tag(:p, error_msg, id: error_id, class: "klods-error", role: "alert") : "".html_safe)
31
68
  end
32
69
  end
33
70
 
34
- # Renders a submit button styled as a primary klods button.
35
- #
36
- # = f.klods_submit "Save"
37
- # = f.klods_submit "Sign up", class: "klods-button--wide" # extra class merged in
38
- def klods_submit(label = nil, **options)
39
- extra = options.delete(:class)
40
- options[:class] = ["klods-button", "klods-button--primary", extra].compact.join(" ")
41
- submit(label, **options)
42
- end
43
-
44
- private
45
-
46
71
  def input_helper_for(type)
47
72
  {
48
73
  email: :email_field,
@@ -52,9 +77,25 @@ module Klods
52
77
  number: :number_field,
53
78
  date: :date_field,
54
79
  time: :time_field,
55
- search: :search_field,
56
- textarea: :text_area
80
+ search: :search_field
57
81
  }.fetch(type, :text_field)
58
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
59
100
  end
60
101
  end
data/lib/klods/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Klods
2
- VERSION = "1.3.0"
2
+ VERSION = "1.3.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klods-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Drue Wilding