Module: XRB::Formatters::HTML::FormFormatter

Included in:
DefinitionListForm, LabelForm
Defined in:
lib/xrb/formatters/html/form_formatter.rb

Instance Method Summary collapse

Instance Method Details

#button(**options) ⇒ Object

A hidden field.



195
196
197
198
199
200
201
202
203
# File 'lib/xrb/formatters/html/form_formatter.rb', line 195

def button(**options)
	options = @options.merge(**options)

	Builder.fragment do |builder|
		builder.inline :button, button_attributes_for(**options) do
			builder.text button_title_for(**options)
		end
	end
end

#button_attributes_for(**options) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/xrb/formatters/html/form_formatter.rb', line 172

def button_attributes_for(**options)
	return {
		:type => options[:type] || "submit",
		:name => name_for(**options),
		:id => options[:id],
		:class => options[:class],
		:disabled => options[:disabled],
		:value => value_for(**options),
		:data => options[:data],
	}
end

#button_title_for(**options) ⇒ Object



184
185
186
187
188
189
190
191
192
# File 'lib/xrb/formatters/html/form_formatter.rb', line 184

def button_title_for(**options)
	type = options.fetch(:type, "submit").to_sym
	
	if type == :submit
		submit_title_for(**options)
	else
		title_for(**options) || Strings::to_title(type.to_s)
	end
end

#checkbox_attributes_for(**options) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/xrb/formatters/html/form_formatter.rb', line 121

def checkbox_attributes_for(**options)
	return {
		:type => options[:type] || "checkbox",
		:id => options[:id],
		:class => options[:class],
		:name => name_for(**options),
		:value => "true",
		:checked => raw_value_for(**options),
		:required => options[:required],
		:disabled => options[:disabled],
		:readonly => options[:readonly],
		:data => options[:data],
	}
end

#details_for(**options) ⇒ Object

Any additional details relating to a field (e.g. explanation text)



18
19
20
# File 'lib/xrb/formatters/html/form_formatter.rb', line 18

def details_for(**options)
	options[:details]
end

#field_for(**options) ⇒ Object



22
23
24
# File 'lib/xrb/formatters/html/form_formatter.rb', line 22

def field_for(**options)
	options[:field]
end

#fieldset(**options, &block) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/xrb/formatters/html/form_formatter.rb', line 205

def fieldset(**options, &block)
	options = @options.merge(**options)
	buffer = XRB::Template.buffer(block.binding)
	
	Builder.fragment(buffer) do |builder|
		builder.tag("fieldset") do
			builder.inline("legend") do
				builder.text title_for(**options)
			end
			
			yield(builder)
		end
	end
end

#hidden(**options) ⇒ Object

A hidden field.



164
165
166
167
168
169
170
# File 'lib/xrb/formatters/html/form_formatter.rb', line 164

def hidden(**options)
	options = @options.merge(**options)

	Builder.fragment do |builder|
		builder.tag :input, hidden_attributes_for(**options)
	end
end

#hidden_attributes_for(**options) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/xrb/formatters/html/form_formatter.rb', line 152

def hidden_attributes_for(**options)
	return {
		:type => options[:type] || "hidden",
		:id => options[:id],
		:class => options[:class],
		:name => name_for(**options),
		:value => value_for(**options),
		:data => options[:data],
	}
end

#input_attributes_for(**options) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/xrb/formatters/html/form_formatter.rb', line 67

def input_attributes_for(**options)
	attributes = {
		:type => options[:type],
		:name => name_for(**options),
		:id => options[:id],
		:class => options[:class],
		:value => value_for(**options),
		:required => options[:required],
		:disabled => options[:disabled],
		:readonly => options[:readonly],
		:pattern => pattern_for(**options),
		:placeholder => placeholder_for(**options),
		# for <input type="range|number">
		:min => options[:minimum] || options[:min],
		:max => options[:maximum] || options[:max],
		:step => options[:step],
		# for <input type="text">
		:minlength => options[:minimum] || options[:minlength],
		:maxlength => options[:maximum] || options[:maxlength],
		:data => options[:data],
	}
	
	return attributes
end

#new_record?Boolean

Return true if the object is begin created or false if it is being updated.

Returns:

  • (Boolean)


13
14
15
# File 'lib/xrb/formatters/html/form_formatter.rb', line 13

def new_record?
	object.nil? or object.new_record?
end

#object_value_for(**options) ⇒ Object



39
40
41
42
43
# File 'lib/xrb/formatters/html/form_formatter.rb', line 39

def object_value_for(**options)
	if object = options[:object] and field = field_for(**options)
		object.send(field)
	end
end

#output_attributes_for(**options) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/xrb/formatters/html/form_formatter.rb', line 92

def output_attributes_for(**options)
	attributes = {
		:name => name_for(**options),
		:id => options[:id],
		:class => options[:class],
		:for => options[:for],
		:form => options[:form],
		:data => options[:data],
	}
	
	return attributes
end

#pattern_for(**options) ⇒ Object



59
60
61
# File 'lib/xrb/formatters/html/form_formatter.rb', line 59

def pattern_for(**options)
	options[:pattern]
end

#placeholder_for(**options) ⇒ Object



63
64
65
# File 'lib/xrb/formatters/html/form_formatter.rb', line 63

def placeholder_for(**options)
	options[:placeholder]
end

#raw_value_for(**options) ⇒ Object



45
46
47
48
49
50
# File 'lib/xrb/formatters/html/form_formatter.rb', line 45

def raw_value_for(**options)
	value = options.fetch(:value) {object_value_for(**options)}
	
	# Allow to specify a default value if the value given, usually from an object, is nil.
	value || options[:default]
end

#submit_attributes_for(**options) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/xrb/formatters/html/form_formatter.rb', line 136

def submit_attributes_for(**options)
	return {
		:type => options[:type] || "submit",
		:name => name_for(**options),
		:id => options[:id],
		:class => options[:class],
		:disabled => options[:disabled],
		:value => title_for(**options),
		:data => options[:data],
	}
end

#submit_title_for(**options) ⇒ Object



148
149
150
# File 'lib/xrb/formatters/html/form_formatter.rb', line 148

def submit_title_for(**options)
	title_for(**options) || (new_record? ? "Create" : "Update")
end

#textarea_attributes_for(**options) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/xrb/formatters/html/form_formatter.rb', line 105

def textarea_attributes_for(**options)
	return {
		:name => name_for(**options),
		:id => options[:id],
		:class => options[:class],
		:required => options[:required],
		:disabled => options[:disabled],
		:readonly => options[:readonly],
		:pattern => pattern_for(**options),
		:placeholder => placeholder_for(**options),
		:minlength => options[:minlength],
		:maxlength => options[:maxlength],
		:data => options[:data],
	}
end

#title_for(**options) ⇒ Object

A title is a text string that will be displayed next to or on top of the control to describe it or its value:



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/xrb/formatters/html/form_formatter.rb', line 27

def title_for(**options)
	if title = options[:title]
		return title
	end
	
	# Generate a title from a field name:
	if field_name = field_for(**options)
		# Remove postfix "_id" or "_ids":
		return Strings::to_title(field_name.to_s.sub(/_ids?/, ""))
	end
end

#value_for(**options) ⇒ Object

The value of the field.



53
54
55
56
57
# File 'lib/xrb/formatters/html/form_formatter.rb', line 53

def value_for(**options)
	if value = raw_value_for(**options)
		self.format(value, **options)
	end
end