I am creating a Category model and using the awesome_nested_set plugin (a replacement for acts_as_nested_set) to deal with the hierarchy. With awesome_nested_set, objects are created, then saved, and then placed within the set. As well, lft, rgt and parent_id are attr_protected so they cannot be written to directly.
I am running into two situations when placing the node into the set that I want to be able to catch so that I notify the user (there might be more that I haven't thought of yet):
- A node is attempted to be placed as its own child (self.id == self.parent_id)
- A node is attempted to be moved beneath its own descendant (self.descendants.include? self.parent_id == true)
In both cases, the move will fail, but awesome_nested_set will only raise an ActiveRecord::ActiveRecordError exception, with a message that is not as descriptive as I'd like to be able to give the user.
awesome_nested_set has a number of node moving methods, which all call move_to(target, position) (where position is one of :root, :child, :left or :right and target is the related node for all positions but :root). The method fires a before_move callback, but doesn't provide a way that I can see to validate a move before it happens. In order to validate a move, I'd need access to the target and position, which the callback does not receive.
Does anyone know of either a way to validate a move in awesome_nested_set (either by having a way to pass target and position to the before_move callback of by another method), or another nested set plugin that will let me validate? I'd prefer not to fork or write my own plugin.