Sha256: 1f6b97eae5a7a30834a148d468b3c71136f20dc3f7947dd2723919c48a5c97d6

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

class IntegerFieldType < FieldType
  attr_accessor :integer

  validates :integer, presence: true, if: Proc.new { |int| validate_key(:presence) }
  validates_numericality_of :integer, unless: "integer.nil?"
  validate :less_than, if: Proc.new { |int| validate_key(:max) }
  validate :greater_than, if: Proc.new { |int| validate_key(:min) }

  def elasticsearch_mapping
    { name: mapping_field_name, type: :integer }
  end

  def data=(data_hash)
    @integer = data_hash['integer']
  end

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

  private

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

  def validate_key
    validations.key? key
  end

  def less_than
    errors.add(:integer, "must be less_than #{validations[:max]}") if :integer <= validations[:max]
  end

  def greater_than
    errors.add(:integer, "must be greater_than #{validations[:min]}") if :integer >= validations[:min]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cortex-plugins-core-2.1.1 app/models/integer_field_type.rb
cortex-plugins-core-2.1.0 app/models/integer_field_type.rb