Module: Models::Lineage::ClassMethods

Defined in:
app/concerns/models/lineage.rb

Overview

ActiveSupport::Concern mixin: class methods.

Belongs to collapse

Has many collapse

Instance Method Summary collapse

Instance Method Details

#acts_as_lineage(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/concerns/models/lineage.rb', line 16

def acts_as_lineage(options = {})
  configuration = {
    foreign_key: :parent_id,
    order: nil,
    counter_cache: nil,
    dependent: :destroy
  }

  configuration.update(options) if options.is_a?(Hash)

  belongs_to :parent,
             class_name: name,
             foreign_key: configuration[:foreign_key],
             counter_cache: configuration[:counter_cache],
             inverse_of: :children,
             optional: true

  has_many :children,
           -> { order(configuration[:order]) },
           class_name: name,
           foreign_key: configuration[:foreign_key],
           dependent: configuration[:dependent],
           inverse_of: :parent

  # NOTE: scopes moved to class_eval to use configured foreign_key
  class_eval %{

    def self.roots
      order_option = "#{configuration.dig(:order)}"
      r = where(#{configuration[:foreign_key]}: nil)
      r = r.order(order_option) if order_option.present?
      r
    end

    def self.root
      self.roots.first
    end

    scope :parents_only, -> { where(#{configuration[:foreign_key]}: nil) }
    scope :children_only, -> { where.not(#{configuration[:foreign_key]}: nil) }
    scope :descendants_lin, ->(pid) { where(id: self_and_descendants_ids(pid) ) }
    scope :ancestors_lin, ->(pid) { where(id: self_and_ancestors_ids(pid)) }
    scope :descendants_only, ->(pid) { where(id: descendants_ids(pid)) }
  }, __FILE__, __LINE__ - 18
end

#ancestors_ids(*ids) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/concerns/models/lineage.rb', line 81

def ancestors_ids(*ids)
  ids = [ids].flatten.filter_map(&:presence).uniq

  return [] if ids.blank?

  foreign_key = reflect_on_all_associations.find { |a| a.name == :children }.foreign_key
  sql = Arel.sql("with recursive t(level,#{foreign_key},id) as (select 0,#{foreign_key},id from #{table_name} where id IN (#{ids.join(',')}) union select t.level+1,c.#{foreign_key},c.id from #{table_name} c join t on c.id = t.#{foreign_key}) select id from t where id not in (#{ids.join(',')});")
  res = ActiveRecord::Base.lease_connection.execute(sql)
  begin
    res.map { |r| r['id'].to_i }.filter_map(&:presence).uniq
  rescue StandardError
    []
  end
end

#childrenActiveRecord::Relation<Child>

Returns:

  • (ActiveRecord::Relation<Child>)

See Also:



33
34
35
36
37
38
# File 'app/concerns/models/lineage.rb', line 33

has_many :children,
-> { order(configuration[:order]) },
class_name: name,
foreign_key: configuration[:foreign_key],
dependent: configuration[:dependent],
inverse_of: :parent

#descendants_ids(*ids) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/concerns/models/lineage.rb', line 62

def descendants_ids(*ids)
  ids = [ids].flatten.filter_map(&:presence).uniq

  return [] if ids.blank?

  foreign_key = reflect_on_all_associations.find { |a| a.name == :children }.foreign_key
  sql = Arel.sql("with recursive t(level,#{foreign_key},id) as (select 0,#{foreign_key},id from #{table_name} where #{foreign_key} IN (#{ids.join(',')}) union select t.level+1,c.#{foreign_key},c.id from #{table_name} c join t on c.#{foreign_key} = t.id) select distinct id from t order by id;")
  res = ActiveRecord::Base.lease_connection.execute(sql)
  begin
    res.map { |r| r['id'].to_i }.filter_map(&:presence).uniq
  rescue StandardError
    []
  end
end

#generate_order_by(*ids) ⇒ Object



119
120
121
# File 'app/concerns/models/lineage.rb', line 119

def generate_order_by(*ids)
  Arel.sql([ids].flatten.map(&:presence).uniq.compact.map { |i| "#{table_name}.id = #{i} desc" }.join(','))
end

#parentParent

Returns:

  • (Parent)

See Also:



26
27
28
29
30
31
# File 'app/concerns/models/lineage.rb', line 26

belongs_to :parent,
class_name: name,
foreign_key: configuration[:foreign_key],
counter_cache: configuration[:counter_cache],
inverse_of: :children,
optional: true

#root_ids(*ids) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/concerns/models/lineage.rb', line 96

def root_ids(*ids)
  ids = ids.flatten.filter_map(&:presence).uniq

  return [] if ids.blank?

  foreign_key = reflect_on_all_associations.find { |a| a.name == :children }.foreign_key
  sql = Arel.sql("with recursive t(level,parent_id,id) as (select 0,#{foreign_key},id from #{table_name} where id IN (#{ids.join(',')}) union select t.level + 1,c.#{foreign_key},c.id from #{table_name} c join t on c.id = t.parent_id) select id from t where t.parent_id IS NULL;")
  res = ActiveRecord::Base.lease_connection.execute(sql)
  begin
    res.map { |r| r['id'].to_i }.filter_map(&:presence).uniq
  rescue StandardError
    []
  end
end

#self_ancestors_and_descendants_ids(*ids) ⇒ Object



115
116
117
# File 'app/concerns/models/lineage.rb', line 115

def self_ancestors_and_descendants_ids(*ids)
  (ancestors_ids(ids) + ids + descendants_ids(ids)).flatten.filter_map(&:presence).uniq
end

#self_and_ancestors_ids(*ids) ⇒ Object



111
112
113
# File 'app/concerns/models/lineage.rb', line 111

def self_and_ancestors_ids(*ids)
  (ancestors_ids(ids) + ids).flatten.filter_map(&:presence).uniq
end

#self_and_descendants_ids(*ids) ⇒ Object



77
78
79
# File 'app/concerns/models/lineage.rb', line 77

def self_and_descendants_ids(*ids)
  (ids + descendants_ids(ids)).flatten.filter_map(&:presence).uniq
end