Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly_extensions/monkey_patches.rb

Class Method Summary collapse

Class Method Details

.all_polymorphic_types(name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 12

def self.all_polymorphic_types(name)
  @poly_hash ||= {}.tap do |hash|
    Dir.glob(File.join(RAILS_ROOT, "app", "models", "**", "*.rb")).each do |file|
      klass = (File.basename(file, ".rb").camelize.constantize rescue nil)
      next if klass.nil? or !klass.ancestors.include?(ActiveRecord::Base)
      klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection|
        (hash[reflection.options[:as]] ||= []) << klass
      end
    end
  end
  @poly_hash[name.to_sym]
end

.randomObject



6
7
8
9
10
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 6

def self.random
  if (c = count) != 0
    find(:first, :offset =>rand(c))
  end
end