Class: Padrino::Helpers::FormBuilder::PadrinoFieldsBuilder

Inherits:
AbstractFormBuilder
  • Object
show all
Includes:
PadrinoFields::DataMapperWrapper, PadrinoFields::Settings
Defined in:
lib/padrino-fields/form_builder.rb

Overview

:nodoc:

Constant Summary collapse

@@settings =
PadrinoFields::Settings

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PadrinoFields::Settings

configure

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



16
17
18
# File 'lib/padrino-fields/form_builder.rb', line 16

def object
  @object
end

#object_nameObject (readonly)

Returns the value of attribute object_name.



16
17
18
# File 'lib/padrino-fields/form_builder.rb', line 16

def object_name
  @object_name
end

#templateObject (readonly)

Returns the value of attribute template.



16
17
18
# File 'lib/padrino-fields/form_builder.rb', line 16

def template
  @template
end

Instance Method Details

#collect_inputs_as(attribute, type, options = {}) ⇒ Object



67
68
69
70
71
# File 'lib/padrino-fields/form_builder.rb', line 67

def collect_inputs_as(attribute,type,options={})
  options[:options].map do |item|
    collection_input(attribute,type,item,options.keep_if {|key, value| key != :options})
  end.join("")
end

#collection_input(attribute, type, item, options = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/padrino-fields/form_builder.rb', line 106

def collection_input(attribute,type,item, options={})
  unchecked_value = options.delete(:uncheck_value) || '0'
  options.reverse_merge!(:id => field_id(attribute), :value => '1')
  options.reverse_merge!(:checked => true) if values_matches_field?(attribute, options[:value])
  klass =  css_class(attribute,type,options[:disabled])
  name  =  type == :checks ? field_name(attribute) + '[]' : field_name(attribute)
  if item.is_a?(Array)
    text, value = item[0], item[1]
  else
    text = item; value = item;
  end
  id = field_id(attribute) + "_" + domize(text)
  opts = html_options(attribute,type,options.merge(:id => id, :class => klass, :value => value))
  if type == :checks
    html = @template.hidden_field_tag(options[:name] || name, :value => unchecked_value, :id => nil)
    input_item = @template.check_box_tag(name, opts)
  else
    html = ""
    input_item = @template.radio_button_tag(name, opts)
  end
  html << "<label for='#{id}' class='#{klass}'>#{input_item}#{text}</label>"
end

#css_class(attribute, type, disabled = false) ⇒ Object



143
144
145
146
147
148
# File 'lib/padrino-fields/form_builder.rb', line 143

def css_class(attribute,type,disabled=false)
  klass = type.to_s
  klass << ' required' if required?(attribute)
  klass << ' required' if disabled
  klass
end

#default_input(attribute, type, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/padrino-fields/form_builder.rb', line 36

def default_input(attribute,type,options={})
  input_options = options.keep_if {|key, value| key != :as}
  if options[:options] || options[:grouped_options]
    if type==:radios || type == :checks
      collect_inputs_as(attribute,type,input_options)
    else
      select(attribute,input_options)
    end
  else
    singular_input_for(attribute,type,options)
  end
end

#default_radios(attribute, type, options) ⇒ Object



170
171
172
173
174
# File 'lib/padrino-fields/form_builder.rb', line 170

def default_radios(attribute,type,options)
  [['yes',1],['no',0]].map do |item|
    collection_input(attribute,:radios,item,options.keep_if {|key, value| key != :options})
  end.join("")
end

#domize(text) ⇒ Object



129
130
131
# File 'lib/padrino-fields/form_builder.rb', line 129

def domize(text)
  text.downcase.gsub(' ','_').gsub(/[^[:alnum:]]/, '')
end

#hint(text) ⇒ Object



166
167
168
# File 'lib/padrino-fields/form_builder.rb', line 166

def hint(text)
  @template.(:span, :class=>'hint') { text }
end

#html_options(attribute, type, options) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/padrino-fields/form_builder.rb', line 150

def html_options(attribute,type,options)
  options.keep_if {|key, value| key != :as}
  html_class = options.merge(:class => css_class(attribute,type,options[:disabled]))
  input_html = options[:input_html]
  if input_html
    html_class.merge(input_html) {|key, first, second| first + " " + second }.delete(:input_html)
  else
    html_class
  end
end

#input(attribute, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/padrino-fields/form_builder.rb', line 18

def input(attribute, options={})
  options.reverse_merge!(:caption => options.delete(:caption)) if options[:caption]
  type = options[:as] || klazz.form_column_type_for(attribute)
  field_html =  ""
  if type == :boolean || options[:as] == :boolean
    field_html << default_input(attribute,type,options) 
    field_html << setup_label(attribute,type,labelize(options))
  else
    field_html << setup_label(attribute,type,labelize(options))
    field_html << default_input(attribute,type, options)
  end
  field_html << hint(options[:hint]) if options[:hint]
  field_html << @template.error_message_on(@object,attribute,{}) if @object.errors.any?
  @template.(@@settings.container, :class => css_class(attribute,type,options[:disabled])) do
    field_html
  end
end

#klazzObject



176
177
178
# File 'lib/padrino-fields/form_builder.rb', line 176

def klazz
  @object.class
end

#labelize(options) ⇒ Object



161
162
163
164
# File 'lib/padrino-fields/form_builder.rb', line 161

def labelize(options)
  opts = {}.merge(options)
  opts.keep_if {|key, value| [:class,:id,:caption].include?(key)}
end

#required?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/padrino-fields/form_builder.rb', line 101

def required?(attribute)
  k = klazz.is_a?(String) ? klazz.constantize : klazz
  k.form_attribute_is_required?(attribute)
end

#setup_label(attribute, type, options = {}) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/padrino-fields/form_builder.rb', line 133

def setup_label(attribute, type, options={})
  marker = @@settings.label_required_marker
  text = ""
  text << marker if required?(attribute) && @@settings.label_required_marker_position == :prepend
  text << field_human_name(attribute)
  text << marker if required?(attribute) && @@settings.label_required_marker_position == :append
  options.reverse_merge!(:caption => text, :class => css_class(attribute,type,options[:disabled]))
  @template.label_tag(field_id(attribute), options)
end

#singular_input_for(attribute, type, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/padrino-fields/form_builder.rb', line 73

def singular_input_for(attribute,type,options={})
  options.keep_if {|key, value| key != :for}
  opts = html_options(attribute,type,options)
  case type
  when :string
    case attribute
    when /email/         ; email_field(attribute, opts);
    when /password/      ; password_field(attribute, opts);
    when /tel/,/phone/   ; tel_field(attribute, opts);
    when /url/,/website/ ; url_field(attribute, opts);
    when /search/        ; search_field(attribute, opts);
    else                 ; text_field(attribute, opts);
    end
  when :text
    text_area(attribute, opts)
  when :boolean
    check_box(attribute, opts)
  when :date
    date_field(attribute, opts)
  when :file
    file_field(attribute, opts)
  when :number
    number_field(attribute, opts)
  when :radios
    default_radios(attribute,type,options)
  end
end