Class: GettextSimpleRails::ModelInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/gettext_simple_rails/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.



16
17
18
# File 'lib/gettext_simple_rails/model_inspector.rb', line 16

def initialize(clazz)
  @clazz = clazz
end

Instance Attribute Details

#clazzObject (readonly)

Returns the value of attribute clazz.



14
15
16
# File 'lib/gettext_simple_rails/model_inspector.rb', line 14

def clazz
  @clazz
end

Class Method Details

.model_classesObject



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/gettext_simple_rails/model_inspector.rb', line 2

def self.model_classes
  clazzes = []
  ::Rails.application.eager_load!
  
  ::Object.constants.each do |clazz|
    clazz = clazz.to_s.constantize
    next unless clazz.class == Class
    next unless clazz < ActiveRecord::Base
    yield ::GettextSimpleRails::ModelInspector.new(clazz)
  end
end

Instance Method Details

#attributesObject



20
21
22
23
24
# File 'lib/gettext_simple_rails/model_inspector.rb', line 20

def attributes
  @clazz.attribute_names.each do |attribute_name|
    yield ::GettextSimpleRails::ModelInspector::Attribute.new(self, attribute_name)
  end
end

#gettext_keyObject



37
38
39
# File 'lib/gettext_simple_rails/model_inspector.rb', line 37

def gettext_key
  return "models.name.#{snake_name}"
end

#gettext_key_oneObject



41
42
43
# File 'lib/gettext_simple_rails/model_inspector.rb', line 41

def gettext_key_one
  return "#{gettext_key}.one"
end

#gettext_key_otherObject



45
46
47
# File 'lib/gettext_simple_rails/model_inspector.rb', line 45

def gettext_key_other
  return "#{gettext_key}.other"
end

#paperclip_attachmentsObject



26
27
28
29
30
31
# File 'lib/gettext_simple_rails/model_inspector.rb', line 26

def paperclip_attachments
  return [] unless ::Kernel.const_defined?("Paperclip")
  Paperclip::AttachmentRegistry.names_for(@clazz).each do |name|
    yield(name)
  end
end

#relationship_gettext_key(name) ⇒ Object



56
57
58
# File 'lib/gettext_simple_rails/model_inspector.rb', line 56

def relationship_gettext_key(name)
  return "models.attributes.#{snake_name}.#{name}"
end

#relationshipsObject

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



50
51
52
53
54
# File 'lib/gettext_simple_rails/model_inspector.rb', line 50

def relationships
  @clazz.reflections.each do |key, reflection|
    yield key, reflection
  end
end

#snake_nameObject



33
34
35
# File 'lib/gettext_simple_rails/model_inspector.rb', line 33

def snake_name
  return ::StringCases.camel_to_snake(clazz.name)
end