Sha256: 6f90dac17208d367eec2187406e7c494f56aab374e5125380a1f2f24f3760d0f

Contents?: true

Size: 1.19 KB

Versions: 5

Compression:

Stored size: 1.19 KB

Contents

module PushType
  class RepeaterField < FieldType

    options json_primitive: :array,
            repeats:        :string

    def initialize(*args)
      super
      raise ArgumentError, "Invalid field type. `#{ @opts[:repeats] }` cannot be used in #{ self.class.name }." if [:tag_list].include?(@opts[:repeats])
      _key, _opts = structure_json_key, @opts
      structure_class.class_eval do
        field _key, _opts[:repeats], _opts.except(:css_class, :repeats).merge(multiple: false)
        define_method(:f) { fields[_key] }
      end
    end

    def value
      return if json_value.blank?
      rows.map(&:_f)
    end

    def template
      'repeater'
    end

    def rows
      Array(json_value).map do |j|
        structure_class.new(field_store: { structure_json_key => j }.as_json)
      end
    end

    def structure
      @structure ||= structure_class.new
    end

    private

    def defaults
      super.except(:template, :form_helper)
    end

    def structure_class
      @structure_class ||= PushType::Structure.clone
    end

    def structure_json_key
      case @opts[:repeats]
        when :relation, :node, :taxonomy, :asset then :_f_id
        else :_f
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
push_type_core-0.7.0.beta.1 app/fields/push_type/repeater_field.rb
push_type_core-0.6.0 app/fields/push_type/repeater_field.rb
push_type_core-0.6.0.beta.4 app/fields/push_type/repeater_field.rb
push_type_core-0.6.0.beta.3 app/fields/push_type/repeater_field.rb
push_type_core-0.6.0.beta.2 app/fields/push_type/repeater_field.rb