Class: Erector::FormFor::WrappedInput

Inherits:
Widget
  • Object
show all
Defined in:
lib/erector/form_for.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, object, object_name, column, opts) ⇒ WrappedInput

Returns a new instance of WrappedInput.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/erector/form_for.rb', line 145

def initialize app, object, object_name, column, opts
  @app = app
  assigns = {}

  [:as, :required].each do |key|
    opts.has_key?(key) and assigns[key] = opts.delete(key)
  end

  case column.to_s
  when /email/
    assigns[:as] ||= :email
  when /url/
    assigns[:as] ||= :url
  when /password/
    assigns[:as] ||= :password
  when /search/, /query/, 'q'
    assigns[:as] ||= :search
  when /phone/, /fax/
    assigns[:as] ||= :tel
  end

  if Sequel::Model === object
    column_schema = object.db_schema[column.to_sym]
    assigns[:required] = !column_schema[:allow_null] unless assigns.has_key?(:required)

    type = column_schema[:db_type]
    case type 
    when /char/ 
      opts[:maxlength] ||= /\d+/.match(type)
      assigns[:as]     ||= :string
    else
      assigns[:as]     ||= type.downcase
    end
  end

  super assigns

  case @as = @as.to_sym
  when :hidden, :string, :text, :boolean, :email, :color, :date, :datetime, :'datetime-local', :datetime_local, :email, :file, :image, :month, :number, :password, :range, :search, :tel, :phone, :fax, :time, :url, :week, :select, :radio
    @widget = Input.new @app, @as, object, object_name, column, opts.merge(:required => @required)
  else
    raise ArgumentError, ":as => #{@as.inspect} is not a valid option"
  end

  @wrapper_html      = {:class => []}
  @wrapper_html[:id] = "input-#{@widget.id}"
  @wrapper_html[:class].push(@required ? 'required' : 'optional') 
  @wrapper_html[:class].push @as
end