Module: ApplicationEnumeration

Defined in:
lib/application_enumeration.rb

Overview

Methods for enumerating models, tables, columns etc. !! If you think that a method belongs here chances are it already exists in a Rails extension.

Class Method Summary collapse

Class Method Details

.all_submodels(klass) ⇒ Object

!! See the built in self.descendants for actual inheritance tracking, this is path based. Return all models in the /app/models/#klassklass.name (not necessarily inheriting) as an Array of Classes. Used in Ranks.



9
10
11
# File 'lib/application_enumeration.rb', line 9

def self.all_submodels(klass)
  Dir.glob(Rails.root + "app/models/#{klass.name.underscore}/**/*.rb").collect{|a| self.model_from_file_name(a) }
end

.model_from_file_name(file_name) ⇒ Object

Return the Class represented by a path included filename from /app/models. e.g. given 'app/models/specimen.rb' the Specimen class is returned



15
16
17
# File 'lib/application_enumeration.rb', line 15

def self.model_from_file_name(file_name)
  file_name.split(/app\/models\//).last[0..-4].split(/\\/).collect{|b| b.camelize}.join("::").safe_constantize
end