Module: Trenni::Formatters::HTML::FormFormatter

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

Instance Method Summary collapse

Instance Method Details

#button(**options) ⇒ Object

A hidden field.



212
213
214
215
216
217
218
219
220
# File 'lib/trenni/formatters/html/form_formatter.rb', line 212

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



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/trenni/formatters/html/form_formatter.rb', line 189

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



201
202
203
204
205
206
207
208
209
# File 'lib/trenni/formatters/html/form_formatter.rb', line 201

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



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/trenni/formatters/html/form_formatter.rb', line 138

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)



35
36
37
# File 'lib/trenni/formatters/html/form_formatter.rb', line 35

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

#field_for(**options) ⇒ Object



39
40
41
# File 'lib/trenni/formatters/html/form_formatter.rb', line 39

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

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



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/trenni/formatters/html/form_formatter.rb', line 222

def fieldset(**options, &block)
	options = @options.merge(**options)
	buffer = Trenni::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.



181
182
183
184
185
186
187
# File 'lib/trenni/formatters/html/form_formatter.rb', line 181

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



169
170
171
172
173
174
175
176
177
178
# File 'lib/trenni/formatters/html/form_formatter.rb', line 169

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/trenni/formatters/html/form_formatter.rb', line 84

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)


30
31
32
# File 'lib/trenni/formatters/html/form_formatter.rb', line 30

def new_record?
	object.new_record?
end

#object_value_for(**options) ⇒ Object



56
57
58
59
60
# File 'lib/trenni/formatters/html/form_formatter.rb', line 56

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

#output_attributes_for(**options) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/trenni/formatters/html/form_formatter.rb', line 109

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



76
77
78
# File 'lib/trenni/formatters/html/form_formatter.rb', line 76

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

#placeholder_for(**options) ⇒ Object



80
81
82
# File 'lib/trenni/formatters/html/form_formatter.rb', line 80

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

#raw_value_for(**options) ⇒ Object



62
63
64
65
66
67
# File 'lib/trenni/formatters/html/form_formatter.rb', line 62

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



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

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



165
166
167
# File 'lib/trenni/formatters/html/form_formatter.rb', line 165

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

#textarea_attributes_for(**options) ⇒ Object



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

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:



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/trenni/formatters/html/form_formatter.rb', line 44

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.



70
71
72
73
74
# File 'lib/trenni/formatters/html/form_formatter.rb', line 70

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