Sha256: 5c89172e983cfd53123919ad45742711a6a9ccb964718c8861b0e1b24c9a34b4

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 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 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

3 entries across 3 versions & 1 rubygems

Version Path
cortex-plugins-core-0.7.1 app/models/text_field_type.rb
cortex-plugins-core-0.7.0 app/models/text_field_type.rb
cortex-plugins-core-0.6.0 app/models/text_field_type.rb