Class: Partisan::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/partisan/helper.rb

Class Method Summary collapse

Class Method Details

.parent_class_name(obj) ⇒ Object

Retrieves the parent class name



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/partisan/helper.rb', line 4

def self.parent_class_name(obj)
  # If the object is an ActiveRecord record
  klass = obj.class if obj.class < ActiveRecord::Base

  # If the object respond to `object` and the value is an ActiveRecord record
  klass ||= obj.object.class if obj.respond_to?(:object) && obj.object.class < ActiveRecord::Base

  # In case we’re using STI, loop back until we find the top-level ActiveRecord model
  klass = klass.superclass while !klass.superclass.abstract_class && klass.superclass != ActiveRecord::Base

  klass.name
end