Class: GeoLabels::Label

Inherits:
ApplicationRecord show all
Defined in:
app/models/geo_labels/label.rb

Constant Summary collapse

RANSACKABLE_ATTRIBUTES =
%w[name].freeze

Constants inherited from ApplicationRecord

ApplicationRecord::RANSACKABLE_ASSOCIATIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes

Class Method Details

.descendants_for_ids(ids) ⇒ Object



15
16
17
18
19
20
21
22
# File 'app/models/geo_labels/label.rb', line 15

def self.descendants_for_ids(ids)
  records = find(ids)
  descendants_scope = records.shift.self_and_descendants
  records.each do |record|
    descendants_scope = descendants_scope.or(record.self_and_descendants)
  end
  descendants_scope
end

.each_h_with_level(&blk) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/geo_labels/label.rb', line 24

def self.each_h_with_level(&blk)
  returner = proc do |label, level|
    blk.call(label, level)
    label[:children].try :each do |child|
      returner.call(child, level + 1)
    end
  end
  Exporter.labels_tree_h.each do |label| # {name: 'Food', id: 3, children: [{name: 'Italian'...}]}
    returner.call(label, 0)
  end
end

Instance Method Details

#associated_contactsObject



36
37
38
39
# File 'app/models/geo_labels/label.rb', line 36

def associated_contacts
  ids = ContactLabel.joins(:label).merge(self_and_descendants).pluck(:contact_id).uniq
  Contact.order(:name).find(ids)
end