Sha256: 4b42ef626f9ac3a3ce495fd206b3e37080ca615321592d3a3e14ccd890f0ee2d

Contents?: true

Size: 904 Bytes

Versions: 3

Compression:

Stored size: 904 Bytes

Contents

class DateTimeFieldType < Cortex::FieldType
  attr_accessor :timestamp

  validates :timestamp, presence: true, if: :validate_presence?
  validate :timestamp_is_valid?, if: :validate_presence?

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

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

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

  private

  def mapping_field_name
    "#{field_name.parameterize(separator: '_')}_date_time"
  end

  def timestamp_is_valid?
    begin
      DateTime.parse(@timestamp)
      true
    rescue ArgumentError
      errors.add(:timestamp, 'must be a valid date')
      false
    end
  end

  def validate_presence?
    validations.key? :presence
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cortex-plugins-core-3.2.0 app/models/date_time_field_type.rb
cortex-plugins-core-3.1.0 app/models/date_time_field_type.rb
cortex-plugins-core-3.0.0 app/models/date_time_field_type.rb