Sha256: 9a0b32ed1e44663ac66ff9a12cc2913ec7649104d8c3ae42897c0ffcc515695d

Contents?: true

Size: 870 Bytes

Versions: 19

Compression:

Stored size: 870 Bytes

Contents

class DateTimeFieldType < FieldType
  attr_accessor :timestamp

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

  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

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

  private

  def mapping_field_name
    "#{field_name.parameterize('_')}_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

19 entries across 19 versions & 1 rubygems

Version Path
cortex-plugins-core-0.12.3 app/models/date_time_field_type.rb
cortex-plugins-core-0.12.2 app/models/date_time_field_type.rb
cortex-plugins-core-0.12.1 app/models/date_time_field_type.rb
cortex-plugins-core-0.12.0 app/models/date_time_field_type.rb
cortex-plugins-core-0.11.3 app/models/date_time_field_type.rb
cortex-plugins-core-0.11.2 app/models/date_time_field_type.rb
cortex-plugins-core-0.11.1 app/models/date_time_field_type.rb
cortex-plugins-core-0.11.0 app/models/date_time_field_type.rb
cortex-plugins-core-0.10.4 app/models/date_time_field_type.rb
cortex-plugins-core-0.10.2 app/models/date_time_field_type.rb
cortex-plugins-core-0.10.1 app/models/date_time_field_type.rb
cortex-plugins-core-0.10.0 app/models/date_time_field_type.rb
cortex-plugins-core-0.9.1 app/models/date_time_field_type.rb
cortex-plugins-core-0.9.0 app/models/date_time_field_type.rb
cortex-plugins-core-0.8.0 app/models/date_time_field_type.rb
cortex-plugins-core-0.7.2 app/models/date_time_field_type.rb
cortex-plugins-core-0.7.1 app/models/date_time_field_type.rb
cortex-plugins-core-0.7.0 app/models/date_time_field_type.rb
cortex-plugins-core-0.6.0 app/models/date_time_field_type.rb