Class: Rails::NextRailsScaffoldGenerator

Inherits:
Generators::NamedBase
  • Object
show all
Includes:
NextRailsScaffold::Actions
Defined in:
lib/generators/rails/next_rails_scaffold/next_rails_scaffold_generator.rb

Constant Summary collapse

NODE_REQUIRED_VERSION =
">= 18.18.0"
YARN_VERSION =
"4.5.3"
NEXT_VERSION =
"15.0.3"

Instance Method Summary collapse

Methods included from NextRailsScaffold::Actions

#route

Constructor Details

#initialize(args, *options) ⇒ NextRailsScaffoldGenerator

:nodoc:



15
16
17
18
# File 'lib/generators/rails/next_rails_scaffold/next_rails_scaffold_generator.rb', line 15

def initialize(args, *options) # :nodoc:
  super
  self.attributes = shell.base.attributes
end

Instance Method Details

#add_resource_routeObject

Properly nests namespaces passed into a generator

$ bin/rails generate resource admin/users/products

should give you

scope '/api' do
  namespace :admin do
    namespace :users do
      resources :products
    end
  end
end


33
34
35
36
37
# File 'lib/generators/rails/next_rails_scaffold/next_rails_scaffold_generator.rb', line 33

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

  route "resources :#{file_name.pluralize}", namespace: regular_class_path, scope: "/api"
end

#create_frontend_projectObject

Check Javascript depencies and create a new Next.js project. Install the the usefull packages and create the scaffold code for frontend application.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/generators/rails/next_rails_scaffold/next_rails_scaffold_generator.rb', line 41

def create_frontend_project
  return say_status :remove, "skip frontend folder", :yellow if shell.base.behavior == :revoke

  check_node!
  append_gitignore!

  empty_directory "frontend"

  inside("frontend") do
    create_next_app!
    install_hygen!

    run("npx hygen scaffold javascript #{name} #{mapped_attributes.join(" ")}")
    run("yarn build")
  end
end