modernistik / parse-stack

Parse Server Ruby Client SDK
https://www.modernistik.com/gems/parse-stack/
MIT License
61 stars 21 forks source link

Float numbers are formatted to integer #73

Open elamonica opened 2 years ago

elamonica commented 2 years ago

The problem appears when you have a numeric column with float data. When you get the model, the attribute is formatted as integer instead of float.

mathieulb commented 1 year ago

What just happened to me is that when I do Parse.auto_generate_models!, and try to read a field whose type is Number, I get a value rounded downwards, e.g. 2.8 (Float) becomes 2 (ruby Integer). Furthermore, attempting to set it to Float 2.8, and then saving it, sets the db field to 2. Actually, even just doing set("stuff", 2.8) then get("stuff") returns 2.

Is that what happens to you ?

mathieulb commented 1 year ago

also :

MyClass.class_eval {property :my_field, :float, field: "My_Field"}

warns :

Property MyClass#my_field already defined with data type :float. Will be ignored.

when the actual type it's already defined with, is :integer ; generally it reports whatever type I write in the statement, instead of the existing one. See lib/parse/model/core/properties.rb :

if (self.fields[key].present? && BASE_FIELD_MAP[key].nil?) || (self < Parse::Object && BASE_FIELD_MAP.has_key?(key))
  warn "Property #{self}##{key} already defined with data type :#{data_type}. Will be ignored."
  return false
end

also wrong documentation for def attributes, copy-pasted from def enums :

# @return [Hash] the fields that are marked as enums.

I just created this workaround :

module Parse::Properties::ClassMethods
  def remove_property key, **opts
    key = key.to_sym
    parse_field = opts[:field].to_sym || key.to_s.camelize(:lower)
    fields.delete key
    fields.delete parse_field
    attributes.delete parse_field
    remove_method parse_field
    remove_method :"#{key}_increment!"
    remove_method :"#{key}_decrement!"
  end
end
class MyClass
  remove_property :my_field, field: :My_Field
  property :my_field, :float, field: :My_Field
end