Module: ActiveExt::ExtHelpers::FormBuilder

Defined in:
lib/active_ext/ext_helpers/form_builder.rb

Class Method Summary collapse

Class Method Details

.build_form_fields(core, model_id, action) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_ext/ext_helpers/form_builder.rb', line 6

def build_form_fields(core, model_id, action)
  fields = []

  if action != 'new'
    #add hidden id
    fields << ActiveExt::ExtHelpers::FieldBuilder.build_field(core.columns[:id], get_value(core, model_id, :id), false, true)
    #add id column if showing it
    if core.options[:show_id]
      fields << ActiveExt::ExtHelpers::FieldBuilder.build_field(core.columns[:id], get_value(core, model_id, :id), true)
    end
  end

  display_only = action == 'show'

  #build ext columns
  core.columns.each do |column|
    next if column.name.to_s =~ /(^id|created_at|updated_at)$/  || core.columns.exclude_column?(column.name)
    next if column.sql_type.blank? || column.sql_type == NilClass
    fields << ActiveExt::ExtHelpers::FieldBuilder.build_field(column, get_value(core, model_id, column.name), display_only)
  end

  #add timestamp columns if showing them
  if core.options[:show_timestamps] && action != 'new'
    fields << ActiveExt::ExtHelpers::FieldBuilder.build_field(core.columns[:created_at], get_value(core, model_id, :created_at), true)
    fields << ActiveExt::ExtHelpers::FieldBuilder.build_field(core.columns[:updated_at], get_value(core, model_id, :updated_at), true)
  end

  fields.to_json
end

.get_value(core, model_id, name) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/active_ext/ext_helpers/form_builder.rb', line 36

def get_value(core, model_id, name)
  value = nil

  unless model_id.nil?
    value = core.model.find(model_id).send(name)
  end

  value
end