Class: Trek::Generators::ScaffoldGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::ResourceHelpers, Helpers
Defined in:
lib/generators/trek/scaffold_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_default_locale_filesObject



73
74
75
76
# File 'lib/generators/trek/scaffold_generator.rb', line 73

def add_default_locale_files
  template "locales/scaffold/admin.en.yml", admin_locale_file(:en)
  template "locales/scaffold/model.en.yml", model_locale_file(:en)
end

#add_model_messageObject



29
30
31
# File 'lib/generators/trek/scaffold_generator.rb', line 29

def add_model_message
  Rails.logger.debug "Now you can edit the model in #{model_file_path}"
end

#add_models_to_menuObject



45
46
47
48
49
50
# File 'lib/generators/trek/scaffold_generator.rb', line 45

def add_models_to_menu
  inject_into_file dashboard_file_path,
    "\n#{class_name},",
    after: "MODELS_IN_MENU = ["
  run "rubocop --autocorrect #{dashboard_file_path}", abort_on_failure: true
end

#add_other_locale_filesObject



78
79
80
81
82
83
84
# File 'lib/generators/trek/scaffold_generator.rb', line 78

def add_other_locale_files
  other_locales.each do |locale|
    @locale = locale.to_sym
    template "locales/scaffold/admin.other.yml", admin_locale_file(locale)
    template "locales/scaffold/model.other.yml", model_locale_file(locale)
  end
end

#add_policyObject



68
69
70
71
# File 'lib/generators/trek/scaffold_generator.rb', line 68

def add_policy
  template "policies/admin/scaffold_policy.rb",
    "app/policies/admin/#{file_path}_policy.rb"
end

#add_routesObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/generators/trek/scaffold_generator.rb', line 52

def add_routes
  route_code = <<~RUBY.chomp
    resources :#{plural_name}, except: :show do
      # Uncomment if reorderable
      # post :reorder, on: :collection
    end
  RUBY

  if model_namespaces.any?
    # Wrap resources in nested namespaces for namespaced models
    model_namespaces.each { |ns| route_code = "namespace :#{ns} do\n  #{route_code.gsub("\n", "\n  ")}\nend" }
  end

  route route_code, namespace: :admin
end

#cleanupObject



100
101
102
103
104
105
# File 'lib/generators/trek/scaffold_generator.rb', line 100

def cleanup
  return if I18n.available_locales.include?(:en)

  FileUtils.rm_f(model_locale_file(:en))
  FileUtils.rm_f(admin_locale_file(:en))
end

#create_controllerObject



33
34
35
36
# File 'lib/generators/trek/scaffold_generator.rb', line 33

def create_controller
  template "controllers/admin/scaffold_controller.rb",
    "app/controllers/admin/#{namespaced_plural}_controller.rb"
end

#create_modelObject



18
19
20
# File 'lib/generators/trek/scaffold_generator.rb', line 18

def create_model
  generate :model, class_name, attributes.map { |a| "#{a.name}:#{a.type}" }
end

#create_viewsObject



38
39
40
41
42
43
# File 'lib/generators/trek/scaffold_generator.rb', line 38

def create_views
  %w[index edit new _form show].each do |view|
    template "views/admin/scaffold/#{view}.html.slim",
      "app/views/admin/#{namespaced_plural}/#{view}.html.slim"
  end
end

#inject_trek_to_modelObject



22
23
24
25
26
27
# File 'lib/generators/trek/scaffold_generator.rb', line 22

def inject_trek_to_model
  inject_into_class model_file_path,
    class_name,
    model_injection
  run "rubocop --autocorrect #{model_file_path}", abort_on_failure: true
end

#translate_other_locale_filesObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/generators/trek/scaffold_generator.rb', line 86

def translate_other_locale_files
  unless ENV["DEEPL_AUTH_KEY"]
    Rails.logger.debug "DEEPL_AUTH_KEY not found. Skipping translation."
    return
  end

  if other_locales.empty?
    Rails.logger.debug "No other locales found. Skipping translation."
    return
  end

  system "i18n-tasks translate-missing --backend=deepl --from=en #{other_locales.map(&:to_s).join(" ")}"
end