Module: Traits::DescendantsListing

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

Instance Method Summary collapse

Instance Method Details

#active_record_descendantsObject



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

def active_record_descendants
  @ar_descendants ||= begin
    load_active_record_descendants!
    ActiveRecord::Base.descendants.reject! { |ar| excluded_active_record_descendant?(ar) }
  end
end

#active_record_descendants_loaded?Boolean

Returns:

  • (Boolean)


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

def active_record_descendants_loaded?
  !!@ar_descendants_loaded
end

#excluded_active_record_descendant?(model_class) ⇒ Boolean

Returns:

  • (Boolean)


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

def excluded_active_record_descendant?(model_class)
  rules_for_excluding_active_records.any? do |rule|
    case rule
      when Regexp then model_class.name =~ rule
      when String then model_class.name == rule
      when Class  then model_class == rule
      else false
    end
  end
end

#invalidate_loaded_active_record_descendants!Object



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

def invalidate_loaded_active_record_descendants!
  @ar_descendants        = nil
  @ar_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!
  @ar_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