Class: Trestle::Form::Automatic

Inherits:
Trestle::Form show all
Defined in:
lib/trestle/form/automatic.rb

Instance Attribute Summary

Attributes inherited from Trestle::Form

#block

Instance Method Summary collapse

Methods inherited from Trestle::Form

#render

Constructor Details

#initialize(admin) ⇒ Automatic

Returns a new instance of Automatic.



4
5
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
# File 'lib/trestle/form/automatic.rb', line 4

def initialize(admin)
  @block = Proc.new do
    admin.default_attributes.each do |attribute|
      next if attribute.primary_key?
      next if attribute.inheritance_column?
      next if attribute.counter_cache?

      case attribute.type
      when :association
        options = attribute.association_class.all.map { |instance| [display(instance), instance.id] }
        prompt = "- Select #{admin.model.human_attribute_name(attribute.association_name)} -"

        select attribute.name, options, include_blank: prompt
      when :text
        text_area attribute.name
      when :date
        date_field attribute.name
      when :datetime
        datetime_field attribute.name
      when :boolean
        check_box attribute.name
      else
        text_field attribute.name
      end
    end
  end
end