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