Module: Trenni::Formatters::HTML::FormFormatter
- Included in:
- DefinitionListForm
- Defined in:
- lib/trenni/formatters/html/form_formatter.rb
Instance Method Summary collapse
- #checkbox_attributes_for(options) ⇒ Object
- #field_for(options) ⇒ Object
-
#hidden(options = {}) ⇒ Object
A hidden field.
- #hidden_attributes_for(options) ⇒ Object
- #input_attributes_for(options) ⇒ Object
-
#name_for(options) ⇒ Object
The name of the field, used for the name attribute of an input.
-
#new_record? ⇒ Boolean
Return true if the object is begin created or false if it is being updated.
-
#object ⇒ Object
The target object of the form.
- #object_value_for(options) ⇒ Object
- #output_attributes_for(options) ⇒ Object
- #pattern_for(options) ⇒ Object
- #placeholder_for(options) ⇒ Object
- #raw_value_for(options) ⇒ Object
- #submit_attributes_for(options) ⇒ Object
- #textarea_attributes_for(options) ⇒ Object
-
#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:.
-
#value_for(options) ⇒ Object
The value of the field.
Instance Method Details
#checkbox_attributes_for(options) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 134 def checkbox_attributes_for() return { :type => [:type] || 'checkbox', :id => [:id], :class => [:class], :name => name_for(), :value => 'true', :checked => raw_value_for(), :required => [:required], :disabled => [:disabled], :readonly => [:readonly], } end |
#field_for(options) ⇒ Object
46 47 48 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 46 def field_for() [:field] end |
#hidden(options = {}) ⇒ Object
A hidden field.
170 171 172 173 174 175 176 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 170 def hidden( = {}) = .merge() Builder.fragment do |builder| builder.tag :input, hidden_attributes_for() end end |
#hidden_attributes_for(options) ⇒ Object
159 160 161 162 163 164 165 166 167 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 159 def hidden_attributes_for() return { :type => [:type] || 'hidden', :id => [:id], :class => [:class], :name => name_for(), :value => value_for(), } end |
#input_attributes_for(options) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 83 def input_attributes_for() attributes = { :type => [:type], :name => name_for(), :id => [:id], :class => [:class], :value => value_for(), :required => [:required], :disabled => [:disabled], :readonly => [:readonly], :pattern => pattern_for(), :placeholder => placeholder_for(), # for <input type="range|number"> :min => [:min], :max => [:max], :step => [:step], # for <input type="text"> :minlength => [:minlength], :maxlength => [:maxlength], } return attributes end |
#name_for(options) ⇒ Object
The name of the field, used for the name attribute of an input.
42 43 44 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 42 def name_for() [:name] || [:field] end |
#new_record? ⇒ Boolean
Return true if the object is begin created or false if it is being updated.
33 34 35 36 37 38 39 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 33 def new_record? if object.respond_to? :new_record? return object.new_record? elsif self.object.respond_to? :saved? return !object.saved? end end |
#object ⇒ Object
The target object of the form.
28 29 30 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 28 def object [:object] end |
#object_value_for(options) ⇒ Object
59 60 61 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 59 def object_value_for() [:object].send(field_for()) if [:object] end |
#output_attributes_for(options) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 107 def output_attributes_for() attributes = { :name => name_for(), :id => [:id], :class => [:class], :for => [:for], :form => [:form], } return attributes end |
#pattern_for(options) ⇒ Object
75 76 77 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 75 def pattern_for() [:pattern] end |
#placeholder_for(options) ⇒ Object
79 80 81 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 79 def placeholder_for() [:placeholder] end |
#raw_value_for(options) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 63 def raw_value_for() value = .fetch(:value) { object_value_for() } # Allow to specify a default value if the value given, usually from an object, is nil. value || [:default] end |
#submit_attributes_for(options) ⇒ Object
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 148 def submit_attributes_for() return { :type => [:type] || 'submit', :name => name_for(), :id => [:id], :class => [:class], :disabled => [:disabled], :value => title_for(), } end |
#textarea_attributes_for(options) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 119 def textarea_attributes_for() return { :name => name_for(), :id => [:id], :class => [:class], :required => [:required], :disabled => [:disabled], :readonly => [:readonly], :pattern => pattern_for(), :placeholder => placeholder_for(), :minlength => [:minlength], :maxlength => [:maxlength], } 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:
51 52 53 54 55 56 57 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 51 def title_for() title = [:title] return Strings::to_html(title) if title field_name = field_for() return Strings::to_title(field_name.to_s) if field_name end |
#value_for(options) ⇒ Object
The value of the field.
71 72 73 |
# File 'lib/trenni/formatters/html/form_formatter.rb', line 71 def value_for() self.format(raw_value_for(), ) end |