Module: Traits::DescendantsListing

Included in:
Traits
Defined in:
lib/traits/descendants_listing.rb

Instance Method Summary collapse

Instance Method Details

#active_record_descendants(filter = true) ⇒ Object



24
25
26
27
28
29
# File 'lib/traits/descendants_listing.rb', line 24

def active_record_descendants(filter = true)
  load_active_record_descendants!
  ary = ActiveRecord::Base.descendants
  ary.reject! { |ar| filter_active_record_descendant(ar) } if filter
  ary
end

#active_record_descendants_loaded?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/traits/descendants_listing.rb', line 6

def active_record_descendants_loaded?
  !!@active_record_descendants_loaded
end

#filter_active_record_descendant(active_record) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/traits/descendants_listing.rb', line 31

def filter_active_record_descendant(active_record)
  active_record_filters.any? do |filter|
    case filter
      when Regexp then active_record.name =~ filter
      when String then active_record.name == filter
      when Class  then active_record == filter
      else false
    end
  end
end

#invalidate_loaded_active_record_descendants!Object



20
21
22
# File 'lib/traits/descendants_listing.rb', line 20

def invalidate_loaded_active_record_descendants!
  @active_record_descendants_loaded = false
end

#load_active_record_descendants!Object



10
11
12
13
14
15
16
17
18
# File 'lib/traits/descendants_listing.rb', line 10

def load_active_record_descendants!
  @active_record_descendants_loaded ||= begin
    # Railties are not necessary here
    # Source: http://stackoverflow.com/questions/6497834/differences-between-railties-and-engines-in-ruby-on-rails-3
    Rails::Engine.subclasses.map(&:instance).each { |i| i.eager_load! }
    Rails.application.eager_load!
    true
  end
end