Class: Olivander::ScaffoldGenerator

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

Instance Method Summary collapse

Instance Method Details

#create_viewsObject



23
24
25
26
# File 'lib/generators/olivander/scaffold_generator.rb', line 23

def create_views
  # template "views/_form.html.haml", File.join("app/views", controller_file_path, '_form.html.haml')
  # template "views/_model.html.haml", File.join("app/views", controller_file_path, "_#{file_name}.html.haml")
end

#handle_menuObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/generators/olivander/scaffold_generator.rb', line 43

def handle_menu
  say_status :menu, "Handling menu option", :yellow
  return unless options[:menu]

  target = 'app/services/menu_builder.rb'
  unless File.exist?(target)
    say_status :error, "#{target} not found, skipping menu injection", :red
    return
  end

  # The exact line we want to insert (note trailing comma)
  new_line = <<~RUBY.indent(6)
        builder.build_menu_item(key: "#{file_name.pluralize}", url: builder.#{file_name.pluralize}_path),
  RUBY

  file_contents = File.read(target)
  # avoid inserting duplicates
  if file_contents.include?(new_line.strip)
    say_status :skip, "menu item already present in #{target}", :yellow
    return
  end

  # Insert before the closing bracket of the array (a line that only contains optional whitespace and `]`)
  inject_into_file target,
                   new_line,
                   before: /^\s*\]\s*$/m,
                   verbose: true,
                   force: false

  say_status :done, "Inserted menu item into #{target}", :green
end

#handle_routeObject

Make an entry in Rails routing file config/routes.rb

route "root 'welcome#index'"
route "root 'admin#index'", namespace: :admin


32
33
34
35
36
37
38
39
40
41
# File 'lib/generators/olivander/scaffold_generator.rb', line 32

def handle_route
  return if options[:actions].present?

  use_route_builder = File.exist?(Rails.root.join("app/services/route_builder.rb"))
  if use_route_builder
    route_for_route_builder
  else
    route "resources :#{file_name.pluralize}", namespace: regular_class_path
  end
end