Class: Localtower::Tools

Inherits:
Object
  • Object
show all
Defined in:
lib/localtower/tools.rb

Defined Under Namespace

Classes: ThorTools

Class Method Summary collapse

Class Method Details

.all_columnsObject



116
117
118
# File 'lib/localtower/tools.rb', line 116

def all_columns
  models.map { |model| model.columns.map { |column| column.name }.flatten }.flatten.uniq.sort
end

.create_logObject

PRIVATE ==============



185
186
187
188
# File 'lib/localtower/tools.rb', line 185

def create_log
  return nil if File.exist?(self.log_file)
  File.open(self.log_file, 'w') { |f| f.write('') }
end

.force_reload!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/localtower/tools.rb', line 15

def force_reload!
  app_folders = Dir["#{Rails.root}/app/models/**/*.rb"]
  lib_folders = Dir["#{Rails.root}/lib/**/*.rb"]

  all_folders = (app_folders + lib_folders).flatten

  all_folders.each do |file|
    begin
      ActiveSupport::Dependencies.require_or_load(file)
    rescue Exception => e
      puts "Error loading: #{file}"
    end
  end
end

.indexes_for_model(model) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/localtower/tools.rb', line 73

def indexes_for_model(model)
  indexes = ActiveRecord::Base.connection.indexes(model.table_name)
  return [] if indexes.empty?

  max_size = indexes.map { |index| index.name.size }.max + 1

  list = []

  indexes.each do |index|
    list << {
      unique: index.unique,
      name: index.name,
      columns: index.columns,
      max_size: max_size
    }
  end

  list
end

.indexes_for_model_and_attribute(model, attribute_name) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/localtower/tools.rb', line 93

def indexes_for_model_and_attribute(model, attribute_name)
  indexes = self.indexes_for_model(model)

  list = []
  done = []

  indexes.each do |index|
    conditions = index[:columns].include?(attribute_name.to_s) and not done.include?(index[:name])
    next unless conditions
    infos = {
      unique: index[:unique],
      name: index[:name],
      columns: index[:columns],
      max_size: index[:max_size]
    }

    done << attribute_name
    list << infos
  end

  list
end

.log(str) ⇒ Object



174
175
176
177
178
# File 'lib/localtower/tools.rb', line 174

def log(str)
  self.create_log
  log_str = "[#{Time.now}] - #{str}\n"
  File.open(self.log_file, 'a') { |f| f.write(log_str) }
end

.log_fileObject



180
181
182
# File 'lib/localtower/tools.rb', line 180

def log_file
  Rails.root.join('log', 'localtower.log')
end

.modelsObject



30
31
32
33
34
35
# File 'lib/localtower/tools.rb', line 30

def models
  self.force_reload!

  root_klass = defined?(ApplicationRecord) ? ApplicationRecord : ActiveRecord::Base
  root_klass.subclasses
end

.models_presentedObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/localtower/tools.rb', line 37

def models_presented
  self.force_reload!

  list = []

  self.models.each do |model|
    attributes_list = []

    model.columns_hash.each do |_k, v|

      belongs_to = nil

      if v.name.strip =~ /\_id$/
        belongs_to = v.name.strip.gsub(/_id$/, "").singularize.camelize
      end

      attributes_list << {
        'name' => v.name.strip,
        'type' => v.sql_type.strip,
        'belongs_to' => belongs_to,
        'type_clean' => v.sql_type.split(' ')[0].strip,
        'primary' => (v.respond_to?(:primary) ? v.primary : false),
        'index' => self.indexes_for_model_and_attribute(model, v.name),
      }
    end

    list << {
      name: model.name,
      table_name: model.table_name,
      attributes_list: attributes_list,
    }
  end

  list
end

.perform_cmd(cmd_str, standalone = true) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/localtower/tools.rb', line 141

def perform_cmd(cmd_str, standalone = true)
  self.perform_raw_cmd("bundle exec #{cmd_str}", standalone)

  # if cmd_str["rake"]
    # self.perform_raw_cmd("zeus #{cmd_str}", standalone)
  # elsif cmd_str["rails g migration"]
    # cmd = cmd_str.split("rails g migration")[1]
    # self.perform_raw_cmd("zeus generate migration #{cmd}", standalone)
  # else
    # self.perform_raw_cmd("bundle exec #{cmd_str}", standalone)
  # end
end

.perform_migration(str, standalone = false) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/localtower/tools.rb', line 131

def perform_migration(str, standalone = false)
  self.perform_cmd("rails g migration #{str}", false)

  if not standalone
    # self.perform_raw_cmd("z rake #{cmd_str}", standalone)
    self.perform_cmd('rake db:migrate')
    # self.perform_cmd('rake db:migrate RAILS_ENV=test')
  end
end

.perform_raw_cmd(cmd_str, standalone = false, root_dir = false) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/localtower/tools.rb', line 154

def perform_raw_cmd(cmd_str, standalone = false, root_dir = false)
  root_dir ||= ::Rails.root

  cmd = standalone ? cmd_str : "cd \"#{root_dir}\" && #{cmd_str}"
  cmd = cmd.strip

  self.log("DOING...: #{cmd}")

  before_sec = Time.now
  output = `#{cmd}`

  after_sec = Time.now
  self.log(output)

  sec = after_sec - before_sec

  self.log("DONE: #{cmd} in #{sec} sec")
  output
end

.rails_projectObject



127
128
129
# File 'lib/localtower/tools.rb', line 127

def rails_project
  Rails.root.to_s.split("/").last
end

.sql_drop_all_tablesObject



120
121
122
123
124
125
# File 'lib/localtower/tools.rb', line 120

def sql_drop_all_tables
  ::ActiveRecord::Base.connection.tables.each do |table|
    cmd = "DROP TABLE if exists #{table.upcase} cascade;"
    ::ActiveRecord::Base.connection.execute(cmd)
  end
end

.word_in_file?(file, word_or_exp) ⇒ Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/localtower/tools.rb', line 190

def word_in_file?(file, word_or_exp)
  File.readlines(file).grep(word_or_exp).size > 0
end