Class: Carload::DashGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/carload/dash_generator.rb

Instance Method Summary collapse

Instance Method Details

#change_dashboard_fileObject



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
  # Process model once atime.
  model = file_name
  model_specs = {}
  Rails.application.eager_load! # It is necessary to load models manually.
  ActiveRecord::Base.descendants.each do |model| # Rails 5 can use ApplicationRecord.
    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 |menu|
        menu.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$/
          menu.choice attribute.to_sym
        end
      end
    end
    spec.revise_stage_2!
  end
  # Check if model exists in dashboard file, but it may be changed.
  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