Hi all, I am not 100% sure but I think I might have found an issue with Mobility's dirty
option and using actions_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 that Mobility::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:
Mobility.configure do
# PLUGINS
plugins do
# Backend
#
# Sets the default backend to use in models. This can be overridden in models
# by passing +backend: ...+ to +translates+.
#
# To default to a different backend globally, replace +:key_value+ by another
# backend name.
#
backend :key_value
# Dirty
#
# Uncomment this line to include and enable globally:
dirty
end
The songs controller action:
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
if @song.update(song_params)
puts @song.saved_changes.inspect # Mobility::Backends::ActionText::RichTextTranslation objects present.
redirect_to songs_path
else
render :edit
end
end
The output in the terminal is the following when submitting the form without changing values, it only includes rich text fields for some reason:
{"updated_at"=>
[Fri, 09 Dec 2022 04:11:06.660466000 UTC +00:00, Fri, 09 Dec 2022 04:12:23.104814000 UTC +00:00],
"description_pt"=>
[#<Mobility::Backends::ActionText::RichTextTranslation:0x000000011331df30
id: nil,
name: "description",
body: nil,
record_type: "Song",
record_id: 4,
created_at: nil,
updated_at: nil,
locale: "pt">,
#<Mobility::Backends::ActionText::RichTextTranslation:0x000000011331df30
id: nil,
name: "description",
body: nil,
record_type: "Song",
record_id: 4,
created_at: nil,
updated_at: nil,
locale: "pt">],
"content_pt"=>
[#<Mobility::Backends::ActionText::RichTextTranslation:0x000000011331e250
id: 18,
name: "content",
body: #<ActionText::Content "<!-- BEGIN app/views/l...">,
record_type: "Song",
record_id: 4,
created_at: Fri, 09 Dec 2022 00:20:25.021014000 UTC +00:00,
updated_at: Fri, 09 Dec 2022 00:20:25.021014000 UTC +00:00,
locale: "pt">,
#<Mobility::Backends::ActionText::RichTextTranslation:0x000000011331e250
id: 18,
name: "content",
body: #<ActionText::Content "<!-- BEGIN app/views/l...">,
record_type: "Song",
record_id: 4,
created_at: Fri, 09 Dec 2022 00:20:25.021014000 UTC +00:00,
updated_at: Fri, 09 Dec 2022 00:20:25.021014000 UTC +00:00,
locale: "pt">],
"description_en"=>
[#<Mobility::Backends::ActionText::RichTextTranslation:0x00000001133960c0
id: nil,
name: "description",
body: nil,
record_type: "Song",
record_id: 4,
created_at: nil,
updated_at: nil,
locale: "en">,
#<Mobility::Backends::ActionText::RichTextTranslation:0x00000001133960c0
id: nil,
name: "description",
body: nil,
record_type: "Song",
record_id: 4,
updated_at: nil,
locale: "en">],
"content_en"=>
[#<Mobility::Backends::ActionText::RichTextTranslation:0x000000011331e4f8
id: 19,
name: "content",
body: #<ActionText::Content "<!-- BEGIN app/views/l...">,
record_type: "Song",
record_id: 4,
created_at: Fri, 09 Dec 2022 00:20:25.022686000 UTC +00:00,
updated_at: Fri, 09 Dec 2022 00:20:25.022686000 UTC +00:00,
locale: "en">,
#<Mobility::Backends::ActionText::RichTextTranslation:0x000000011331e4f8
id: 19,
name: "content",
body: #<ActionText::Content "<!-- BEGIN app/views/l...">,
record_type: "Song",
record_id: 4,
created_at: Fri, 09 Dec 2022 00:20:25.022686000 UTC +00:00,
updated_at: Fri, 09 Dec 2022 00:20:25.022686000 UTC +00:00,
locale: "en">]}
If the form is submitted and the title property is changed, it does include it (this works as it should):
{"updated_at"=>
[Fri, 09 Dec 2022 04:25:58.481859000 UTC +00:00, Fri, 09 Dec 2022 04:26:29.673314000 UTC +00:00],
"description_pt"=>
[#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000116a9dd48
id: nil,
name: "description",
body: nil,
record_type: "Song",
record_id: 4,
created_at: nil,
updated_at: nil,
locale: "pt">,
#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000116a9dd48
id: nil,
name: "description",
body: nil,
record_type: "Song",
record_id: 4,
created_at: nil,
updated_at: nil,
locale: "pt">],
"content_pt"=>
[#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000116a9e040
id: 18,
name: "content",
body: #<ActionText::Content "<!-- BEGIN app/views/l...">,
record_type: "Song",
record_id: 4,
created_at: Fri, 09 Dec 2022 00:20:25.021014000 UTC +00:00,
updated_at: Fri, 09 Dec 2022 00:20:25.021014000 UTC +00:00,
locale: "pt">,
#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000116a9e040
id: 18,
name: "content",
body: #<ActionText::Content "<!-- BEGIN app/views/l...">,
record_type: "Song",
record_id: 4,
created_at: Fri, 09 Dec 2022 00:20:25.021014000 UTC +00:00,
updated_at: Fri, 09 Dec 2022 00:20:25.021014000 UTC +00:00,
locale: "pt">],
"title_en"=>["Butter got spilt", "Butter got spilt edited"],
"description_en"=>
[#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000116ade4b0
id: nil,
name: "description",
body: nil,
record_type: "Song",
record_id: 4,
created_at: nil,
updated_at: nil,
locale: "en">,
#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000116ade4b0
id: nil,
name: "description",
body: nil,
record_type: "Song",
record_id: 4,
created_at: nil,
updated_at: nil,
locale: "en">],
"content_en"=>
[#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000116a9e2e8
id: 19,
name: "content",
body: #<ActionText::Content "<!-- BEGIN app/views/l...">,
record_type: "Song",
record_id: 4,
created_at: Fri, 09 Dec 2022 00:20:25.022686000 UTC +00:00,
updated_at: Fri, 09 Dec 2022 00:20:25.022686000 UTC +00:00,
locale: "en">,
#<Mobility::Backends::ActionText::RichTextTranslation:0x0000000116a9e2e8
id: 19,
name: "content",
body: #<ActionText::Content "<!-- BEGIN app/views/l...">,
record_type: "Song",
record_id: 4,
created_at: Fri, 09 Dec 2022 00:20:25.022686000 UTC +00:00,
updated_at: Fri, 09 Dec 2022 00:20:25.022686000 UTC +00:00,
locale: "en">]}
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:
{
"title_en"=>["Butter got spilt", "Butter got spilt edited"]
}
Actual behaviour
RichText fields are added saved_changes
even if they're not modified in the form.