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

Add support for Rails 5.1+ AR Dirty changes #379

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/ancestry/has_ancestry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def has_ancestry options = {}
scope :siblings_of, lambda { |object| where(sibling_conditions(object)) }
scope :ordered_by_ancestry, Proc.new { |order|
if %w(mysql mysql2 sqlite sqlite3 postgresql).include?(connection.adapter_name.downcase) && ActiveRecord::VERSION::MAJOR >= 5
reorder("coalesce(#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)}, '')", order)
reorder(Arel.sql("coalesce(#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)}, '')"), order)
else
reorder("(CASE WHEN #{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)} IS NULL THEN 0 ELSE 1 END), #{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)}", order)
end
Expand Down Expand Up @@ -86,7 +86,7 @@ def has_ancestry options = {}
after_destroy :touch_ancestors_callback

if ActiveRecord::VERSION::STRING >= '5.1.0'
after_save :touch_ancestors_callback, if: :saved_changes?
after_save :touch_ancestors_callback, if: :has_changes_to_save?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saved_changes? and has_changes_to_save? have different semantics. saved_changes? is true if there were any changes when an instance was saved last time. has_changes_to_save? (which is an alias to changed?) is true if there are unsaved changes.

With this change touch_ancestors_callback is never called, two more tests fail. Tested with AR 5.2.0.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@khustochka thanks for the insight. It sounds like save_changes? is the one we till want to use

else
after_save :touch_ancestors_callback, if: :changed?
end
Expand Down
12 changes: 10 additions & 2 deletions lib/ancestry/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ 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?

if self.send("#{self.ancestry_base_class.ancestry_column}_was").blank? then id.to_s else "#{self.send "#{self.ancestry_base_class.ancestry_column}_was"}/#{id}" end
if ActiveRecord::VERSION::STRING >= '5.1.0'
if self.send("#{self.ancestry_base_class.ancestry_column}_in_database").blank? then id.to_s else "#{self.send "#{self.ancestry_base_class.ancestry_column}_in_database"}/#{id}" end
else
if self.send("#{self.ancestry_base_class.ancestry_column}_was").blank? then id.to_s else "#{self.send "#{self.ancestry_base_class.ancestry_column}_was"}/#{id}" end
end
end

# Ancestors
Expand All @@ -90,7 +94,11 @@ def ancestors?
alias :has_parent? :ancestors?

def ancestry_changed?
changed.include?(self.ancestry_base_class.ancestry_column.to_s)
if ActiveRecord::VERSION::STRING >= '5.1.0'
changed_attribute_names_to_save.include?(self.ancestry_base_class.ancestry_column.to_s)
else
changed.include?(self.ancestry_base_class.ancestry_column.to_s)
end
end

def parse_ancestry_column obj
Expand Down