Class: AwesomeTranslations::ModelInspector
- Inherits:
-
Object
- Object
- AwesomeTranslations::ModelInspector
- Defined in:
- lib/awesome_translations/model_inspector.rb
Defined Under Namespace
Classes: Attribute
Instance Attribute Summary collapse
-
#clazz ⇒ Object
readonly
Returns the value of attribute clazz.
Class Method Summary collapse
- .active_storage_path?(model_path) ⇒ Boolean
- .engines ⇒ Object
- .find_subclasses(clazz, &blk) ⇒ 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.
25 26 27 |
# File 'lib/awesome_translations/model_inspector.rb', line 25 def initialize(clazz) @clazz = clazz end |
Instance Attribute Details
#clazz ⇒ Object (readonly)
Returns the value of attribute clazz.
4 5 6 |
# File 'lib/awesome_translations/model_inspector.rb', line 4 def clazz @clazz end |
Class Method Details
.active_storage_path?(model_path) ⇒ Boolean
140 141 142 |
# File 'lib/awesome_translations/model_inspector.rb', line 140 def self.active_storage_path?(model_path) model_path.match?(/\/gems\/activestorage-([\d\.]+)\//) end |
.engines ⇒ Object
121 122 123 |
# File 'lib/awesome_translations/model_inspector.rb', line 121 def self.engines ::Rails::Engine.subclasses.map(&:instance) end |
.find_subclasses(clazz, &blk) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/awesome_translations/model_inspector.rb', line 96 def self.find_subclasses(clazz, &blk) return if @scanned[clazz.name] @scanned[clazz.name] = true clazz.subclasses.each do |subclass| blk.call ::AwesomeTranslations::ModelInspector.new(subclass) find_subclasses(subclass, &blk) end end |
.load_models ⇒ Object
Preloads all models for Rails app and all engines (if they aren’t loaded, then they cant be inspected).
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/awesome_translations/model_inspector.rb', line 108 def self.load_models return false if AwesomeTranslations::ModelInspector.models_loaded AwesomeTranslations::ModelInspector.models_loaded = true load_models_for(Rails.root) engines.each do |engine| load_models_for(engine.root) end true end |
.load_models_for(root) ⇒ Object
Loads models for the given app-directory (Rails-root or engine).
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/awesome_translations/model_inspector.rb', line 126 def self.load_models_for(root) Dir.glob("#{root}/app/models/**/*.rb").sort.each do |model_path| next if active_storage_path?(model_path) begin require model_path rescue StandardError => e warn "Could not load model in #{model_path}" warn e.inspect warn e.backtrace end end end |
.model_classes ⇒ Object
Yields a model-inspector for each model found in the application.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/awesome_translations/model_inspector.rb', line 8 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
84 85 86 |
# File 'lib/awesome_translations/model_inspector.rb', line 84 def attribute_key(attribute_name) "activerecord.attributes.#{snake_name}.#{attribute_name}" end |
#attributes ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/awesome_translations/model_inspector.rb', line 29 def attributes ArrayEnumerator.new do |yielder| @clazz.attribute_names.each do |attribute_name| yielder << ::AwesomeTranslations::ModelInspector::Attribute.new(self, attribute_name) end end end |
#class_key ⇒ Object
65 66 67 |
# File 'lib/awesome_translations/model_inspector.rb', line 65 def class_key "activerecord.models.#{snake_name}" end |
#class_key_one ⇒ Object
69 70 71 |
# File 'lib/awesome_translations/model_inspector.rb', line 69 def class_key_one "#{class_key}.one" end |
#class_key_other ⇒ Object
73 74 75 |
# File 'lib/awesome_translations/model_inspector.rb', line 73 def class_key_other "#{class_key}.other" end |
#globalize_attributes ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/awesome_translations/model_inspector.rb', line 53 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
92 93 94 |
# File 'lib/awesome_translations/model_inspector.rb', line 92 def inspect to_s end |
#money_attributes ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/awesome_translations/model_inspector.rb', line 45 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
37 38 39 40 41 42 43 |
# File 'lib/awesome_translations/model_inspector.rb', line 37 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?
78 79 80 81 82 |
# File 'lib/awesome_translations/model_inspector.rb', line 78 def relationships @clazz.reflections.each do |key, reflection| yield key, reflection end end |
#snake_name ⇒ Object
61 62 63 |
# File 'lib/awesome_translations/model_inspector.rb', line 61 def snake_name clazz.name.gsub("::", "/").split("/").map { |part| ::StringCases.camel_to_snake(part) }.join("/") end |
#to_s ⇒ Object
88 89 90 |
# File 'lib/awesome_translations/model_inspector.rb', line 88 def to_s "<AwesomeTranslations::ModelInspector class-name: \"#{@clazz.name}\">" end |