Class: PushType::RepeaterField

Inherits:
FieldType show all
Defined in:
app/fields/push_type/repeater_field.rb

Instance Attribute Summary

Attributes inherited from FieldType

#model, #name

Instance Method Summary collapse

Methods inherited from FieldType

#css_class, #field_options, #form_helper, #html_options, #json_primitive, #json_value, #kind, #label, #multiple?, on_class, on_instance, options, #primitive

Constructor Details

#initialize(*args) ⇒ RepeaterField

Returns a new instance of RepeaterField.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
# File 'app/fields/push_type/repeater_field.rb', line 7

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

Instance Method Details

#rowsObject



26
27
28
29
30
# File 'app/fields/push_type/repeater_field.rb', line 26

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

#structureObject



32
33
34
# File 'app/fields/push_type/repeater_field.rb', line 32

def structure
  @structure ||= structure_class.new
end

#templateObject



22
23
24
# File 'app/fields/push_type/repeater_field.rb', line 22

def template
  'repeater'
end

#valueObject



17
18
19
20
# File 'app/fields/push_type/repeater_field.rb', line 17

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