pxs-forms 0.0.2 → 0.0.4
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 +4 -4
- data/lib/pxs/forms/model_form_builder.rb +215 -0
- data/lib/pxs/forms/model_helpers.rb +1 -1
- data/lib/pxs/forms/version.rb +1 -1
- data/lib/pxs/forms.rb +1 -5
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b78b1ae5381a69dd4f9b75d4c90878f8eaadc09c3a336c5ded134d92e529ab6b
|
4
|
+
data.tar.gz: 8325584e7c5158a52ffb7f5f8618b2001b897e83f15c41731b55332ac14292ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6de19d9021c51c308c53640a1eea5570da58149f0353d552b26ca9b0dab9657ef6a1acab747a98dfcc13ad84036c509636ff43805a70a71ba240e3e70b96e41
|
7
|
+
data.tar.gz: 5c9cefb22209e754a9cc32141bc6b4308c95f48a3cd937952e7ddfc2cd75e50788905e79823c72d9d33bea13e300ee554714af313a696d7af4c4291a8452c9d9
|
@@ -0,0 +1,215 @@
|
|
1
|
+
class ModelFormBuilder < ActionView::Helpers::FormBuilder
|
2
|
+
|
3
|
+
delegate :tag, :safe_join, to: :@template
|
4
|
+
|
5
|
+
# define a model field
|
6
|
+
# the format is deduced from the column type or attribute name
|
7
|
+
def field(attribute, options = {})
|
8
|
+
@form_options = options
|
9
|
+
|
10
|
+
# extract the object type from the attribute
|
11
|
+
object_type = object_type_for_attribute(attribute)
|
12
|
+
|
13
|
+
# set the input type depending on the attribute type
|
14
|
+
input_type = case object_type
|
15
|
+
when :date then :string
|
16
|
+
when :integer then :string
|
17
|
+
else object_type
|
18
|
+
end
|
19
|
+
|
20
|
+
# if as: :input_type is set, use it to set input type
|
21
|
+
override_input_type = if options[:as]
|
22
|
+
options[:as]
|
23
|
+
# for collections, use a Select
|
24
|
+
elsif options[:collection]
|
25
|
+
:select
|
26
|
+
end
|
27
|
+
|
28
|
+
# return result of [input_type]_input method
|
29
|
+
send("#{override_input_type || input_type}_input", attribute, options)
|
30
|
+
end
|
31
|
+
|
32
|
+
def radio_buttons_field(attribute, options = {})
|
33
|
+
collection_of(:radio_buttons, attribute, options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def check_boxes_field(attribute, options = {})
|
37
|
+
collection_of(:check_boxes, attribute, options)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def object_type_for_attribute(attribute)
|
42
|
+
# if @object defines an attribute
|
43
|
+
result = if @object.respond_to?(:type_for_attribute) && @object.has_attribute?(attribute)
|
44
|
+
# return attribute type
|
45
|
+
@object.type_for_attribute(attribute.to_s).try(:type)
|
46
|
+
# else if @object matches a column
|
47
|
+
elsif @object.respond_to?(:column_for_attribute) && @object.has_attribute?(attribute)
|
48
|
+
@object.column_for_attribute(attribute).try(:type)
|
49
|
+
end
|
50
|
+
|
51
|
+
result || :string
|
52
|
+
end
|
53
|
+
|
54
|
+
## inputs and helpers
|
55
|
+
|
56
|
+
def string_input(attribute, options = {})
|
57
|
+
field_block(attribute, options) do
|
58
|
+
safe_join [
|
59
|
+
(field_label(attribute, options[:label]) unless options[:label] == false),
|
60
|
+
string_field(attribute, merge_input_options({class: "form-control #{"is-invalid" if has_error?(attribute)}"}, options[:input_html])),
|
61
|
+
]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def text_input(attriubte, options = {})
|
66
|
+
field_block(attribute, options) do
|
67
|
+
safe_join [
|
68
|
+
(field_label(attribute, options[:label]) unless options[:label] == false),
|
69
|
+
text_area(attribute, merge_input_options({class: "form-control #{"is-invalid" if has_error?(attribute)}"}, options[:input_html])),
|
70
|
+
]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def boolean_input(attribute, options = {})
|
75
|
+
field_block(attribute, options) do
|
76
|
+
tag.div(class: "checkbox-field") do
|
77
|
+
safe_join [
|
78
|
+
check_box(attribute, merge_input_options({class: "checkbox-input"}, options[:input_html])),
|
79
|
+
label(attribute, options[:label], class: "checkbox-label"),
|
80
|
+
]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def collection_input(attriubte, options, &block)
|
86
|
+
field_block(method, options) do
|
87
|
+
safe_join [
|
88
|
+
label(method, options[:label]),
|
89
|
+
block.call,
|
90
|
+
]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def select_input(attribute, options = {})
|
95
|
+
|
96
|
+
# default value method to :id
|
97
|
+
value_method = options[:value_method] || :id
|
98
|
+
# default text method to :name
|
99
|
+
text_method = options[:text_method] || :name
|
100
|
+
input_options = options[:input_html] || {}
|
101
|
+
|
102
|
+
multiple = input_options[:multiple]
|
103
|
+
|
104
|
+
collection_input(attribute, options) do
|
105
|
+
collection_select(attribute, options[:collection], value_method, text_method, options, merge_input_options({class: "#{"custom-select" unless multiple} form-control #{"is-invalid" if has_error?(attribute)}"}, options[:input_html]))
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def grouped_select_input(attribute, options = {})
|
110
|
+
|
111
|
+
# We probably need to go back later and adjust this for more customization
|
112
|
+
collection_input(attribute, options) do
|
113
|
+
grouped_collection_select(attribute, options[:collection], :last, :first, :to_s, :to_s, options, merge_input_options({class: "custom-select form-control #{"is-invalid" if has_error?(attribute)}"}, options[:input_html]))
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def file_input(attribute, options = {})
|
118
|
+
field_block(attribute, options) do
|
119
|
+
safe_join [
|
120
|
+
(field_label(attribute, options[:label]) unless options[:label] == false),
|
121
|
+
custom_file_field(attribute, options),
|
122
|
+
]
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def collection_of(input_type, attribute, options = {})
|
127
|
+
form_builder_method, custom_class, input_builder_method = case input_type
|
128
|
+
when :radio_buttons then [:collection_radio_buttons, "radio", :radio_button]
|
129
|
+
when :check_boxes then [:collection_check_boxes, "checkbox", :check_box]
|
130
|
+
else raise "Invalid input_type for collection_of, valid input_types are \":radio_buttons\", \":check_boxes\""
|
131
|
+
end
|
132
|
+
|
133
|
+
field_block(attribute, options) do
|
134
|
+
safe_join [
|
135
|
+
field_label(attribute, options[:label]),
|
136
|
+
tag.div(class: "#{custom_class}-list") {
|
137
|
+
(send(form_builder_method, attribute, options[:collection], options[:value_method], options[:text_method]) do |b|
|
138
|
+
tag.div(class: "#{custom_class}") {
|
139
|
+
safe_join [
|
140
|
+
b.send(input_builder_method, class: "#{custom_class}-input"),
|
141
|
+
b.label(class: "#{custom_class}-label"),
|
142
|
+
]
|
143
|
+
}
|
144
|
+
end)
|
145
|
+
},
|
146
|
+
]
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def string_field(attribute, options = {})
|
151
|
+
case object_type_for_attribute(attribute)
|
152
|
+
when :date then
|
153
|
+
safe_join [
|
154
|
+
date_field(attribute, merge_input_options(options, {data: {datepicker: true}})),
|
155
|
+
tag.div {
|
156
|
+
date_select(attribute, {
|
157
|
+
order: [:year, :month, :day],
|
158
|
+
start_year: Date.today.year,
|
159
|
+
end_year: Date.today.year,
|
160
|
+
}, {data: {date_select: true}})
|
161
|
+
},
|
162
|
+
]
|
163
|
+
when :integer then number_field(attribute, options)
|
164
|
+
when :string
|
165
|
+
case attribute.to_s
|
166
|
+
when /password/ then password_field(attribute, options)
|
167
|
+
# when /time_zone/ then :time_zone
|
168
|
+
# when /country/ then :country
|
169
|
+
when /email/ then email_field(attribute, options)
|
170
|
+
when /phone/ then telephone_field(attribute, options)
|
171
|
+
when /url/ then url_field(attribute, options)
|
172
|
+
else
|
173
|
+
text_field(attribute, options)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def field_block(attribute, options = {}, &block)
|
179
|
+
tag.div class: "field #{attribute}", id: options[:field_id] do
|
180
|
+
safe_join [
|
181
|
+
block.call,
|
182
|
+
hint_text(options[:hint]),
|
183
|
+
error_text(attribute),
|
184
|
+
].compact
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def field_label(attribute, options = {})
|
189
|
+
label(attribute, options)
|
190
|
+
end
|
191
|
+
|
192
|
+
def hint_text(text)
|
193
|
+
unless text.nil?
|
194
|
+
tag.small text, class: "hint hint--form"
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
def error_text(attribute)
|
199
|
+
if has_error? attribute
|
200
|
+
tag.div @object.errors[method].join("<br />").html_safe, class: "form-errors"
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
def has_error?(attribute)
|
205
|
+
return false unless @object.respond_to?(:errors)
|
206
|
+
@object.errors.key?(attribute)
|
207
|
+
end
|
208
|
+
|
209
|
+
def merge_input_options(options, user_options)
|
210
|
+
return options if user_options.nil?
|
211
|
+
|
212
|
+
# TODO handle class merging here
|
213
|
+
options.merge(user_options)
|
214
|
+
end
|
215
|
+
end
|
@@ -47,7 +47,7 @@ module Pxs
|
|
47
47
|
|
48
48
|
def model_form_with(model: nil, scope: nil, url: nil, format: nil, **options, &block)
|
49
49
|
options = options.reverse_merge(builder: ModelFormBuilder)
|
50
|
-
form_with(model: model, scope: scope, url: url, format: format, class: "form#{options.has_key? :class ? " #{options[:class]}" : ""}", **options, &block)
|
50
|
+
form_with(model: model, scope: scope, url: url, format: format, class: "form#{(options.has_key? :class) ? " #{options[:class]}" : ""}", **options, &block)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
end
|
data/lib/pxs/forms/version.rb
CHANGED
data/lib/pxs/forms.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pxs-forms
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Poubelle
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-03-
|
11
|
+
date: 2024-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -25,6 +25,7 @@ files:
|
|
25
25
|
- Rakefile
|
26
26
|
- lib/pxs/forms.rb
|
27
27
|
- lib/pxs/forms/link_helpers.rb
|
28
|
+
- lib/pxs/forms/model_form_builder.rb
|
28
29
|
- lib/pxs/forms/model_helpers.rb
|
29
30
|
- lib/pxs/forms/version.rb
|
30
31
|
- pxs-forms.gemspec
|