Giter Club home page Giter Club logo

Comments (5)

shioyama avatar shioyama commented on May 25, 2024 1

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

from mobility.

csalmeida avatar csalmeida commented on May 25, 2024

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?

from mobility.

csalmeida avatar csalmeida commented on May 25, 2024

I have also opened an issue at mobility-actiontext: sedubois/mobility-actiontext#37

I noticed that comparing the previous Mobility::Backends::ActionText body value with the new one can be used to determine if something is changed.

I'm trying to build some logic to filter out attributes that use Mobility::Backends::ActionText out of previous_changes if no changes were made to them but it might be a hacky way. I'll share it here if I get it working! 🙏

from mobility.

csalmeida avatar csalmeida commented on May 25, 2024

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

from mobility.

csalmeida avatar csalmeida commented on May 25, 2024

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

from mobility.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.