Sha256: 37e82f820b9ea3a80d5ef2b5e520b50112b780486f2012b4e7660047dd8282dd

Contents?: true

Size: 941 Bytes

Versions: 10

Compression:

Stored size: 941 Bytes

Contents

class TextFieldType < FieldType
  attr_accessor :text

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

  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 validate_presence?
    @validations.key? :presence
  end

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
cortex-plugins-core-0.5.0 app/models/text_field_type.rb
cortex-plugins-core-0.4.8 app/models/text_field_type.rb
cortex-plugins-core-0.4.7 app/models/text_field_type.rb
cortex-plugins-core-0.4.6 app/models/text_field_type.rb
cortex-plugins-core-0.4.5 app/models/text_field_type.rb
cortex-plugins-core-0.4.4 app/models/text_field_type.rb
cortex-plugins-core-0.4.3 app/models/text_field_type.rb
cortex-plugins-core-0.4.2 app/models/text_field_type.rb
cortex-plugins-core-0.4.1 app/models/text_field_type.rb
cortex-plugins-core-0.4.0 app/models/text_field_type.rb