Skip to content
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

Open
csalmeida opened this issue Dec 9, 2022 · 9 comments

Comments

@csalmeida
Copy link

csalmeida commented Dec 9, 2022

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.

@sedubois
Copy link
Owner

sedubois commented Dec 9, 2022

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 mobility-actiontextand only using the mobility gem? In that case it will make more sense to open the issue on the Mobility repo. I would also suggest reporting the version of the plugins you're using.

@csalmeida
Copy link
Author

csalmeida commented Dec 10, 2022

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 @song.update it works as expected no values changed and therefore previous_changes is empty:

(ruby) @song.previous_changes
{}

When submitting the form again but this time with a value changed it reflects that change in previous_changes, sof far so good!

(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. 🙏

@csalmeida
Copy link
Author

csalmeida commented Dec 10, 2022

Found another important aspect, after the update the object has a different ID than the one it had before saving the record:

Before @song.update:

#<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 @song.update:

#<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 @song.previous_changes hash?
It could also be related to the dirty implementation as it patches methods so they work with translated attributes but I need to investigate further to be sure.

@csalmeida
Copy link
Author

csalmeida commented Dec 13, 2022

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

@csalmeida
Copy link
Author

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! 🙏

@sedubois
Copy link
Owner

@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.

@csalmeida
Copy link
Author

@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.

@shioyama
Copy link

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.

@csalmeida
Copy link
Author

Thanks, @shioyama, this is an issue I would like to look into further, seems like an interesting problem.

Will create an application with the issue and try to suggest a solution as soon as I have a spare minute and share it here so you and @sedubois can take a look. Thanks again 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants