Module: ActiveRecord::ModelSpaces::Util

Included in:
Context, ModelSpace, Persistor, Registry, TableManager, TableNames
Defined in:
lib/active_record/model_spaces/util.rb

Class Method Summary collapse

Class Method Details

.all_model_superclasses(klass) ⇒ Object

returns all model superclasses upto but not including ActiveRecord::Base



42
43
44
# File 'lib/active_record/model_spaces/util.rb', line 42

def all_model_superclasses(klass)
  superclasses = klass.ancestors.grep(Class).sort.take_while{|k| k < ActiveRecord::Base}
end

.class_for_classname(classname) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/active_record/model_spaces/util.rb', line 29

def class_for_classname(classname)
  begin
    model_from_name(classname)
  rescue
    false
  end
end

.class_from_classname(classname) ⇒ Object



37
38
39
# File 'lib/active_record/model_spaces/util.rb', line 37

def class_from_classname(classname)
  class_for_classname(classname) || require_for_classname(classname)
end

.is_active_record_model?(klass) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/active_record/model_spaces/util.rb', line 46

def is_active_record_model?(klass)
  superclasses = klass.ancestors.grep(Class).sort.take_while{|k| k <= ActiveRecord::Base}
  superclasses.length > 1 &&
    superclasses.include?(ActiveRecord::Base)
end

.model_from_name(key) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/active_record/model_spaces/util.rb', line 12

def model_from_name(key)
  if key.is_a? String
    Kernel.eval(key)
  else
    key
  end
end

.name_from_model(model) ⇒ Object



8
9
10
# File 'lib/active_record/model_spaces/util.rb', line 8

def name_from_model(model)
  model.to_s
end

.require_for_classname(classname) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/active_record/model_spaces/util.rb', line 20

def require_for_classname(classname)
  begin
    Kernel.require ActiveSupport::Inflector.underscore(classname)
    Kernel.eval(classname)
  rescue Exception=>e
    false
  end
end