TrestleAdmin / trestle

A modern, responsive admin framework for Ruby on Rails
https://trestle.io
GNU Lesser General Public License v3.0
1.97k stars 177 forks source link

Array, json bad serialisation #199

Open gesaleh opened 5 years ago

gesaleh commented 5 years ago

I'm having an issue i have 2 Fields as json, one is array sensors ["x","y","e"] and another is array or arrays (polygone) area [[a,b],[c,d]] trestle don't handle these value when i do edit and apply first array is good but i have null value added ["", "x"," y"," e"] second one is converted to string and it is not anymore array

spohlenz commented 5 years ago

What field types are you using for these (I'm guessing multiple selects)?

You may need to add in some model logic on the writer methods (e.g. def sensors=(sensors)) to reject blank values, and to parse the values as JSON, since Rails passes those parameters in as strings.

gesaleh commented 5 years ago

Thank you for the reply, where you define this I'm new to both rails and trestle , both parameters are defined in my postgres as json ,

spohlenz commented 5 years ago

By field types, I mean which form fields are you currently using (or want to use) on the front end? Is it a select (i.e. a dropdown) or a plain text_area/text_field (where you're manually inputting the JSON source)?

gesaleh commented 5 years ago

right now it is text_area for sensors it became dropdown , but for area it didn't I would prefer keeping it simple text_area

gesaleh commented 5 years ago
Screenshot 2019-05-15 at 08 32 21

image from what i have now

spohlenz commented 5 years ago

Check out this comment (as well as the others in that thread): https://github.com/TrestleAdmin/trestle/issues/18#issuecomment-322739568

If you define that module and include in your ApplicationRecord class, it may get you at least some of the way there.

gesaleh commented 5 years ago

thanks it is confusing since if i update in postgres using console values i have a good dispaly in table (almost) but not in edit

Screenshot 2019-05-15 at 09 08 52
gesaleh commented 5 years ago

I did some progress as you advised on area value so now it is handled by model I noticed that i don't have anymore empty values in sensors but i can't add new values although i see values are being posted

Parameters: {"utf8"=>"✓", "authenticity_token"=>"B5D479/3bcDcW7dpFyloIJj3nQNpdyEfhlnxkuZvnvxLr+qAHXENvnzsZU27RTOiANCeP5ywSxwO60RJizSjcw==", "role"=>{"name"=>"test default", "api_key"=>"xxxxxxxxxxxxx", "country"=>"test", "lng"=>"34.7818", "lat"=>"32.0853", "zoom"=>"9.0", "created_at"=>"2019-05-15 08:12", "updated_at"=>"2019-05-15 10:45", "sensors"=>["", "H2", "CO", "NO2", "temperature", "pressure", "aqi", "H3S", "H2S", "H4S"], "default_sensor"=>"CO", "area"=>"34.86827134019347 32.183232701632946 34.87777340044991 32.0201294673291 34.73524249669731 31.998925871027126 34.811258978701886 32.19508338688222"}, "button"=>"", "id"=>"1"}

initial values are "sensors"=>["H2", "CO", "NO2", "temperature", "pressure", "aqi"] it stays at this value

sensors are array that can be modified I don't have a predefined list containing all possible values.

spohlenz commented 5 years ago

I've just pushed some improvements to the master branch regarding the default handling of JSON attributes. Could you please give that a try and let me know if that works better for you?

gesaleh commented 5 years ago

yes now i see my value of Array of arrays better thank this is great so i need to create a new adapter to catch this value to json not to string right

gesaleh commented 5 years ago

done using jsonserialize it is working only issue i have now is how to update the sensors array (do i need to create a list with all possible sensors ?

gesaleh commented 5 years ago

i need to correct my sensors is defined as text array and i get multiple select in trestle but if i try to modify it or create a new role i can't update sensors value

spohlenz commented 5 years ago

When using a multiple select you'll have to define a custom writer method, since the values will be coming in as an array of strings rather than a single string.

Something like this might do the job (untested), but you will of course need to customize it to suit your expected data format:

def sensors=(value)
  case value
  when String
    super(JSON.parse(value))
  when Array
    super(value.map { |v| v.is_a?(String) ? JSON.parse(v) : v })
  else
    super
  end
end