Class: ZenAdmin::Generators::ModelGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/zen_admin/model/model_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_model_if_missingObject



9
10
11
12
13
14
15
# File 'lib/generators/zen_admin/model/model_generator.rb', line 9

def create_model_if_missing
  path = File.join("app/models", class_path, "#{file_name}.rb")
  unless File.exist?(path)
    say "Model #{class_name} not found. Generating it...", :yellow
    generate "model", "#{class_name} #{attributes.map { |a| "#{a.name}:#{a.type}" }.join(' ')}"
  end
end

#inject_zen_admin_configObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/generators/zen_admin/model/model_generator.rb', line 17

def inject_zen_admin_config
  path = File.join("app/models", class_path, "#{file_name}.rb")
  
  if File.exist?(path)
    @attrs = derived_attributes
    
    # 渲染模板
    template_path = File.expand_path("templates/zen_admin_config.rb.erb", __dir__)
    config_content = ERB.new(File.read(template_path), trim_mode: '-').result(binding)

    inject_into_class path, class_name do
      config_content
    end
    
    say "Successfully integrated ZenAdmin DSL into #{class_name}", :green
  else
    say "Error: Model file #{path} not found.", :red
  end
end