Sha256: ba6c0d37c0f877b36ea213e992444bfd43f78b20bed23b5f26913dd665f92336

Contents?: true

Size: 1.81 KB

Versions: 8

Compression:

Stored size: 1.81 KB

Contents

module Netzke
  module Basepack
    # Takes care of automatic field configuration in {Basepack::Form}
    class FieldConfig < AttrConfig
      def set_defaults!
        super

        self.attr_type ||= @data_adapter.attr_type(name)

        set_xtype! if xtype.nil?

        self.field_label ||= @data_adapter.human_attribute_name(name).gsub(/\s+/, " ")

        self.hidden = true if hidden.nil? && primary?
        self.hide_label = hidden if hide_label.nil?

        case attr_type
        when :boolean
          configure_checkbox!
        when :date
          configure_date_field!
        end
      end

    private

      def set_xtype!
        if association?
          set_xtype_for_association!
        else
          self.xtype = xtype_for_attr_type(attr_type)
        end
      end

      def set_xtype_for_association!
        assoc_name, method = name.split('__').map(&:to_sym)
        assoc_method_type = @data_adapter.get_assoc_property_type(assoc_name, method)
        if nested_attribute
          self.xtype = xtype_for_attr_type(assoc_method_type)
        else
          self.xtype = assoc_method_type == :boolean ? xtype_for_attr_type(assoc_method_type) : :netzkeremotecombo
        end
      end

      def xtype_for_attr_type(type)
        { integer:    :numberfield,
          boolean:    :checkboxfield,
          date:       :datefield,
          datetime:   :xdatetime,
          text:       :textarea,
          json:       :jsonfield,
          string:     :textfield
        }[type] || :textfield
      end

      def configure_checkbox!
        self.checked = value
        self.unchecked_value = false
        self.input_value = true
      end

      def configure_date_field!
        self.submit_format = "Y-m-d"
        self.format ||= I18n.t("date", scope: 'netzke.formats', default: "Y-m-d")
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
netzke-basepack-0.10.1 lib/netzke/basepack/field_config.rb
netzke-basepack-0.10.0 lib/netzke/basepack/field_config.rb
netzke-basepack-0.9.0 lib/netzke/basepack/field_config.rb
netzke-basepack-0.10.0.rc2 lib/netzke/basepack/field_config.rb
netzke-basepack-0.9.0.rc1 lib/netzke/basepack/field_config.rb
netzke-basepack-0.8.4 lib/netzke/basepack/field_config.rb
netzke-basepack-0.8.3 lib/netzke/basepack/field_config.rb
netzke-basepack-0.8.2 lib/netzke/basepack/field_config.rb