Class: DutyFree::ModelGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
FancyGets
Defined in:
lib/generators/duty_free/model_generator.rb

Overview

Auto-generates an IMPORT_TEMPLATE entry for a model

Instance Method Summary collapse

Instance Method Details

#df_model_templateObject



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
56
57
58
59
60
61
62
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/generators/duty_free/model_generator.rb', line 25

def df_model_template
  # %%% If Apartment is active, ask which schema they want

  # Load all models
  Rails.configuration.eager_load_namespaces.select { |ns| ns < Rails::Application }.each(&:eager_load!)

  # Generate a list of viable models that can be chosen
  longest_length = 0
  model_info = Hash.new { |h, k| h[k] = {} }
  tableless = Hash.new { |h, k| h[k] = [] }
  models = ActiveRecord::Base.descendants.reject do |m|
    trouble = if m.abstract_class?
                true
              elsif !m.table_exists?
                tableless[m.table_name] << m.name
                ' (No Table)'
              else
                this_f_keys = (model_info[m][:f_keys] = m.reflect_on_all_associations.select { |a| a.macro == :belongs_to }) || []
                column_names = (model_info[m][:column_names] = m.columns.map(&:name) - [m.primary_key, 'created_at', 'updated_at', 'deleted_at'] - this_f_keys.map(&:foreign_key))
                if column_names.empty? && this_f_keys && !this_f_keys.empty?
                  fk_message = ", although #{this_f_keys.length} foreign keys"
                  " (No columns#{fk_message})"
                end
              end
    # puts "#{m.name}#{trouble}" if trouble&.is_a?(String)
    trouble
  end
  models.sort! do |a, b| # Sort first to separate namespaced stuff from the rest, then alphabetically
    is_a_namespaced = a.name.include?('::')
    is_b_namespaced = b.name.include?('::')
    if is_a_namespaced && !is_b_namespaced
      1
    elsif !is_a_namespaced && is_b_namespaced
      -1
    else
      a.name <=> b.name
    end
  end
  models.each do |m| # Find longest name in the list for future use to show lists on the right side of the screen
    # Strangely this can't be inlined since it assigns to "len"
    if longest_length < (len = m.name.length)
      longest_length = len
    end
  end

  model_name = ARGV[0]&.camelize
  unless (starting = models.find { |m| m.name == model_name })
    puts "#{"Couldn't find #{model_name}.  " if model_name}Pick a model to start from:"
    starting = gets_list(
      list: models,
      on_select: proc do |item|
        selected = item[:selected] || item[:focused]
        this_model_info = model_info[selected]
        selected.name + " (#{(this_model_info[:column_names] + this_model_info[:f_keys].map(&:name).map(&:upcase)).join(', ')})"
      end
    )
  end
  puts "\nThinking..."

  # %%% Find out how many hops at most we can go from this model
  max_hm_nav = starting.suggest_template(-1, true, false)
  max_bt_nav = starting.suggest_template(-1, false, false)
  hops_with_hm, num_hm_hops_tables = calc_num_hops([[starting, max_hm_nav[:all]]], models)
  hops, num_hops_tables = calc_num_hops([[starting, max_bt_nav[:all]]], models)
  # print "\b" * 11
  unless hops_with_hm.length == hops.length
    starting_name = starting.name
    unless (is_hm = ARGV[1]&.downcase)
      puts "Navigate from #{starting_name} using:\n#{'=' * (21 + starting_name.length)}"
      is_hm = gets_list(
        ["Only belongs_to (max of #{hops.length} hops and #{num_hops_tables} tables)",
         "has_many as well as belongs_to (max of #{hops_with_hm.length} hops and #{num_hm_hops_tables} tables)"]
      )
    end
    is_hm = is_hm.start_with?('has_many') || is_hm[0] == 'y'
    hops = hops_with_hm if is_hm
  end

  unless (num_hops = ARGV[2]&.to_i)
    puts "\nNow, how many hops total would you like to navigate?"
    index = 0
    cumulative = 0
    hops_list = ['0'] + hops.map { |h| "#{index += 1} (#{cumulative += h.length} linkages)" }
    num_hops = gets_list(
      list: hops_list,
      on_select: proc do |value|
                   associations = Hash.new { |h, k| h[k] = 0 }
                   index = (value[:selected] || value[:focused]).split(' ').first.to_i - 1
                   layer = hops[index] if index >= 0
                   layer ||= []
                   layer.each { |i| associations[i.last] += 1 }
                   associations.each { |k, v| associations.delete(k) if v == 1 }
                   layer.map do |l|
                     associations.keys.include?(l.last) ? "#{l.first.name.demodulize} #{l.last}" : l.last
                   end.join(', ')
                   # y = model_info[data[:focused].name]
                   # data[:focused].name + " (#{(y[:column_names] + y[:f_keys].map(&:name).map(&:upcase)).join(', ')})"
                   # layer.inspect
                 end
    ).split(' ').first.to_i
  end

  print "Navigating from #{starting_name}" if model_name
  puts "\nOkay, #{num_hops} hops#{', including has_many,' if is_hm} it is!"
  # Grab the console output from this:
  original_stdout = $stdout
  $stdout = StringIO.new
  starting.suggest_template(num_hops, is_hm)
  output = $stdout
  $stdout = original_stdout
  filename = nil
  output.rewind
  lines = output.each_line.each_with_object([]) do |line, s|
    if line == "\n"
      # Do nothing
    elsif filename
      s << line
    elsif line.start_with?('# Place the following into ')
      filename = line[27..-1]&.split(':')&.first
    end
    s
  end

  model_file = File.open(filename, 'r+')
  insert_at = nil
  starting_name = starting.name.demodulize
  loop do
    break if model_file.eof?

    line_parts = model_file.readline.strip.gsub(/ +/, ' ').split(' ')
    if line_parts.first == 'class' && line_parts[1] && (line_parts[1] == starting_name || line_parts[1].end_with?("::#{starting_name}"))
      insert_at = model_file.pos
      break
    end
  end
  line = nil
  import_template_blocks = []
  import_template_block = nil
  indentation = nil
  # See if there's already any IMPORT_TEMPLATE entries in the model file.
  # If there already is just one, we will comment it out if needs be before adding a fresh one.
  loop do
    break if model_file.eof?

    line_parts = (line = model_file.readline).strip.split(/[\s=]+/)
    indentation ||= line[0...(/\S/ =~ line)]
    case line_parts[-2..-1]
    when ['IMPORT_TEMPLATE', '{']
      import_template_blocks << import_template_block if import_template_block
      import_template_block = [model_file.pos - line.length, nil, line.strip[0] == '#', []]
    when ['#', '------------------------------------------']
      import_template_block[1] = model_file.pos if import_template_block # && import_template_block[1].nil?
    end
    next unless import_template_block

    # Collect all the lines of any existing block
    import_template_block[3] << line
    # Cap this one if it's done
    if import_template_block[1]
      import_template_blocks << import_template_block
      import_template_block = nil
    end
  end
  import_template_blocks << import_template_block if import_template_block
  comments = nil
  is_add_cr = nil
  if import_template_blocks.length > 1
    # %%% maybe in the future:  remove any older commented ones
    puts 'Found multiple existing import template blocks.  Will not attempt to automatically add yet another.'
    insert_at = nil
  elsif import_template_blocks.length == 1
    # Get set up to add the new block after the existing one
    insert_at = (import_template_block = import_template_blocks.first)[1]
    if insert_at.nil?
      puts "Found what looked like the start of an existing IMPORT_TEMPLATE block, but couldn't determine where it ends.  Will not attempt to automatically add anything."
    elsif import_template_block[2] # Already commented
      is_add_cr = true
    else # Needs to be commented
      # Find what kind and how much indentation is present from the first commented line
      indentation = import_template_block[3].first[0...(/\S/ =~ import_template_block[3].first)]
      comments = import_template_block[3].map { |l| "#{l[0...indentation.length]}# #{l[indentation.length..-1]}" }
    end
    # else # Must be no IMPORT_TEMPLATE block yet
    #   insert_at = model_file.pos
  end
  if insert_at.nil?
    puts "Please edit #{filename} manually and add this code:\n\n#{lines.join}"
  else
    is_good = ARGV[3]&.downcase&.start_with?('y')
    args = [starting_name,
            is_hm ? 'has_many' : 'no',
            num_hops]
    args << 'yes' if is_good
    args = args.each_with_object(+'') do |v, s|
      s << " #{v}"
      s
    end
    lines.unshift("# Added #{DateTime.now.strftime('%b %d, %Y %I:%M%P')} by running `bin/rails g duty_free:model#{args}`\n")
    # add a new one afterwards
    print is_good ? 'Will' : 'OK to'
    print "#{" comment #{comments.length} existing lines and" if comments} add #{lines.length} new lines to #{filename}"
    puts is_good ? '.' : '?'
    if is_good || gets_list(%w[Yes No]) == 'Yes'
      # Store rest of file
      model_file.pos = insert_at
      rest_of_file = model_file.read
      if comments
        model_file.pos = import_template_block[0]
        model_file.write("#{comments.join}\n")
        puts "Commented #{comments.length} existing lines"
      else
        model_file.pos = insert_at
      end
      model_file.write("\n") if is_add_cr
      model_file.write(lines.map { |l| "#{indentation}#{l}" }.join)
      model_file.write(rest_of_file)
    end
  end
  model_file.close
end