module Trenni::Formatters::HTML::FormFormatter
Public Instance Methods
checkbox_attributes_for(**options)
click to toggle source
# 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)
click to toggle source
Any additional details relating to a field (e.g. explanation text)
# File lib/trenni/formatters/html/form_formatter.rb, line 35 def details_for(**options) options[:details] end
field_for(**options)
click to toggle source
# File lib/trenni/formatters/html/form_formatter.rb, line 39 def field_for(**options) options[:field] end
fieldset(**options) { |builder| ... }
click to toggle source
# 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
input_attributes_for(**options)
click to toggle source
# 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?()
click to toggle source
Return true if the object is begin created or false if it is being updated.
# File lib/trenni/formatters/html/form_formatter.rb, line 30 def new_record? object.new_record? end
object_value_for(**options)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/trenni/formatters/html/form_formatter.rb, line 76 def pattern_for(**options) options[:pattern] end
placeholder_for(**options)
click to toggle source
# File lib/trenni/formatters/html/form_formatter.rb, line 80 def placeholder_for(**options) options[:placeholder] end
raw_value_for(**options)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
A title is a text string that will be displayed next to or on top of the control to describe it or its value:
# 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)
click to toggle source
The value of the field.
# 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