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

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

Instance Method Summary collapse

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(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],
  }
end

#field_for(options) ⇒ Object



46
47
48
# File 'lib/trenni/formatters/html/form_formatter.rb', line 46

def field_for(options)
  options[: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(options = {})
  options = @options.merge(options)

  Builder.fragment do |builder|
    builder.tag :input, hidden_attributes_for(options)
  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(options)
  return {
    :type => options[:type] || 'hidden',
    :id => options[:id],
    :class => options[:class],
    :name => name_for(options),
    :value => value_for(options),
  }
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(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[:min],
    :max => options[:max],
    :step => options[:step],
    # for <input type="text">
    :minlength => options[:minlength],
    :maxlength => options[: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(options)
  options[:name] || options[:field]
end

#new_record?Boolean

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

Returns:

  • (Boolean)


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

#objectObject

The target object of the form.



28
29
30
# File 'lib/trenni/formatters/html/form_formatter.rb', line 28

def object
  @options[:object]
end

#object_value_for(options) ⇒ Object



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

def object_value_for(options)
  options[:object].send(field_for(options)) if options[: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(options)
  attributes = {
    :name => name_for(options),
    :id => options[:id],
    :class => options[:class],
    :for => options[:for],
    :form => options[:form],
  }
  
  return attributes
end

#pattern_for(options) ⇒ Object



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

def pattern_for(options)
  options[:pattern]
end

#placeholder_for(options) ⇒ Object



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

def placeholder_for(options)
  options[: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(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



148
149
150
151
152
153
154
155
156
157
# File 'lib/trenni/formatters/html/form_formatter.rb', line 148

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),
  }
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(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],
  }
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(options)
  title = options[:title]
  return Strings::to_html(title) if title
  
  field_name = field_for(options)
  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(options)
  self.format(raw_value_for(options), options)
end