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.

Note the use of Module.nesting (urbanautomaton.com/blog/2013/08/27/rails-autoloading-hell/)

Class Method Summary (collapse)

Class Method Details

+ (Object) all_submodels(klass)

!! See the built in self.descendants for actual inheritance tracking, this is path based. return [Array of Classes]

all models in the /app/models/#{klass.name} (not necessarily inheriting)

Used in Ranks.



34
35
36
# File 'lib/application_enumeration.rb', line 34

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

+ (Object) alternate_value_attributes(object)

return [Array]

a list symbols that represent of populated, non "cached", non "_id", non reserved attributes


15
16
17
18
19
20
21
# File 'lib/application_enumeration.rb', line 15

def self.alternate_value_attributes(object)
  if object.class::ALTERNATE_VALUES_FOR.blank?
    raise("#{object.class} attempted to annotate a class without ALTERNATE_VALUES_FOR -  please inform the programmers")
  else
    object.attributes.select{|k,v| !v.blank? && object.class::ALTERNATE_VALUES_FOR.include?(k.to_sym)}.keys.map(&:to_sym)
  end
end

+ (Object) annotatable_attributes(object)

return [Array of Symbols]

a whitelist of the attributes of a given instance that may be annotated

!! Some models have blacklists (e.g. Serial)



26
27
28
# File 'lib/application_enumeration.rb', line 26

def self.annotatable_attributes(object)
  object.attributes.select{|k,v| !v.blank? && !(k =~ /.*_id\z|cached_*.*/)}.keys.map(&:to_sym) - ( RESERVED_ATTRIBUTES - [:parent_id])  
end

+ (Object) community_data_classes



51
52
53
# File 'lib/application_enumeration.rb', line 51

def self.community_data_classes
  ActiveRecord::Base.descendants.select{|a|  a.superclass == ActiveRecord::Base && (a < Shared::SharedAcrossProjects) }
end

+ (Object) model_from_file_name(file_name)

return [Class]

represented by a path included filename from /app/models.

e.g. given 'app/models/specimen.rb' the Specimen class is returned



41
42
43
# File 'lib/application_enumeration.rb', line 41

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

+ (Object) project_data_classes

return [Array of Classes]

the classes in TaxonWorks that are project based/have a project_id


47
48
49
# File 'lib/application_enumeration.rb', line 47

def self.project_data_classes
  ActiveRecord::Base.descendants.select{|a| a.superclass == ActiveRecord::Base &&  a.column_names.include?('project_id') }
end