Module: Volt::Models::Helpers::Base::ClassMethods

Defined in:
lib/volt/models/helpers/base.rb

Instance Method Summary collapse

Instance Method Details

#class_at_path(path) ⇒ Object

Gets the class for a model at the specified path.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/volt/models/helpers/base.rb', line 75

def class_at_path(path)
  if path
    begin
      # remove the _ and then singularize/pluralize
      if path.last == :[]
        index = -2
      else
        index = -1
      end

      # process_class_name is defined by Model/ArrayModel as
      # singularize/pluralize
      klass_name = process_class_name(klass_name = path[index]).camelize

      # Lookup the class
      klass = Object.const_get(klass_name)

      # Use it if it is a model
      klass = self unless klass < self
    rescue NameError => e
      # Ignore exception, just means the model isn't defined
      klass = self
    end
  else
    klass = self
  end

  klass
end