class Godmin::FormBuilders::FormBuilder

Public Instance Methods

association(attribute, options = {}, html_options = {}) click to toggle source
# File lib/godmin/helpers/forms.rb, line 31
def association(attribute, options = {}, html_options = {})
  case association_type(attribute)
  when :belongs_to
    select("#{attribute}_id", association_collection_for_select(attribute), options, html_options.deep_merge(
      data: { behavior: "select-box" }
    ))
  else
    input(attribute, options)
  end
end
date_field(attribute, options = {}) click to toggle source
# File lib/godmin/helpers/forms.rb, line 42
def date_field(attribute, options = {})
  text_field(attribute, options.deep_merge(
    value: datetime_value(attribute, options, :datepicker),
    data: { behavior: "datepicker" }
  ))
end
datetime_field(attribute, options = {}) click to toggle source
# File lib/godmin/helpers/forms.rb, line 49
def datetime_field(attribute, options = {})
  text_field(attribute, options.deep_merge(
    value: datetime_value(attribute, options, :datetimepicker),
    data: { behavior: "datetimepicker" }
  ))
end
input(attribute, options = {}) click to toggle source
# File lib/godmin/helpers/forms.rb, line 16
def input(attribute, options = {})
  case attribute_type(attribute)
  when :text
    text_area(attribute, options)
  when :boolean
    check_box(attribute, options)
  when :date
    date_field(attribute, options)
  when :datetime
    datetime_field(attribute, options)
  else
    text_field(attribute, options)
  end
end

Private Instance Methods

association_collection(attribute) click to toggle source
# File lib/godmin/helpers/forms.rb, line 68
def association_collection(attribute)
  association_reflection(attribute).try(:klass).try(:all)
end
association_collection_for_select(attribute) click to toggle source
# File lib/godmin/helpers/forms.rb, line 76
def association_collection_for_select(attribute)
  association_collection(attribute).map { |a| [a.to_s, a.id] }
end
association_reflection(attribute) click to toggle source
# File lib/godmin/helpers/forms.rb, line 72
def association_reflection(attribute)
  @object.class.reflect_on_association(attribute)
end
association_type(attribute) click to toggle source
# File lib/godmin/helpers/forms.rb, line 64
def association_type(attribute)
  association_reflection(attribute).try(:macro)
end
attribute_type(attribute) click to toggle source
# File lib/godmin/helpers/forms.rb, line 58
def attribute_type(attribute)
  if @object.has_attribute?(attribute)
    @object.column_for_attribute(attribute).type
  end
end
datetime_value(attribute, options, format) click to toggle source
# File lib/godmin/helpers/forms.rb, line 80
def datetime_value(attribute, options, format)
  value = options[:value] || @object.send(attribute)
  value.try(:strftime, @template.translate_scoped("datetimepickers.#{format}"))
end