Module: Models::Lineage::ClassMethods

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

Belongs to collapse

Has many collapse

Instance Method Summary collapse

Instance Method Details

#acts_as_lineage(options = {}) ⇒ Object



14
15
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
# File 'app/concerns/models/lineage.rb', line 14

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



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

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

  return [] unless ids.present?

  foreign_key = reflect_on_all_associations.detect { |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.connection.execute(sql)
  begin
    res.map { |r| r['id'].to_i }.map(&:presence).compact.uniq
  rescue StandardError
    []
  end
end

#childrenActiveRecord::Relation<Child>

Returns:

  • (ActiveRecord::Relation<Child>)

See Also:



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

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

#descendants_ids(*ids) ⇒ Object



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

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

  return [] unless ids.present?

  foreign_key = reflect_on_all_associations.detect { |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.connection.execute(sql)
  begin
    res.map { |r| r['id'].to_i }.map(&:presence).compact.uniq
  rescue StandardError
    []
  end
end

#generate_order_by(*ids) ⇒ Object



117
118
119
# File 'app/concerns/models/lineage.rb', line 117

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:



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

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



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

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

  return [] unless ids.present?

  foreign_key = reflect_on_all_associations.detect { |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.connection.execute(sql)
  begin
    res.map { |r| r['id'].to_i }.map(&:presence).compact.uniq
  rescue StandardError
    []
  end
end

#self_ancestors_and_descendants_ids(*ids) ⇒ Object



113
114
115
# File 'app/concerns/models/lineage.rb', line 113

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

#self_and_ancestors_ids(*ids) ⇒ Object



109
110
111
# File 'app/concerns/models/lineage.rb', line 109

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

#self_and_descendants_ids(*ids) ⇒ Object



75
76
77
# File 'app/concerns/models/lineage.rb', line 75

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