next_rails_scaffold 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59d3d25e8b717552cb8ebd2a090c806de449f66b71c29463e22d41ca8ed85dcf
4
- data.tar.gz: bdf00a7fd490e2a87baa48adea598d28a5481a2cb9e730902d0e4080b21194f5
3
+ metadata.gz: abd99131dfb6cd8584a12c366b039b743168e744b41f471c902596e10b77e138
4
+ data.tar.gz: 9523b8f173ba5373678d946497a7344e36afbd11505187153eeb79d2223e6540
5
5
  SHA512:
6
- metadata.gz: 87d2348fcba0bd076632291330c078ab0f4db23a0730b64ee1b89e166f07e491983281cf8341d068878a96cf890990aca80b627a75038715f6317859018b747a
7
- data.tar.gz: 68647f776a9f0ba73148639c76ac9c117026f390e9d9810382b86548e4c82bdb6908740e532bdae47ff0f23a674907686b1ff743d9a5159bf79c8845d3d19627
6
+ metadata.gz: d630cd3b07ce3c38b0411638def67bf59f5e2c1d79fbcc358a2ba681cf8318ca7bce8d8ef3a6b2f6929ec51c3019a49bdcbac8b36300f95e5c7c3fa1de311d0a
7
+ data.tar.gz: 49a4fb1332d41bfe302c91ab814177e748ff152fa4ed0f3ad43154d73a30e8137ee7ab3e96c6fc638ad48b5119f5e2973ef5380e139269b7f0f41717469c9c9f
data/.editorconfig ADDED
@@ -0,0 +1,12 @@
1
+ # EditorConfig is awesome: https://EditorConfig.org
2
+
3
+ # top-most EditorConfig file
4
+ root = true
5
+
6
+ [*]
7
+ indent_style = space
8
+ indent_size = 2
9
+ end_of_line = lf
10
+ charset = utf-8
11
+ trim_trailing_whitespace = true
12
+ insert_final_newline = false
@@ -0,0 +1,3 @@
1
+ {
2
+ "editor.formatOnSave": true
3
+ }
data/README.md CHANGED
@@ -1,24 +1,61 @@
1
1
  # NextRails
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ The `next_rails_scaffold` gem is a powerful extension to the standard Ruby on Rails scaffold generator. It streamlines the development workflow by not only creating the backend structure with Rails but also automating the setup of a frontend directory using Next.js. Upon running the scaffold generator, this gem intelligently generates a Next.js application within the specified frontend directory.
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/next_rails`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ The generated Next.js app follows best practices, including a structured page routing system, ensuring that each resource created by the scaffold has its corresponding page and components. This integration enables developers to seamlessly transition between Rails backend and Next.js frontend development, fostering a cohesive and efficient development environment.
6
6
 
7
- ## Installation
7
+ Key Features:
8
+
9
+ - **Automatic Frontend Setup:** The gem automates the creation of a frontend directory within the Rails project, ready for Next.js development.
10
+ - **Page Routing Integration:** All scaffolded resources come with their own pages and components, organized using Next.js' page routing system.
11
+ - **Effortless Transition:** Developers can seamlessly switch between Rails backend and Next.js frontend development within the same project.
12
+ - **Boosted Productivity:** Accelerate development by eliminating the manual setup of frontend components and pages, allowing developers to focus on building features.
8
13
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
14
+ Integrate `next_rails_scaffold` into your Ruby on Rails projects to enjoy a streamlined, organized, and efficient full-stack development experience.
15
+
16
+ ## Installation
10
17
 
11
18
  Install the gem and add to the application's Gemfile by executing:
12
19
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
20
+ $ bundle add next_rails_scaffold
14
21
 
15
22
  If bundler is not being used to manage dependencies, install the gem by executing:
16
23
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
24
+ $ gem install next_rails_scaffold
18
25
 
19
26
  ## Usage
20
27
 
21
- TODO: Write usage instructions here
28
+ The `next_rails_scaffold` gem enhances the default Ruby on Rails scaffold generator by seamlessly integrating with Next.js, a React framework. This gem automates the process of scaffolding a Rails application along with a corresponding frontend directory containing a Next.js application. The generated Next.js app includes all necessary pages and components, leveraging the power of page routing for a smooth and organized development experience.
29
+
30
+ Example:
31
+
32
+ ```
33
+ bin/rails generate scaffold Post tile:string body:text
34
+ ```
35
+
36
+ This will create:
37
+
38
+ ```
39
+ app/
40
+ controllers/
41
+ posts_controller.rb
42
+ models/
43
+ post.rb
44
+ ...
45
+ frontend/
46
+ src
47
+ pages
48
+ posts
49
+ [id]
50
+ edit.js
51
+ index.js
52
+ _components
53
+ Post.js
54
+ PostForm.js
55
+ index.js
56
+ new.js
57
+ services.js
58
+ ```
22
59
 
23
60
  ## Development
24
61
 
@@ -28,7 +65,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
28
65
 
29
66
  ## Contributing
30
67
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/next_rails.
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/raphox/next_rails.
32
69
 
33
70
  ## License
34
71
 
@@ -0,0 +1,27 @@
1
+ Description:
2
+ The `next_rails_scaffold` gem enhances the default Ruby on Rails scaffold generator by seamlessly integrating with Next.js, a React framework. This gem automates the process of scaffolding a Rails application along with a corresponding frontend directory containing a Next.js application. The generated Next.js app includes all necessary pages and components, leveraging the power of page routing for a smooth and organized development experience.
3
+
4
+ Example:
5
+ bin/rails generate scaffold Post tile:string body:text
6
+
7
+ This will create:
8
+
9
+ app/
10
+ controllers/
11
+ posts_controller.rb
12
+ models/
13
+ post.rb
14
+ ...
15
+ frontend/
16
+ src
17
+ pages
18
+ posts
19
+ [id]
20
+ edit.js
21
+ index.js
22
+ _components
23
+ Post.js
24
+ PostForm.js
25
+ index.js
26
+ new.js
27
+ services.js
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NextRails
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ desc "Copy NextRails default files"
7
+ source_root File.expand_path("templates", __dir__)
8
+
9
+ def copy_config
10
+ template "config/initializers/next_rails.rb"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "next_rails"
4
+
5
+ NextRails.setup do |config|
6
+ config.generators do |g|
7
+ g.helper :next_rails
8
+ end
9
+ end
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ class NextRailsGenerator < Rails::Generators::NamedBase
5
+ source_root File.expand_path("templates", __dir__)
6
+
7
+ NODE_REQUIRED_VERSION = ">= 18.17.0"
8
+ YARN_VERSION = "1.22.19"
9
+ NEXT_VERSION = "14.0.2"
10
+ NODULES_MODULES = [
11
+ "@hookform/resolvers",
12
+ "@tanstack/react-query",
13
+ "axios",
14
+ "react-hook-form",
15
+ "zod"
16
+ ].freeze
17
+
18
+ argument :attributes, type: :array, default: [], banner: "field:type field:type"
19
+
20
+ def initialize(args, *options) # :nodoc:
21
+ super
22
+ self.attributes = shell.base.attributes
23
+ end
24
+
25
+ # Properly nests namespaces passed into a generator
26
+ #
27
+ # $ bin/rails generate resource admin/users/products
28
+ #
29
+ # should give you
30
+ #
31
+ # scope '/api' do
32
+ # namespace :admin do
33
+ # namespace :users do
34
+ # resources :products
35
+ # end
36
+ # end
37
+ # end
38
+ def add_resource_route
39
+ return if options[:actions].present?
40
+
41
+ route "resources :#{file_name.pluralize}", namespace: regular_class_path, scope: "/api"
42
+ end
43
+
44
+ # Check Javascript depencies and create a new Next.js project, install the the usefull packages and create the
45
+ # scaffold code for frontend application.
46
+ def create_frontend_project
47
+ check_node!
48
+ append_gitignore!
49
+
50
+ empty_directory "frontend"
51
+
52
+ inside("frontend") do
53
+ create_next_app!
54
+
55
+ install_hygen!
56
+ install_dependencies!
57
+
58
+ run("npx hygen generate scaffold #{name} #{mapped_attributes.join(" ")}")
59
+ run("yarn build")
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def check_node!
66
+ node_version = run("node --version", capture: true).gsub(/[^0-9.]/, "")
67
+
68
+ if Gem::Dependency.new("", NODE_REQUIRED_VERSION).match?("", node_version)
69
+ say "Your Node version is '#{node_version}'", :green
70
+ else
71
+ say_error "You need to have a Node version '#{NODE_REQUIRED_VERSION}'", :red
72
+ abort
73
+ end
74
+ end
75
+
76
+ def append_gitignore!
77
+ rows = <<~HEREDOC
78
+
79
+ # Ingoring node modules for Rails and Next.js projects
80
+ node_modules/
81
+ HEREDOC
82
+
83
+ append_to_file ".gitignore", rows
84
+ end
85
+
86
+ def create_next_app!
87
+ return if File.exist?("package.json")
88
+
89
+ run("npm install --global yarn@#{YARN_VERSION}")
90
+ run("yarn global add create-next-app@#{NEXT_VERSION}")
91
+ run("yarn create next-app . --no-app --src-dir --import-alias \"@/*\"")
92
+ end
93
+
94
+ def install_hygen!
95
+ return if Dir.exist?("_templates")
96
+
97
+ run("yarn add -D hygen hygen-add")
98
+ run("npx hygen-add next-rails-scaffold")
99
+ end
100
+
101
+ def install_dependencies!
102
+ run("yarn add #{NODULES_MODULES.join(" ")}")
103
+ end
104
+
105
+ def mapped_attributes
106
+ attributes.map { |attr| "#{attr.name}:#{attr.type}" }
107
+ end
108
+
109
+ # Make an entry in \Rails routing file <tt>config/routes.rb</tt>
110
+ #
111
+ # route "root 'welcome#index'"
112
+ # route "root 'admin#index'", namespace: :admin
113
+ # route "root 'admin#index'", namespace: :admin, scope: '/api'
114
+ def route(routing_code, namespace: nil, scope: nil)
115
+ namespace = Array(namespace)
116
+ namespace_pattern = route_namespace_pattern(namespace)
117
+ routing_code = namespace.reverse.reduce(routing_code) do |code, name|
118
+ "namespace :#{name} do\n#{rebase_indentation(code, 2)}end"
119
+ end
120
+
121
+ scope = Array(scope)
122
+ routing_code = scope.reverse.reduce(routing_code) do |code, name|
123
+ "scope '#{name}' do\n#{rebase_indentation(code, 2)}end"
124
+ end
125
+
126
+ log :route, routing_code
127
+
128
+ in_root do
129
+ if namespace_match = match_file("config/routes.rb", namespace_pattern)
130
+ base_indent, *, existing_block_indent = namespace_match.captures.compact.map(&:length)
131
+ existing_line_pattern = /^ {,#{existing_block_indent}}\S.+\n?/
132
+ routing_code = rebase_indentation(routing_code, base_indent + 2).gsub(existing_line_pattern, "")
133
+ namespace_pattern = /#{Regexp.escape namespace_match.to_s}/
134
+ end
135
+
136
+ inject_into_file "config/routes.rb", routing_code, after: namespace_pattern, verbose: false, force: false
137
+
138
+ if behavior == :revoke && namespace.any? && namespace_match
139
+ empty_block_pattern = /(#{namespace_pattern})((?:\s*end\n){1,#{namespace.size}})/
140
+ gsub_file "config/routes.rb", empty_block_pattern, verbose: false, force: true do |matched|
141
+ beginning, ending = empty_block_pattern.match(matched).captures
142
+ ending.sub!(/\A\s*end\n/, "") while !ending.empty? && beginning.sub!(/^ *namespace .+ do\n\s*\z/, "")
143
+ beginning + ending
144
+ end
145
+ end
146
+ end
147
+ end
148
+
149
+ def exit_on_failure?
150
+ true
151
+ end
152
+ end
153
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NextRails
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: next_rails_scaffold
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raphael Araújo
@@ -43,11 +43,17 @@ executables: []
43
43
  extensions: []
44
44
  extra_rdoc_files: []
45
45
  files:
46
+ - ".editorconfig"
46
47
  - ".rubocop.yml"
48
+ - ".vscode/settings.json"
47
49
  - CHANGELOG.md
48
50
  - LICENSE.txt
49
51
  - README.md
50
52
  - Rakefile
53
+ - lib/generators/next_rails/USAGE
54
+ - lib/generators/next_rails/install_generator.rb
55
+ - lib/generators/next_rails/templates/config/initializers/next_rails.rb
56
+ - lib/generators/rails/next_rails/next_rails_generator.rb
51
57
  - lib/next_rails.rb
52
58
  - lib/next_rails/version.rb
53
59
  - sig/next_rails.rbs
@@ -58,7 +64,7 @@ metadata:
58
64
  allowed_push_host: https://rubygems.org
59
65
  homepage_uri: https://github.com/raphox/next-rails#readme
60
66
  source_code_uri: https://github.com/raphox/next-rails
61
- changelog_uri: https://github.com/raphox/next-rails/CHANGELOG.md
67
+ changelog_uri: https://github.com/raphox/next-rails/blob/main/CHANGELOG.md
62
68
  post_install_message:
63
69
  rdoc_options: []
64
70
  require_paths:
@@ -74,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
80
  - !ruby/object:Gem::Version
75
81
  version: '0'
76
82
  requirements: []
77
- rubygems_version: 3.4.22
83
+ rubygems_version: 3.3.7
78
84
  signing_key:
79
85
  specification_version: 4
80
86
  summary: The `next_rails_scaffold` gem enhances the default Ruby on Rails scaffold