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
|
# File 'lib/generators/carload/dash_generator.rb', line 5
def change_dashboard_file
Rails.application.eager_load!
model_specs = {}
ActiveRecord::Base.descendants.each do |model|
next if model.name == 'ApplicationRecord' or model.name == 'PgSearch::Document'
model_specs[model.name.underscore.to_sym] = ModelSpec.new model
end
model_name = file_name.to_sym
spec = model_specs[model_name]
if not spec.associations.empty?
cli = HighLine.new
spec.associations.each do |name, association|
next if association[:filtered]
reflection = association[:reflection]
next if reflection.class ==
if reflection.options[:polymorphic]
attributes = association[:attributes]
elsif reflection.options[:class_name]
attributes = model_specs[reflection.options[:class_name].underscore.to_sym].attributes.permitted.select { |x| x.class != Hash }
else
attributes = model_specs[reflection.name.to_s.singularize.to_sym].attributes.permitted.select { |x| x.class != Hash }
end
if attributes.size == 1
association[:choose_by] = attributes.first
else
association[:choose_by] = cli.choose do ||
.prompt = "Choose the attribute of model #{reflection.name} for choosing in #{model_name}? "
attributes.each do |attribute|
next if attribute.to_s =~ /_id$/
.choice attribute
end
end
end
spec.index_page[:shows][:attributes] = spec.index_page[:shows][:attributes].map do |attribute|
if attribute.to_s =~ /#{name}/
attribute = "#{name}.#{association[:choose_by]}"
else
attribute
end
end
end
end
if not Dashboard.models.keys.include? model_name or Dashboard.model(model_name).changed? spec
Dashboard.models[model_name] = spec
Dashboard.write 'app/carload/dashboard.rb'
end
end
|