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
|
# 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'
name = model.name.underscore
model_specs[name] = Dashboard::ModelSpec.new model
end
spec = model_specs[model]
if not spec.associated_models.empty?
spec.revise_stage_1!
cli = HighLine.new
cli.say "\nModel #{model} has associated with other models."
spec.associated_models.each do |associated_model, options|
spec.associated_models[associated_model][:choose_by] = cli.choose do ||
.prompt = "Choose the attribute of model #{associated_model} for choosing in #{model}? "
attributes = options[:polymorphic] ? options[:common_attributes] : model_specs[associated_model].attributes.permitted
attributes.each do |attribute|
next if attribute =~ /_id$/
.choice attribute.to_sym
end
end
end
spec.revise_stage_2!
end
begin
load 'app/carload/dashboard.rb'
rescue LoadError
Dashboard.models[model.to_sym] = spec
Dashboard.write 'app/carload/dashboard.rb'
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
|