Sha256: 9c1d96931897985ccaa28de7f9e801b0976ff035486b24b8cda02d528d801f63

Contents?: true

Size: 1.89 KB

Versions: 15

Compression:

Stored size: 1.89 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"
        # DEBT: why is self.format not working?? ('too few arguments')
        self[:format] ||= I18n.t("date", scope: 'netzke.formats', default: "Y-m-d")
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
netzke-basepack-0.12.9 lib/netzke/basepack/field_config.rb
netzke-basepack-0.12.8 lib/netzke/basepack/field_config.rb
netzke-basepack-0.12.7 lib/netzke/basepack/field_config.rb
netzke-basepack-0.12.6 lib/netzke/basepack/field_config.rb
netzke-basepack-0.12.5 lib/netzke/basepack/field_config.rb
netzke-basepack-0.12.4 lib/netzke/basepack/field_config.rb
netzke-basepack-0.12.3 lib/netzke/basepack/field_config.rb
netzke-basepack-0.12.2 lib/netzke/basepack/field_config.rb
netzke-basepack-0.12.1 lib/netzke/basepack/field_config.rb
netzke-basepack-0.11.2 lib/netzke/basepack/field_config.rb
netzke-basepack-0.11.1 lib/netzke/basepack/field_config.rb
netzke-basepack-0.12.0 lib/netzke/basepack/field_config.rb
netzke-basepack-0.12.0.rc1 lib/netzke/basepack/field_config.rb
netzke-basepack-0.12.0.beta lib/netzke/basepack/field_config.rb
netzke-basepack-0.11.0 lib/netzke/basepack/field_config.rb