Skip to content

Commit

Permalink
Merge pull request #17360 from NickLaMuro/ancestry_patches
Browse files Browse the repository at this point in the history
Ancestry Patches
(cherry picked from commit 095f4dc)

https://bugzilla.redhat.com/show_bug.cgi?id=1593798
  • Loading branch information
kbrock authored and simaishi committed Jun 21, 2018
1 parent ec917a8 commit f92ee1f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/orchestration_stack.rb
@@ -1,4 +1,6 @@
require 'ancestry'
require 'ancestry_patch'

class OrchestrationStack < ApplicationRecord
require_nested :Status

Expand Down
1 change: 1 addition & 0 deletions app/models/relationship.rb
@@ -1,4 +1,5 @@
require 'ancestry'
require 'ancestry_patch'

class Relationship < ApplicationRecord
has_ancestry
Expand Down
1 change: 1 addition & 0 deletions app/models/service.rb
@@ -1,4 +1,5 @@
require 'ancestry'
require 'ancestry_patch'

class Service < ApplicationRecord
DEFAULT_PROCESS_DELAY_BETWEEN_GROUPS = 120
Expand Down
1 change: 1 addition & 0 deletions app/models/tenant.rb
@@ -1,4 +1,5 @@
require 'ancestry'
require 'ancestry_patch'

class Tenant < ApplicationRecord
HARDCODED_LOGO = "custom_logo.png"
Expand Down
70 changes: 70 additions & 0 deletions lib/patches/ancestry_patch.rb
@@ -0,0 +1,70 @@
module AncestryInstanceMethodsPatch
def update_descendants_with_new_ancestry
super
unless ancestry_callbacks_disabled?
clear_memoized_instance_variables
if ancestry_changed? && !new_record? && sane_ancestry?
unscoped_descendants.each(&:clear_memoized_instance_variables)
end
end
end
end

module Ancestry
module InstanceMethods
prepend AncestryInstanceMethodsPatch

ANCESTRY_DELIMITER = '/'.freeze

def parse_ancestry_column(obj)
obj.to_s.split(ANCESTRY_DELIMITER).map! { |id| cast_primary_key(id) }
end

def ancestor_ids
@_ancestor_ids ||= parse_ancestry_column(read_attribute(ancestry_base_class.ancestry_column))
end

def parent_id
return @_parent_id if defined?(@_parent_id)
@_parent_id = if @_ancestor_ids
@_ancestor_ids.empty? ? nil : @_ancestor_ids.last
else
col = read_attribute(ancestry_base_class.ancestry_column)
# Specifically not using `.blank?` here because it is
# slower than doing the below.
if col.nil? || col.empty? # rubocop:disable Rails/Blank
nil
else
rindex = col.rindex(ANCESTRY_DELIMITER)
cast_primary_key(rindex ? col[rindex + 1, col.length] : col)
end
end
end

def depth
@_depth ||= if @_ancestor_ids
@_ancestor_ids.size
else
col = read_attribute(ancestry_base_class.ancestry_column)
col ? col.count(ANCESTRY_DELIMITER) + 1 : 0
end
end

STRING_BASED_KEYS = %i[string uuid text].freeze
def cast_primary_key(key)
if STRING_BASED_KEYS.include?(primary_key_type)
key
else
key.to_i
end
end

def clear_memoized_instance_variables
@_ancestor_ids = nil
@_depth = nil

# can't assign to `nil` since `nil` could be a valid result
remove_instance_variable(:@_parent_id) if defined?(@_parent_id)
end
end
end

0 comments on commit f92ee1f

Please sign in to comment.