Sha256: 214d89da7893df315afc39a30f602e24a3705b2c8fa61bbcffd3736c2d77c2a3

Contents?: true

Size: 1.27 KB

Versions: 12

Compression:

Stored size: 1.27 KB

Contents

class TextFieldType < FieldType
  attr_accessor :text
  jsonb_accessor :data, text: :string

  validates :text, presence: true, if: :validate_presence?
  validate :text_length, if: :validate_length?
  validate :text_unique, if: :validate_uniqueness?

  def data=(data_hash)
    @text = data_hash.deep_symbolize_keys[:text]
  end

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

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

  private

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

  def text_present
    errors.add(:text, 'must be present') if @text.empty?
  end

  def text_length
    validator = LengthValidator.new(validations[:length].merge(attributes: [:text]))
    validator.validate_each(self, :text, text)
  end

  def text_unique
    unless metadata[:existing_data][:text] == text || field.field_items.jsonb_contains(:data, text: text).empty?
      errors.add(:text, "#{field.name} Must be unique")
    end
  end

  def validate_uniqueness?
    @validations.key? :uniqueness
  end

  def validate_presence?
    @validations.key? :presence
  end

  def validate_length?
    @validations.key? :length
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
cortex-plugins-core-0.11.3 app/models/text_field_type.rb
cortex-plugins-core-0.11.2 app/models/text_field_type.rb
cortex-plugins-core-0.11.1 app/models/text_field_type.rb
cortex-plugins-core-0.11.0 app/models/text_field_type.rb
cortex-plugins-core-0.10.4 app/models/text_field_type.rb
cortex-plugins-core-0.10.2 app/models/text_field_type.rb
cortex-plugins-core-0.10.1 app/models/text_field_type.rb
cortex-plugins-core-0.10.0 app/models/text_field_type.rb
cortex-plugins-core-0.9.1 app/models/text_field_type.rb
cortex-plugins-core-0.9.0 app/models/text_field_type.rb
cortex-plugins-core-0.8.0 app/models/text_field_type.rb
cortex-plugins-core-0.7.2 app/models/text_field_type.rb