-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Marks object as dirty even if nothing was changed in RichTextTranslation fields #597
Comments
Found another important aspect, after the update the object has a different ID than the one it had before saving the record: Before #<Mobility::Backends::ActionText::RichTextTranslation:0x00000001192de460
id: nil,
name: "description",
body: nil,
record_type: "Song",
record_id: 4,
created_at: nil,
updated_at: nil,
locale: "en"> After #<Mobility::Backends::ActionText::RichTextTranslation:0x000000011a3e6168
id: nil,
name: "description",
body: nil,
record_type: "Song",
record_id: 4,
created_at: nil,
updated_at: nil,
locale: "en"> Could it be that this is what ends up adding the value to the |
I have also opened an issue at I noticed that comparing the previous I'm trying to build some logic to filter out attributes that use |
For now, I've solved it like this, sharing in case it helps anyone in the future: def record_changed?(previous_record, current_record, params)
record_changes = current_record.previous_changes
mobility_action_text = "Mobility::Backends::ActionText::RichTextTranslation"
# Loop through each array of changes:
record_changes&.each do |key, value|
# Previous value is used to compare the class name but it can't be compared with changed_value because they're both storing updated values.
previous_value = value[0]
changed_value = value[1]
# Check if the items for each changes array are part of Mobility Action Text
if previous_value&.class&.name == mobility_action_text
# Compares a duplicate of the original object because previous_value and changed value cannot be trusted.
# Removes key entirely if the value is still the same:
if previous_record&.public_send(key)&.to_plain_text == current_record&.public_send(key)&.to_plain_text
record_changes.delete(key)
end
end
end
# If both content and description were not changed, then remove update from the hash.
if record_changes.size == 1 && record_changes["updated_at"]&.present?
record_changes.delete("updated_at")
end
# Whether or not the record has changed.
return record_changes.empty? ? false : true
end Please feel free to close if this is out of scope of the library, thanks a lot! 🙏 |
Thanks for the details! Glad you found a fix. For reference, Mobility doesn't support compatibility with other gems, that's something you need to figure out yourself (which it seems like you have). |
Thanks @shioyama! It's reassuring to read that extending is a good way of doing things. That's correct, I have adapted it for now and it seems to work fine. 🙏 |
Context
Hi all, I am not sure here but I think I might have found an issue with Mobility's
dirty
option and usingaction_text
fields.I have a model which includes two text areas with Trix. Content and Description. When submitting the edit form without changing any values the
@song.saved_changes
after@song.update
shows thatMobility::Backends::ActionText::RichTextTranslation
were saved even if the contents did not change.Is this expected behaviour or it shouldn't happen? I assume those fields shouldn't save either since they haven't changed?
The mobility plugin configuration:
The songs controller action:
The output in the terminal is the following when submitting the form without changing values, it only includes rich text fields for some reason:
If the form is submitted and the title property is changed, it does include it (this works as it should):
Expected behaviour
Enabling the
dirty
plugin and running@song.saved_changes
should be empty if none of the values were changed.For example, when no changes are made to the form
saved_changes
would be{}
and when a change is made it would be:Actual behaviour
RichText fields are added
saved_changes
even if they're not modified in the form.Possible Fix
It might be that I'm missing something but that's the behaviour I'm getting that happens when a field is set to use the action text backend at the moment.
I have a repository here where this could be replicated, sorry I could not get it deployed: https://github.com/csalmeida/rails-mobility
Creating a song and then editing it to check for the changed attribute should replicate the results I'm having.
I'm thinking that maybe because it's markup coming from Trix it might get modified in the client for some reason perhaps?
I have the following model using just Mobility's field definitions and submit a form without changing any fields I get:
Model definition:
Inspecting after
@song.update
it works as expected no values changed and thereforeprevious_changes
is empty:When submitting the form again but this time with a value changed it reflects that change in
previous_changes
, so far so good!Now I'm introducing a Mobility Action Text field, and submitting the form without changing any values:
The input now becomes:
If the title is changed that change is added to the changes array as well:
Could I be missing something here? It could well be that I'm assuming that previous changes should be empty in this particular case but maybe that's the intended behaviour.
Thank you very much, I'm curious to read what you think. 🙏
The text was updated successfully, but these errors were encountered: