Skip to content

Commit

Permalink
use before_last_save for rails >= 5.1
Browse files Browse the repository at this point in the history
The {attribute}_was has changed. there are now 2 different methods

changed the code according to whether it was called in the 
after save (needed to use _before_last_save) OR
before save/destroy callback (still use _was)

This will remove rails 5.1 deprecation warnings
  • Loading branch information
kbrock committed May 2, 2018
1 parent 9f226a4 commit ef25574
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions lib/ancestry/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def ancestry_exclude_self
errors.add(:base, "#{self.class.name.humanize} cannot be a descendant of itself.") if ancestor_ids.include? self.id
end

# Update descendants with new ancestry
# Update descendants with new ancestry (before save)
def update_descendants_with_new_ancestry
# If enabled and node is existing and ancestry was updated and the new ancestry is sane ...
if !ancestry_callbacks_disabled? && !new_record? && ancestry_changed? && sane_ancestry?
Expand All @@ -25,7 +25,7 @@ def update_descendants_with_new_ancestry
end
end

# Apply orphan strategy
# Apply orphan strategy (before destroy - no changes)
def apply_orphan_strategy
if !ancestry_callbacks_disabled? && !new_record?
case self.ancestry_base_class.orphan_strategy
Expand Down Expand Up @@ -61,7 +61,7 @@ def apply_orphan_strategy
end
end

# Touch each of this record's ancestors
# Touch each of this record's ancestors (after save)
def touch_ancestors_callback
if !ancestry_callbacks_disabled? && self.ancestry_base_class.touch_ancestors
# Touch each of the old *and* new ancestors
Expand All @@ -73,7 +73,7 @@ def touch_ancestors_callback
end
end

# The ancestry value for this record's children
# The ancestry value for this record's children (not in after_save_callbacks)
def child_ancestry
# New records cannot have children
raise Ancestry::AncestryException.new('No child ancestry for new record. Save record before performing tree operations.') if new_record?
Expand Down Expand Up @@ -105,14 +105,26 @@ def ancestors depth_options = {}
self.ancestry_base_class.scope_depth(depth_options, depth).ordered_by_ancestry.where ancestor_conditions
end

# deprecate
def ancestor_was_conditions
{primary_key_with_table => ancestor_ids_was}
{primary_key_with_table => ancestor_ids_before_last_save}
end

# deprecated - probably don't want to use anymore
def ancestor_ids_was
parse_ancestry_column(send("#{self.ancestry_base_class.ancestry_column}_was"))
end

if ActiveRecord::VERSION::STRING >= '5.1.0'
def ancestor_ids_before_last_save
parse_ancestry_column(send("#{self.ancestry_base_class.ancestry_column}_before_last_save"))
end
else
def ancestor_ids_before_last_save
parse_ancestry_column(send("#{self.ancestry_base_class.ancestry_column}_was"))
end
end

def path_ids
ancestor_ids + [id]
end
Expand All @@ -139,6 +151,7 @@ def ancestor_of?(node)

# Parent

# currently parent= does not work in after save callbacks
def parent= parent
write_attribute(self.ancestry_base_class.ancestry_column, if parent.nil? then nil else parent.child_ancestry end)
end
Expand Down Expand Up @@ -296,9 +309,10 @@ def unscoped_descendants
end
end

# works with after save context (hence before_last_save)
def unscoped_current_and_previous_ancestors
unscoped_where do |scope|
scope.where id: (ancestor_ids + ancestor_ids_was).uniq
scope.where id: (ancestor_ids + ancestor_ids_before_last_save).uniq
end
end

Expand Down

0 comments on commit ef25574

Please sign in to comment.