Module: Namespaces::Traversal::Linear

Extended by:
ActiveSupport::Concern
Includes:
LinearScopes
Included in:
Namespace
Defined in:
app/models/namespaces/traversal/linear.rb

Constant Summary collapse

UnboundedSearch =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#all_project_idsObject



114
115
116
# File 'app/models/namespaces/traversal/linear.rb', line 114

def all_project_ids
  all_projects.select(:id)
end

#ancestor_ids(hierarchy_order: nil) ⇒ Object



150
151
152
153
154
# File 'app/models/namespaces/traversal/linear.rb', line 150

def ancestor_ids(hierarchy_order: nil)
  return super unless use_traversal_ids?

  hierarchy_order == :desc ? traversal_ids[0..-2] : traversal_ids[0..-2].reverse
end

#ancestors(hierarchy_order: nil, skope: self.class) ⇒ Object



142
143
144
145
146
147
148
# File 'app/models/namespaces/traversal/linear.rb', line 142

def ancestors(hierarchy_order: nil, skope: self.class)
  return super unless use_traversal_ids?

  return skope.none if parent_id.blank?

  lineage(bottom: parent, hierarchy_order: hierarchy_order, skope: skope)
end

#ancestors_upto(top = nil, hierarchy_order: nil) ⇒ Object

Returns all ancestors up to but excluding the top. When no top is given, all ancestors are returned. When top is not found, returns all ancestors.

This copies the behavior of the recursive method. We will deprecate this behavior soon.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/models/namespaces/traversal/linear.rb', line 162

def ancestors_upto(top = nil, hierarchy_order: nil)
  return super unless use_traversal_ids?

  # We can't use a default value in the method definition above because
  # we need to preserve those specific parameters for super.
  hierarchy_order ||= :desc

  top_index = ancestors_upto_top_index(top)
  ids = traversal_ids[top_index...-1].reverse

  # WITH ORDINALITY lets us order the result to match traversal_ids order.
  ids_string = ids.map { |id| Integer(id) }.join(',')
  from_sql = "    unnest(ARRAY[\#{ids_string}]::bigint[]) WITH ORDINALITY AS ancestors(id, ord)\n    INNER JOIN namespaces ON namespaces.id = ancestors.id\n  SQL\n\n  self.class\n    .from(Arel.sql(from_sql))\n    .order('ancestors.ord': hierarchy_order)\nend\n"

#descendantsObject



130
131
132
133
134
# File 'app/models/namespaces/traversal/linear.rb', line 130

def descendants
  return super unless use_traversal_ids?

  self_and_descendants.id_not_in(id)
end

#parent=(obj) ⇒ Object



198
199
200
201
# File 'app/models/namespaces/traversal/linear.rb', line 198

def parent=(obj)
  super(obj)
  set_traversal_ids
end

#parent_id=(id) ⇒ Object



203
204
205
206
# File 'app/models/namespaces/traversal/linear.rb', line 203

def parent_id=(id)
  super(id)
  set_traversal_ids
end

#root_ancestorObject

Return the top most ancestor of this namespace. This method aims to minimize the number of queries by trying to re-use data that has already been loaded.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/namespaces/traversal/linear.rb', line 96

def root_ancestor
  strong_memoize(:root_ancestor) do
    if parent_loaded_and_present?
      parent.root_ancestor
    elsif parent_id_present_and_traversal_ids_empty?
      # Parent is in the database, so find our root ancestor using our parent's traversal_ids.
      parent = Namespace.where(id: parent_id).select(:traversal_ids)
      Namespace.from("(#{parent.to_sql}) AS parent_namespace, namespaces")
               .find_by('namespaces.id = parent_namespace.traversal_ids[1]')
    elsif parent_id.nil?
      # There is no parent, so we are the root ancestor.
      self
    else
      Namespace.find_by(id: traversal_ids.first)
    end
  end
end

#self_and_ancestor_ids(hierarchy_order: nil) ⇒ Object



192
193
194
195
196
# File 'app/models/namespaces/traversal/linear.rb', line 192

def self_and_ancestor_ids(hierarchy_order: nil)
  return super unless use_traversal_ids?

  hierarchy_order == :desc ? traversal_ids : traversal_ids.reverse
end

#self_and_ancestors(hierarchy_order: nil, skope: self.class) ⇒ Object



184
185
186
187
188
189
190
# File 'app/models/namespaces/traversal/linear.rb', line 184

def self_and_ancestors(hierarchy_order: nil, skope: self.class)
  return super unless use_traversal_ids?

  return skope.where(id: id) if parent_id.blank?

  lineage(bottom: self, hierarchy_order: hierarchy_order, skope: skope)
end

#self_and_descendant_ids(skope: self.class) ⇒ Object



124
125
126
127
128
# File 'app/models/namespaces/traversal/linear.rb', line 124

def self_and_descendant_ids(skope: self.class)
  return super unless use_traversal_ids?

  self_and_descendants(skope: skope).as_ids
end

#self_and_descendants(skope: self.class) ⇒ Object



118
119
120
121
122
# File 'app/models/namespaces/traversal/linear.rb', line 118

def self_and_descendants(skope: self.class)
  return super unless use_traversal_ids?

  lineage(top: self, skope: skope)
end

#self_and_hierarchyObject



136
137
138
139
140
# File 'app/models/namespaces/traversal/linear.rb', line 136

def self_and_hierarchy
  return super unless use_traversal_ids?

  self_and_descendants.or(ancestors)
end

#traversal_path(with_organization: false) ⇒ Object



82
83
84
85
86
87
88
# File 'app/models/namespaces/traversal/linear.rb', line 82

def traversal_path(with_organization: false)
  ids = traversal_ids.clone

  ids.prepend(organization_id) if with_organization

  "#{ids.join('/')}/"
end

#use_traversal_ids?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'app/models/namespaces/traversal/linear.rb', line 90

def use_traversal_ids?
  traversal_ids.present?
end