Sha256: 0a0b0164a6a9a11debde350a131dee5f8499d0c74d0d35f3539c33e88f483cb8

Contents?: true

Size: 698 Bytes

Versions: 2

Compression:

Stored size: 698 Bytes

Contents

module PushType
  class MatrixField < ArrayField

    def param
      { name.to_sym => mapping.keys }
    end
    
    def template
      @opts[:template] || :matrix
    end

    def mapping
      @mapping ||= (@opts[:mapping] || []).reduce({}) do |h, col|
        col.is_a?(Hash) ? h.merge(col) : h.update(col => form_helper)
      end
    end

    def to_json(val)
      return if val.blank?
      super.reject { |v| v.blank? or v.values.all?(&:blank?) }
    end

    def from_json(val)
      return if val.blank?
      super.reject { |v| v.blank? or v.values.all?(&:blank?) }.map { |h| struct.new(*h.values) }
    end

    def struct
      @struct ||= Struct.new *mapping.keys
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
push_type_core-0.5.0.alpha.2 app/fields/push_type/matrix_field.rb
push_type_core-0.5.0.alpha.1 app/fields/push_type/matrix_field.rb