Class: Brick::ControllersGenerator

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

Overview

Auto-generates controllers

Instance Method Summary collapse

Instance Method Details

#brick_controllersObject



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/generators/brick/controllers_generator.rb', line 16

def brick_controllers
  # %%% If Apartment is active and there's no schema_to_analyse, ask which schema they want

  ::Brick.mode = :on
  ActiveRecord::Base.establish_connection

  # Load all models and controllers
  ::Brick.eager_load_classes

  # Generate a list of viable controllers that can be chosen
  longest_length = 0
  model_info = Hash.new { |h, k| h[k] = {} }
  tableless = Hash.new { |h, k| h[k] = [] }
  existing_controllers = ActionController::Base.descendants.reject do |c|
    c.name.nil? || c.name&.start_with?('Turbo::Native::')
  end.map(&:name)
  controllers = ::Brick.relations.each_with_object([]) do |rel, s|
    next if rel.first.is_a?(Symbol)

    tbl_parts = rel.first.split('.')
    tbl_parts.shift if [::Brick.default_schema, 'public'].include?(tbl_parts.first)
    begin
      s << ControllerOption.new(tbl_parts.join('/').camelize, rel.last[:class_name].constantize)
    rescue
    end
  end.reject { |c| existing_controllers.include?(c.to_s) }
  controllers.sort! do |a, b| # Sort first to separate namespaced stuff from the rest, then alphabetically
    is_a_namespaced = a.to_s.include?('::')
    is_b_namespaced = b.to_s.include?('::')
    if is_a_namespaced && !is_b_namespaced
      1
    elsif !is_a_namespaced && is_b_namespaced
      -1
    else
      a.to_s <=> b.to_s
    end
  end
  controllers.each do |m| # Find longest name in the list for future use to show lists on the right side of the screen
    if longest_length < (len = m.to_s.length)
      longest_length = len
    end
  end
  chosen = gets_list(list: controllers, chosen: controllers.dup)
  relations = ::Brick.relations
  chosen.each do |controller_option|
    namespace = if (controller_parts = controller_option.to_s.split('::')).length > 1
                  controller_parts.first.constantize
                else
                  Object
                end
    controller_parts[controller_parts.length - 1] = (controller_name = "#{controller_parts.last}Controller")
    _built_controller, code = Object.send(:build_controller, namespace, controller_name, controller_name, controller_option.model, relations)
    path = ['controllers']
    path.concat(controller_parts.map(&:underscore))
    dir = +"#{::Rails.root}/app"
    path[0..-2].each do |path_part|
      dir << "/#{path_part}"
      Dir.mkdir(dir) unless Dir.exist?(dir)
    end
    File.open("#{dir}/#{path.last}.rb", 'w') { |f| f.write code } unless code.blank?
  end
  puts "\n*** Created #{chosen.length} controller files under app/controllers ***"
end