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
-
.all_submodels(klass) ⇒ Array of Classes
!! See the built in self.descendants for actual inheritance tracking, this is path based.
-
.alternate_value_attributes(object) ⇒ Array
A list symbols that represent populated, non “cached”, non “_id”, non reserved attributes.
-
.annotatable_attributes(object) ⇒ Array of Symbols
!! Some models have blacklists (e.g. Serial).
-
.community_data_classes ⇒ Array
All superclass models that are community/shared.
-
.data_models ⇒ Array
All superclass data models.
-
.klass_reflections(klass, relationship_type = :has_many) ⇒ Object
Array of AR associations to access the related class use `.klass`.
-
.model_from_file_name(file_name) ⇒ Class
e.g.
- .nested_subclasses(parent = self) ⇒ Hash
-
.project_data_classes ⇒ Array of Classes
All models with a project_id attribute.
-
.superclass_models ⇒ Array
All models that inherit directly from ApplicationRecord.
Class Method Details
.all_submodels(klass) ⇒ Array of Classes
!! See the built in self.descendants for actual inheritance tracking, this is path based. Used in Ranks.
61 62 63 |
# File 'lib/application_enumeration.rb', line 61 def self.all_submodels(klass) Dir.glob(Rails.root + "app/models/#{klass.name.underscore}/**/*.rb").collect{|a| self.model_from_file_name(a) } end |
.alternate_value_attributes(object) ⇒ Array
Returns a list symbols that represent populated, non “cached”, non “_id”, non reserved attributes.
16 17 18 19 20 21 22 |
# File 'lib/application_enumeration.rb', line 16 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 |
.annotatable_attributes(object) ⇒ Array of Symbols
!! Some models have blacklists (e.g. Serial)
28 29 30 |
# File 'lib/application_enumeration.rb', line 28 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 |
.community_data_classes ⇒ Array
Returns all superclass models that are community/shared.
46 47 48 |
# File 'lib/application_enumeration.rb', line 46 def self.community_data_classes superclass_models.select{|a| a < Shared::SharedAcrossProjects } end |
.data_models ⇒ Array
Returns all superclass data models.
52 53 54 |
# File 'lib/application_enumeration.rb', line 52 def self.data_models superclass_models.select{|a| a < Shared::IsData} end |
.klass_reflections(klass, relationship_type = :has_many) ⇒ Object
Returns Array of AR associations to access the related class use `.klass`.
83 84 85 86 |
# File 'lib/application_enumeration.rb', line 83 def self.klass_reflections(klass, relationship_type = :has_many) a = klass.reflect_on_all_associations(relationship_type).sort{ |a, b| a.name <=> b.name } a end |
.model_from_file_name(file_name) ⇒ Class
e.g. given 'app/models/specimen.rb' the Specimen class is returned
69 70 71 |
# File 'lib/application_enumeration.rb', line 69 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 |
.nested_subclasses(parent = self) ⇒ Hash
75 76 77 78 79 |
# File 'lib/application_enumeration.rb', line 75 def self.nested_subclasses(parent = self) parent.subclasses.inject({}) { | hsh, subclass | hsh.merge!(subclass.name => nested_subclasses(subclass)) } end |
.project_data_classes ⇒ Array of Classes
Returns all models with a project_id attribute.
40 41 42 |
# File 'lib/application_enumeration.rb', line 40 def self.project_data_classes superclass_models.select{|a| a.column_names.include?('project_id') } end |
.superclass_models ⇒ Array
Returns all models that inherit directly from ApplicationRecord.
34 35 36 |
# File 'lib/application_enumeration.rb', line 34 def self.superclass_models ApplicationRecord.descendants.select{|a| a.superclass == ApplicationRecord } end |