Class: Conjoin::FormBuilder::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/conjoin/form_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, app, record) ⇒ Input

Returns a new instance of Input.



302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/conjoin/form_builder.rb', line 302

def initialize data, app, record
  @data = data
  @app = app
  @record = record
  @options = {
    name: data.name,
    type: :text,
    id: id,
    value: data.value,
    class: ''
  }.merge! data.options
  options[:class] += ' form-control'
  @options
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



300
301
302
# File 'lib/conjoin/form_builder.rb', line 300

def app
  @app
end

#dataObject

Returns the value of attribute data.



300
301
302
# File 'lib/conjoin/form_builder.rb', line 300

def data
  @data
end

#optionsObject

Returns the value of attribute options.



300
301
302
# File 'lib/conjoin/form_builder.rb', line 300

def options
  @options
end

#recordObject

Returns the value of attribute record.



300
301
302
# File 'lib/conjoin/form_builder.rb', line 300

def record
  @record
end

Instance Method Details

#displayObject



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/conjoin/form_builder.rb', line 341

def display
  append_button = options.delete :append_button

  if append_button
    mab do
      div class: 'input-group' do
        input options
        div class: 'input-group-btn' do
          button class: 'btn btn-primary', type: append_button[:type] || 'button', 'on-click-get' => append_button[:href] do
            text append_button[:text]
          end
        end
      end
    end
  else
    mab { input options }
  end
end

#errors?Boolean

Returns:

  • (Boolean)


328
329
330
# File 'lib/conjoin/form_builder.rb', line 328

def errors?
  data.errors
end

#idObject



317
318
319
# File 'lib/conjoin/form_builder.rb', line 317

def id
  data.name.gsub(/[^a-z0-9]/, '_').gsub(/__/, '_').gsub(/_$/, '')
end

#nested_nameObject



321
322
323
324
325
326
# File 'lib/conjoin/form_builder.rb', line 321

def nested_name
  # create field names that map to the correct models
  data.names.each_with_index.map do |field, i|
    i != 0 ? "[#{field}]" : field
  end.join
end

#renderObject



332
333
334
335
336
337
338
339
# File 'lib/conjoin/form_builder.rb', line 332

def render
  if options[:type] == :hidden \
  or (options.key?(:wrapper) and options[:wrapper] == false)
    options[:class] = options[:class].gsub(/form-control/, '')
  end

  display
end