Class: PeakFlowUtils::ModelInspector
- Inherits:
-
Object
- Object
- PeakFlowUtils::ModelInspector
- Defined in:
- app/services/peak_flow_utils/model_inspector.rb
Instance Attribute Summary collapse
-
#clazz ⇒ Object
readonly
Returns the value of attribute clazz.
Class Method Summary collapse
- .engines ⇒ Object
- .find_subclasses(clazz) ⇒ Object
-
.load_models! ⇒ Object
Preloads all models for Rails app and all engines (if they aren’t loaded, then they cant be inspected).
-
.load_models_for(root) ⇒ Object
Loads models for the given app-directory (Rails-root or engine).
-
.model_classes ⇒ Object
Yields a model-inspector for each model found in the application.
Instance Method Summary collapse
- #attribute_key(attribute_name) ⇒ Object
- #attributes ⇒ Object
- #class_key ⇒ Object
- #class_key_one ⇒ Object
- #class_key_other ⇒ Object
- #globalize_attributes ⇒ Object
-
#initialize(clazz) ⇒ ModelInspector
constructor
A new instance of ModelInspector.
- #inspect ⇒ Object
- #money_attributes ⇒ Object
- #paperclip_attachments ⇒ Object
-
#relationships ⇒ Object
TODO: Maybe this should yield a ModelInspector::Relationship instead?.
- #snake_name ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(clazz) ⇒ ModelInspector
Returns a new instance of ModelInspector.
24 25 26 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 24 def initialize(clazz) @clazz = clazz end |
Instance Attribute Details
#clazz ⇒ Object (readonly)
Returns the value of attribute clazz.
2 3 4 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 2 def clazz @clazz end |
Class Method Details
.engines ⇒ Object
118 119 120 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 118 def self.engines ::Rails::Engine.subclasses.map(&:instance) end |
.find_subclasses(clazz) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 95 def self.find_subclasses(clazz, &) return if @scanned[clazz.name] @scanned[clazz.name] = true clazz.subclasses.each do |subclass| yield ::PeakFlowUtils::ModelInspector.new(subclass) find_subclasses(subclass, &) end end |
.load_models! ⇒ Object
Preloads all models for Rails app and all engines (if they aren’t loaded, then they cant be inspected).
107 108 109 110 111 112 113 114 115 116 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 107 def self.load_models! return if PeakFlowUtils::ModelInspector.models_loaded PeakFlowUtils::ModelInspector.models_loaded = true load_models_for(Rails.root) engines.each do |engine| load_models_for(engine.root) end end |
.load_models_for(root) ⇒ Object
Loads models for the given app-directory (Rails-root or engine).
123 124 125 126 127 128 129 130 131 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 123 def self.load_models_for(root) Dir.glob("#{root}/app/models/**/*.rb").each do |model_path| require model_path rescue StandardError => e warn "Could not load model in #{model_path}" warn e.inspect warn e.backtrace end end |
.model_classes ⇒ Object
Yields a model-inspector for each model found in the application.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 7 def self.model_classes # Make sure all models are loaded. load_models! @scanned = {} @yielded = {} @skip = ["ActiveRecord::SchemaMigration"] ArrayEnumerator.new do |yielder| find_subclasses(ActiveRecord::Base) do |model_inspector| next if !model_inspector.clazz.name || @skip.include?(model_inspector.clazz.name) yielder << model_inspector end end end |
Instance Method Details
#attribute_key(attribute_name) ⇒ Object
83 84 85 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 83 def attribute_key(attribute_name) "activerecord.attributes.#{snake_name}.#{attribute_name}" end |
#attributes ⇒ Object
28 29 30 31 32 33 34 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 28 def attributes ArrayEnumerator.new do |yielder| @clazz.attribute_names.each do |attribute_name| yielder << PeakFlowUtils::AttributeService.new(self, attribute_name) end end end |
#class_key ⇒ Object
64 65 66 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 64 def class_key "activerecord.models.#{snake_name}" end |
#class_key_one ⇒ Object
68 69 70 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 68 def class_key_one "#{class_key}.one" end |
#class_key_other ⇒ Object
72 73 74 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 72 def class_key_other "#{class_key}.other" end |
#globalize_attributes ⇒ Object
52 53 54 55 56 57 58 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 52 def globalize_attributes return if !::Kernel.const_defined?("Globalize") || !@clazz.respond_to?(:translated_attribute_names) @clazz.translated_attribute_names.each do |attribute| yield attribute.to_s end end |
#inspect ⇒ Object
91 92 93 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 91 def inspect to_s end |
#money_attributes ⇒ Object
44 45 46 47 48 49 50 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 44 def money_attributes return if !::Kernel.const_defined?("Money") || !@clazz.respond_to?(:monetized_attributes) @clazz.monetized_attributes.each do |attribute| yield attribute[0].to_s end end |
#paperclip_attachments ⇒ Object
36 37 38 39 40 41 42 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 36 def return unless ::Kernel.const_defined?("Paperclip") Paperclip::AttachmentRegistry.names_for(@clazz).each do |name| yield name end end |
#relationships ⇒ Object
TODO: Maybe this should yield a ModelInspector::Relationship instead?
77 78 79 80 81 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 77 def relationships @clazz.reflections.each do |key, reflection| yield key, reflection end end |
#snake_name ⇒ Object
60 61 62 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 60 def snake_name clazz.name.gsub("::", "/").split("/").map(&:underscore).join("/") end |
#to_s ⇒ Object
87 88 89 |
# File 'app/services/peak_flow_utils/model_inspector.rb', line 87 def to_s "<PeakFlowUtils::ModelInspector class-name: \"#{@clazz.name}\">" end |