-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute.rb
More file actions
55 lines (52 loc) · 1.69 KB
/
attribute.rb
File metadata and controls
55 lines (52 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module I18n
module Tasks
module Ar
module Attribute
class << self
# Load classes and attributes and transform them into tash
#
# Attribue.names # => { 'MyClass' => ['first_attribute', 'second_attribute'] }
#
def names
result = {}
Rails.application.eager_load! if Rails.env == 'development' rescue true
ActiveRecord::Base.descendants.each do |model|
result[model.name] = model.attribute_names
end
result
end
#
# Generate hash with models and attributes transformed into i18n structure
#
# Attribute.hash # => { 'my_model/subclass' => { 'id' => 'ID', 'attribute_name' => 'Attribute name' } }
#
def hash
if !names.empty?
result = {}
names.each do |key, value|
hash_name = Model.slashed(key)
result[hash_name] = {}
value.each do |attribute|
result[hash_name].merge!({ attribute => attribute.gsub('_', ' ').capitalize })
end
end
result
end
end
#
# Generate final hash with translatable structure
#
# Attribute.final_hash('en') # => { 'en' => { 'activerecord' => { 'attributes' => { ... } } } }
#
# @param lang [String] [Translation locale]
#
def final_hash lang
result = {}
result[lang.to_s] = { 'activerecord' => { 'attributes' => hash } }
result
end
end
end
end
end
end