Class: DynamicScaffold::FormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_scaffold/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ FormBuilder

Returns a new instance of FormBuilder.



214
215
216
217
# File 'lib/dynamic_scaffold/config.rb', line 214

def initialize(config)
  @config = config
  @items = []
end

Instance Method Details

#item(type, *args, &block) ⇒ Object

rubocop:disable Metrics/MethodLength



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/dynamic_scaffold/config.rb', line 230

def item(type, *args, &block) # rubocop:disable Metrics/MethodLength
  case type
  when
    :check_box,
    :radio_button,
    :text_area,
    :text_field,
    :password_field,
    :hidden_field,
    :file_field,
    :color_field then
    item = Form::Item::SingleOption.new(@config, type, *args)
  when
    :time_select,
    :date_select,
    :datetime_select,
    :collection_select,
    :grouped_collection_select then
    item = Form::Item::TwoOptions.new(@config, type, *args)
  when
    :collection_check_boxes,
    :collection_radio_buttons then
    item = Form::Item::TwoOptionsWithBlock.new(@config, type, *args)
  when
    :block then
    item = Form::Item::Block.new(@config, type, *args, block)
  else
    raise DynamicScaffold::Error::InvalidParameter, "Unknown form item type #{type}"
  end
  @items << item
  item
end

#itemsObject



219
220
221
222
223
224
225
226
227
228
# File 'lib/dynamic_scaffold/config.rb', line 219

def items
  if @items.empty?
    @config.model.column_names.each do |column|
      type = :text_field
      type = :hidden_field if @config.scope && @config.scope.include?(column.to_sym)
      @items << Form::Item::SingleOption.new(@config, type, column)
    end
  end
  @items
end