Class: ModelAttributes::ChangeModel

Inherits:
Object
  • Object
show all
Defined in:
lib/model_attributes/change_model.rb

Instance Method Summary collapse

Instance Method Details

#add_attr_desc_to_modelsObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/model_attributes/change_model.rb', line 98

def add_attr_desc_to_models      
  models_with_desc = create_desc_for_models

  paths = models_path(models_with_desc.keys)

  paths.each do |k,v|
    insert_desc_to_model(v, models_with_desc[k], k)
  end

end

#create_desc_for_modelsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/model_attributes/change_model.rb', line 25

def create_desc_for_models
  models = get_models
  
  models.each do |key, value|       
    str = ""      
    value.each do |attribute, type|          
      str+= "# :" + attribute + " => " + type + "\n"          
    end        

    str.insert(0, "#" * 10 + " #{key}" + " Attributes " + "#" * 17 + "\n")
    str.insert(str.length, "#" * 20 + " End of Attributes " + "#" * 10 + "\n" + "\n")
    
    models[key] = str
  end
  models
end

#get_modelsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/model_attributes/change_model.rb', line 5

def get_models         
  Rails.application.eager_load!
  all_models = ActiveRecord::Base.descendants

  models_hash = {}
  models_attributes = {}


  all_models.each do |model|        
    next if model.name.include?("::")        
    model.columns_hash.each do |attribute, details|         
      models_attributes[attribute] = details.type.to_s          
    end                
    models_hash[model.name] = models_attributes
    models_attributes = {}
  end

  models_hash
end

#insert_desc_to_model(path, desc, model) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/model_attributes/change_model.rb', line 63

def insert_desc_to_model(path, desc, model)      
  has_desc = false
  end_of_desc = false
  valid_line = true
  new_content = ""
  old_content = ""

  File.open(path, "r") do |file|        
    file.each do |line|
      old_content += line
      
      if line.include?("#" * 10 + " #{model}" + " Attributes " + "#" * 17) 
        has_desc = true          
      elsif line.include?("#" * 20 + " End of Attributes " + "#" * 10)
        has_desc = true
        end_of_desc = true
        valid_line = false
      end

      if !has_desc || (has_desc && end_of_desc && valid_line)
        new_content += line            
      end
      valid_line = true
    end
    new_content.insert(0, desc)        
  end

  if old_content != new_content
    File.open(path, "w+") do |file|
      file.write(new_content)
    end
  end
  
end

#models_path(models_names) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/model_attributes/change_model.rb', line 42

def models_path(models_names)
  paths = {}

  models_names.each do |name|
    file_name = ""

    name.split("").each_with_index do |n, index|          
      if n == n.upcase && index != 0 && index + 1 != name.length 
        file_name += "_" + n.downcase
      elsif n == n.upcase
        file_name += n.downcase
      else
        file_name += n
      end
    end
    paths[name] = Rails.root.join('app/models/').join(file_name + ".rb")
  end

  paths
end