Class: Trestle::Form::Automatic
- Inherits:
-
Trestle::Form
- Object
- Trestle::Form
- Trestle::Form::Automatic
- Defined in:
- lib/trestle/form/automatic.rb
Instance Attribute Summary
Attributes inherited from Trestle::Form
Instance Method Summary collapse
-
#initialize(admin, options = {}) ⇒ Automatic
constructor
A new instance of Automatic.
Methods inherited from Trestle::Form
Constructor Details
#initialize(admin, options = {}) ⇒ 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/trestle/form/automatic.rb', line 4 def initialize(admin, ={}) super() do |instance| admin.default_form_attributes.each do |attribute| if attribute.array? if [:string, :text].include?(attribute.type) select attribute.name, nil, {}, { multiple: true, data: { tags: true, select_on_close: true }} end else case attribute.type when :association if attribute.polymorphic? static_field attribute.name do if associated_instance = instance.public_send(attribute.association_name) admin_link_to format_value(associated_instance), associated_instance else tag.span(I18n.t("admin.format.blank"), class: "blank") end end else prompt = I18n.t("admin.form.select.prompt", default: "- Select %{attribute_name} -", attribute_name: admin.human_attribute_name(attribute.association_name)) select attribute.name, attribute.association_class.all, include_blank: prompt end when :text text_area attribute.name when :date date_field attribute.name when :datetime datetime_field attribute.name when :boolean form_group label: false do check_box attribute.name end when :enum attribute.name, attribute.[:values] || [], :first, :last when :json, :jsonb value = instance.public_send(attribute.name) text_area attribute.name, value: value.try(:to_json) else text_field attribute.name end end end end end |