lib/hierarchable/hierarchable.rb in hierarchable-0.3.0 vs lib/hierarchable/hierarchable.rb in hierarchable-0.3.1

- old
+ new

@@ -394,10 +394,11 @@ models = [] models_to_analyze = [self.class] until models_to_analyze.empty? klass = models_to_analyze.pop + next unless klass next if models.include?(klass) obj = klass.new next unless obj.respond_to?(:hierarchy_descendant_associations) @@ -512,20 +513,20 @@ # The most common case is if we want to specify additional associations. # This will take all of the associations that can be auto-detected and # also add in the one provided. # # class A - # include Hierarched + # include Hierarchable # hierarched parent_source: :parent, # additional_descendant_associations: [:some_association] # end # # There may also be a case when we want exact control over what associations # that should be used. In that case, we can specify it like this: # # class A - # include Hierarched + # include Hierarchable # hierarched parent_source: :parent, # descendant_associations: [:some_association] # end def hierarchy_descendant_associations if hierarchable_config[:descendant_associations].present? @@ -680,15 +681,19 @@ # current objects's value for the ID corresponding to the # `hierarchy_parent_source` has been updated. def hierarchy_parent_changed? # FIXME: We need to figure out how to deal with updating the # object_hierarchy_ancestry_path, object_hierarchy_full_path, etc., - if hierarchy_parent_source.present? - public_send("#{hierarchy_parent_source}_id_changed?") - else - false - end + return true unless persisted? + + source = hierarchy_parent_source + return false if source.blank? + + changed_method = "#{source}_id_changed?" + public_send(changed_method) if respond_to?(changed_method) + + send(source).id == hierarchy_parent_id end # Update the hierarchy_ancestors_path if the hierarchy has changed. def update_dirty_hierarchy_ancestors_path set_hierarchy_ancestors_path @@ -697,9 +702,8 @@ # Get the class that is associated with a given association def class_for_association(association) self.association(association) .reflection - .class_name - .safe_constantize + .klass end end