Module: Volt::ModelHelpers::ClassMethods

Defined in:
lib/volt/models/model_helpers.rb

Instance Method Summary collapse

Instance Method Details

#class_at_path(path) ⇒ Object

Gets the class for a model at the specified path.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/volt/models/model_helpers.rb', line 26

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

      klass_name = path[index].singularize.camelize

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

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

  klass
end