Class: AwesomeTranslations::ModelInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/awesome_translations/model_inspector.rb

Defined Under Namespace

Classes: Attribute

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clazz) ⇒ ModelInspector

Returns a new instance of ModelInspector.



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

def initialize(clazz)
  @clazz = clazz
end

Instance Attribute Details

#clazzObject (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

Returns:

  • (Boolean)


137
138
139
# File 'lib/awesome_translations/model_inspector.rb', line 137

def self.active_storage_path?(model_path)
  model_path.match?(/\/gems\/activestorage-([\d.]+)\//)
end

.enginesObject



118
119
120
# File 'lib/awesome_translations/model_inspector.rb', line 118

def self.engines
  ::Rails::Engine.subclasses.map(&:instance)
end

.find_subclasses(clazz, &blk) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/awesome_translations/model_inspector.rb', line 93

def self.find_subclasses(clazz, &blk)
  return if @scanned[clazz.name]

  @scanned[clazz.name] = true

  clazz.subclasses.each do |subclass|
    yield ::AwesomeTranslations::ModelInspector.new(subclass)
    find_subclasses(subclass, &blk)
  end
end

.load_modelsObject

Preloads all models for Rails app and all engines (if they aren’t loaded, then they cant be inspected).



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/awesome_translations/model_inspector.rb', line 105

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).



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/awesome_translations/model_inspector.rb', line 123

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_classesObject

Yields a model-inspector for each model found in the application.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/awesome_translations/model_inspector.rb', line 9

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



81
82
83
# File 'lib/awesome_translations/model_inspector.rb', line 81

def attribute_key(attribute_name)
  "activerecord.attributes.#{snake_name}.#{attribute_name}"
end

#attributesObject



30
31
32
33
34
35
36
# File 'lib/awesome_translations/model_inspector.rb', line 30

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_keyObject



64
65
66
# File 'lib/awesome_translations/model_inspector.rb', line 64

def class_key
  "activerecord.models.#{snake_name}"
end

#class_key_oneObject



68
69
70
# File 'lib/awesome_translations/model_inspector.rb', line 68

def class_key_one
  "#{class_key}.one"
end

#class_key_otherObject



72
73
74
# File 'lib/awesome_translations/model_inspector.rb', line 72

def class_key_other
  "#{class_key}.other"
end

#globalize_attributesObject



52
53
54
55
56
57
58
# File 'lib/awesome_translations/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

#inspectObject



89
90
91
# File 'lib/awesome_translations/model_inspector.rb', line 89

def inspect
  to_s
end

#money_attributesObject



44
45
46
47
48
49
50
# File 'lib/awesome_translations/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(&blk) ⇒ Object



38
39
40
41
42
# File 'lib/awesome_translations/model_inspector.rb', line 38

def paperclip_attachments(&blk)
  return unless ::Kernel.const_defined?(:Paperclip)

  Paperclip::AttachmentRegistry.names_for(@clazz, &blk)
end

#relationships(&blk) ⇒ Object

TODO: Maybe this should yield a ModelInspector::Relationship instead?



77
78
79
# File 'lib/awesome_translations/model_inspector.rb', line 77

def relationships(&blk)
  @clazz.reflections.each(&blk)
end

#snake_nameObject



60
61
62
# File 'lib/awesome_translations/model_inspector.rb', line 60

def snake_name
  clazz.name.gsub("::", "/").split("/").map { |part| ::StringCases.camel_to_snake(part) }.join("/")
end

#to_sObject



85
86
87
# File 'lib/awesome_translations/model_inspector.rb', line 85

def to_s
  "<AwesomeTranslations::ModelInspector class-name: \"#{@clazz.name}\">"
end