-
Notifications
You must be signed in to change notification settings - Fork 8
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 #37
Comments
Hi @csalmeida, thanks for the report! As this gem does not explicitly touch the Dirty plugin, could you please check if you can reproduce the issue when not using |
Hi @sedubois, thank you for getting back to me! I see, the reason I opened there is that this only happens to rich text objects but it could be better placed in Mobility. 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 a the moment. I have a repository here where this could be replicated, sorry I could not get it deployed but I have invited you to it and it's public: 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: class Song < ApplicationRecord
attr_accessor :language
extend Mobility
translates :title, type: :string, locale_accessors: [:en, :pt]
translates :description, type: :text, locale_accessors: [:en, :pt]
translates :content, type: :text, locale_accessors: [:en, :pt]
end Inspecting after (ruby) @song.previous_changes
{} When submitting the form again but this time with a value changed it reflects that change in (ruby) @song.previous_changes
{"updated_at"=>[Sat, 10 Dec 2022 16:09:21.395697000 UTC +00:00, Sat, 10 Dec 2022 16:16:55.869677000 UTC +00:00], "title_en"=>["The wood of Maçaranduba", "The wood of Maçaranduba 2"]} Now I'm introducing a Mobility Action Text field, and submitting the form without changing any values: class Song < ApplicationRecord
attr_accessor :language
extend Mobility
translates :title, type: :string, locale_accessors: [:en, :pt]
translates :description, type: :text, locale_accessors: [:en, :pt]
translates :content, backend: :action_text, locale_accessors: [:en, :pt]
end The input now becomes: (Duration: 0.7ms | Allocations: 1072)
{"updated_at"=>[Sat, 10 Dec 2022 16:19:40.699785000 UTC +00:00, Sat, 10 Dec 2022 16:20:33.864672000 UTC +00:00],
"content_en"=>
[#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000113f44bd8
id: 2,
name: "content",
body: #<ActionText::Content "<div class=\"trix-conte...">,
record_type: "Song",
record_id: 1,
created_at: Sun, 03 Apr 2022 22:25:54.450739000 UTC +00:00,
updated_at: Sun, 03 Apr 2022 23:27:54.407177000 UTC +00:00,
locale: "en">,
#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000113f44bd8
id: 2,
name: "content",
body: #<ActionText::Content "<div class=\"trix-conte...">,
record_type: "Song",
record_id: 1,
created_at: Sun, 03 Apr 2022 22:25:54.450739000 UTC +00:00,
updated_at: Sun, 03 Apr 2022 23:27:54.407177000 UTC +00:00,
locale: "en">],
"content_pt"=>
[#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000113f44a20
id: 3,
name: "content",
body: #<ActionText::Content "<div class=\"trix-conte...">,
record_type: "Song",
record_id: 1,
created_at: Sun, 03 Apr 2022 23:20:59.179068000 UTC +00:00,
updated_at: Sun, 03 Apr 2022 23:20:59.179068000 UTC +00:00,
locale: "pt">,
#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000113f44a20
id: 3,
name: "content",
body: #<ActionText::Content "<div class=\"trix-conte...">,
record_type: "Song",
record_id: 1,
created_at: Sun, 03 Apr 2022 23:20:59.179068000 UTC +00:00,
updated_at: Sun, 03 Apr 2022 23:20:59.179068000 UTC +00:00,
locale: "pt">]} If the title is changed that change is added to the changes array as well: {"updated_at"=>[Sat, 10 Dec 2022 16:16:55.871974000 UTC +00:00, Sat, 10 Dec 2022 16:19:40.697910000 UTC +00:00],
"title_en"=>["The wood of Maçaranduba 2", "The wood of Maçaranduba"],
"content_en"=>
[#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000113d97128
id: 2,
name: "content",
body: #<ActionText::Content "<div class=\"trix-conte...">,
record_type: "Song",
record_id: 1,
created_at: Sun, 03 Apr 2022 22:25:54.450739000 UTC +00:00,
updated_at: Sun, 03 Apr 2022 23:27:54.407177000 UTC +00:00,
locale: "en">,
#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000113d97128
id: 2,
name: "content",
body: #<ActionText::Content "<div class=\"trix-conte...">,
record_type: "Song",
record_id: 1,
created_at: Sun, 03 Apr 2022 22:25:54.450739000 UTC +00:00,
updated_at: Sun, 03 Apr 2022 23:27:54.407177000 UTC +00:00,
locale: "en">],
"content_pt"=>
[#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000113d96f20
id: 3,
name: "content",
body: #<ActionText::Content "<div class=\"trix-conte...">,
record_type: "Song",
record_id: 1,
created_at: Sun, 03 Apr 2022 23:20:59.179068000 UTC +00:00,
updated_at: Sun, 03 Apr 2022 23:20:59.179068000 UTC +00:00,
locale: "pt">,
#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000113d96f20
id: 3,
name: "content",
body: #<ActionText::Content "<div class=\"trix-conte...">,
record_type: "Song",
record_id: 1,
created_at: Sun, 03 Apr 2022 23:20:59.179068000 UTC +00:00,
updated_at: Sun, 03 Apr 2022 23:20:59.179068000 UTC +00:00,
locale: "pt">]} 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 intended behaviour. Thank you very much, I'm curious to read what you think. 🙏 |
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 |
In the meantime, I think I found a way to get past this, perhaps it's not the best solution but it works: # app/controllers/application_controller.rb
# This method was created because Mobility Action Text objects are added incorrectly to changed objects.
# At the moment only Rails and Mobility attributes track the dirty property correctly.
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
# An array of parameters with nested values
return record_changes.empty? ? false : true
end Using the method in a controller action: # app/controllers/songs_controller.rb
def update
@song = Song.find_by(id: params[:id])
# Allows all tags are removed on submission.
# Otherwise it will the last one.
if params.dig(:song, :tag_ids).nil?
params[:song][:tag_ids] = []
end
duplicate_record = @song.dup
if @song.update(song_params)
log_contribution(@song) if record_changed?(duplicate_record, @song, song_params) # Save a contribution for this user if record changed.
redirect_to songs_path
else
render :edit
end
end |
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! 🙏 |
@csalmeida thank you for all your investigations. Unfortunately I have no time to dig in myself, but by all means if you find something that needs fixing please feel free to submit a PR. Also I would encourage trying to narrow down which part of the code is causing the issue, i.e. whether this should be solved in this gem or in Mobility itself. If it relates to Mobility code then @shioyama might offer advice. |
No worries at all I completely understand it might be difficult to find time to look into it. 🙏 It's an interesting problem and would be happy to look more into it as soon as I have a spare minute, I'll keep you both posted if I get to investigate further. |
Hi folks, sorry missed this. I really can't comment on this gem. If you find a problem with the dirty plugin on Mobility (without actiontext) then that's something I could maybe help, but then you'd need an isolated test to reproduce the issue. |
Hi all, I am not 100% sure but I think I might have found an issue with Mobility's
dirty
option and usingactions_text
.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 we're 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.The text was updated successfully, but these errors were encountered: