Sha256: d3474dc2ba12bf55f8a70530020f2a5639691e1d8d398095ac051118ffd3b4cd

Contents?: true

Size: 1.33 KB

Versions: 7

Compression:

Stored size: 1.33 KB

Contents

class TagFieldType < FieldType
  VALIDATION_TYPES = {
    presence: :valid_presence_validation?
  }.freeze

  attr_accessor :data, :tag_list, :field_name
  attr_reader :validations, :metadata

  validates :tag_list, presence: true, if: :validate_presence?

  def validations=(validations_hash)
    @validations = validations_hash.deep_symbolize_keys
  end

  def data=(data_hash)
    @tag_list = data_hash.deep_symbolize_keys[:tag_list]
    @tag_list.nil? ? nil : (@tag_list = @tag_list.split(","))
  end

  def metadata=(metadata_hash)
    @metadata = metadata_hash.deep_symbolize_keys
  end

  def acceptable_validations?
    valid_types? && valid_options?
  end

  def field_item_as_indexed_json_for_field_type(field_item, options = {})
    json = {}
    json[mapping_field_name] = field_item.data['tag']
    json
  end

  def mapping
    {name: mapping_field_name, type: :string, analyzer: :snowball}
  end

  private

  def mapping_field_name
    "#{field_name.parameterize('_')}_tag"
  end

  def valid_types?
    validations.all? do |type, options|
      VALIDATION_TYPES.include?(type)
    end
  end

  def valid_options?
    validations.all? do |type, options|
      self.send(VALIDATION_TYPES[type])
    end
  end

  def valid_presence_validation?
    @validations.key? :presence
  end

  def validate_presence?
    @validations.key? :presence
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
cortex-plugins-core-0.3.1 app/models/tag_field_type.rb
cortex-plugins-core-0.3.0 app/models/tag_field_type.rb
cortex-field_types-core-0.2.3 app/models/tag_field_type.rb
cortex-field_types-core-0.2.2 app/models/tag_field_type.rb
cortex-field_types-core-0.2.1 app/models/tag_field_type.rb
cortex-field_types-core-0.1.1 app/models/tag_field_type.rb
cortex-field_types-core-0.1.0 app/models/tag_field_type.rb