Skip to content

Using acts-as-dag with acts_as_list #26

@MarkMT

Description

@MarkMT

Unfortunately it seems this excellent gem is no longer being actively maintained, but I mention the following here for the benefit of anyone else who happens to use it and may run into the same problem...

If using acts-as-dag with acts_as_list, e.g. to allow sibling links to be ordered, it is important that in the link model definition acts_as_dag_links be called before acts_as_list.

Explanation -
acts_as_list adds a before_destroy callback to the link model that causes a link record to be reloaded from the database before it is destroyed:

before_destroy :reload, unless: Proc.new { new_record? || destroyed_via_scope? || act_as_list_no_update? }

However acts-as-dag also has a destroy callback:

before_destroy :destroyable!, :perpetuate

perpetuate uses rewire_crossing to obtain other links affected by the destruction of a direct link and to manipulate the count attribute of each one in memory in order to act as a flag to determine what subsequently happens to it in push_associated_modification!. In the case where the direct link is being destroyed, associated indirect links will also be destroyed by push_associated_modification!. However the before_destroy callback above then causes destroyable! to be called on that indirect link, which also checks the count attribute that was modified by perpetuate to ensure that the link is in fact destroyable?.

Here is where the problem occurs - if acts-as-list is called before acts_as_dag_links, the reload callback will fire before destroyable! and the unsaved modification that perpetuate made to the count attribute will be lost. The result is that destroyable? will fail and destroyable! will throw an exception.

The solution is simply to ensure that acts_as_dag_links is called before acts_as_list.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions