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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/volt/models/helpers/base.rb', line 75

def class_at_path(path)
  if path
    # 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

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

      # Use it if it is a model
      return (klass < self ? klass : (klass = self))
    rescue NameError => e
      # Ignore exception, just means the model isn't defined
      #
      return klass = self if klass_name.singular?
    end

    # Checl for special case where we are subclassing a Volt::Model that has a custom Volt::ArrayModel
    begin
      # Get the pluralised name of the superclass of the model
      super_klass_name = Object.const_get(klass_name.singularize).superclass.to_s.pluralize

      # Get the class, rescue if not found
      klass = Object.const_get(super_klass_name)

      klass = self unless klass < self
    rescue NameError => e
      # Ignore exception, array model isn't defined.
      return klass = self
    end

  else
    klass = self
  end

  klass
end