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

+ (Array of Classes) all_submodels(klass)

!! See the built in self.descendants for actual inheritance tracking, this is path based. Used in Ranks.

Parameters:

  • klass (Object)

Returns:

  • (Array of Classes)

    all models in the /app/models/#klassklass.name (not necessarily inheriting)



59
60
61
# File 'lib/application_enumeration.rb', line 59

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

+ (Array) alternate_value_attributes(object)

Returns a list symbols that represent populated, non “cached”, non “_id”, non reserved attributes

Returns:

  • (Array)

    a list symbols that represent populated, non “cached”, non “_id”, non reserved attributes



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

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

+ (Array of Symbols) annotatable_attributes(object)

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

Parameters:

  • object (Object)

Returns:

  • (Array of Symbols)

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



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

+ (Array) community_data_classes

Returns all superclass models that are community/shared

Returns:

  • (Array)

    all superclass models that are community/shared



44
45
46
# File 'lib/application_enumeration.rb', line 44

def self.community_data_classes
  superclass_models.select{|a| a < Shared::SharedAcrossProjects }
end

+ (Array) data_models

Returns all superclass data models

Returns:

  • (Array)

    all superclass data models



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

def self.data_models
  superclass_models.select{|a| a < Shared::IsData}
end

+ (Class) model_from_file_name(file_name)

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

Parameters:

  • file_name (String)

Returns:

  • (Class)

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



67
68
69
# File 'lib/application_enumeration.rb', line 67

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

+ (Hash) nested_subclasses(parent = self)

Parameters:

  • parent (Object) (defaults to: self)

Returns:

  • (Hash)


73
74
75
76
77
# File 'lib/application_enumeration.rb', line 73

def self.nested_subclasses(parent = self)
  parent.subclasses.inject({}) { | hsh, subclass |
    hsh.merge!(subclass.name => nested_subclasses(subclass))
  }
end

+ (Array of Classes) project_data_classes

Returns all models with a project_id attribute

Returns:

  • (Array of Classes)

    all models with a project_id attribute



38
39
40
# File 'lib/application_enumeration.rb', line 38

def self.project_data_classes
  superclass_models.select{|a| a.column_names.include?('project_id') }
end

+ (Array) superclass_models

Returns all models that inherit directly from ApplicationRecord

Returns:

  • (Array)

    all models that inherit directly from ApplicationRecord



32
33
34
# File 'lib/application_enumeration.rb', line 32

def self.superclass_models
  ApplicationRecord.descendants.select{|a| a.superclass == ApplicationRecord }
end