-
Notifications
You must be signed in to change notification settings - Fork 45
Add routes generator #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d4247c6
Add routes generator
8fd5120
Merge branch 'master' into add-generators
00dav00 69e379b
Refactor route generator
bb6d597
Execute devise initializer
b8b6954
Merge branch 'master' into add-generators
00dav00 2a39d0d
Replace generated DTA route with gqld route
8b8dc15
Update readme
f26ce75
Test mark down anchors
ff03f86
Remove generators from future work
c6de340
Skip calling devise and dta generators on specs
108ad7d
Apply code review suggestions
042fc24
Comment generator specs
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,3 +23,4 @@ | |
| .ruby-gemset | ||
|
|
||
| .env | ||
| /spec/tmp/config/routes.rb | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| module GraphqlDevise | ||
| class InstallGenerator < ::Rails::Generators::Base | ||
| source_root File.expand_path('templates', __dir__) | ||
|
|
||
| argument :user_class, type: :string, default: 'User' | ||
| argument :mount_path, type: :string, default: 'auth' | ||
|
|
||
| def mount_resource_route | ||
| f = 'config/routes.rb' | ||
| str = "mount_graphql_devise_for '#{user_class}', at: '#{mount_path}'" | ||
| routes_file = File.join(destination_root, f) | ||
|
|
||
| if File.exist?(routes_file) | ||
| line = parse_file_for_line(f, 'mount_graphql_devise_for') | ||
|
|
||
| if line | ||
| existing_user_class = true | ||
| else | ||
| line = 'Rails.application.routes.draw do' | ||
| existing_user_class = false | ||
| end | ||
|
|
||
| if parse_file_for_line(f, str) | ||
| say_status('skipped', "Routes already exist for #{user_class} at #{mount_path}") | ||
| else | ||
| insert_text_after_line(f, line, str) | ||
|
|
||
| if existing_user_class | ||
| scoped_routes = ''\ | ||
| "as :#{user_class.underscore} do\n"\ | ||
| " # Define routes for #{user_class} within this block.\n"\ | ||
| " end\n" | ||
| insert_text_after_line(f, str, scoped_routes) | ||
| end | ||
| end | ||
| else | ||
| say_status('skipped', "config/routes.rb not found. Add \"mount_graphql_devise_for '#{user_class}', at: '#{mount_path}'\" to your routes file.") | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def insert_text_after_line(filename, line, str) | ||
| gsub_file filename, /(#{Regexp.escape(line)})/mi do |match| | ||
| "#{match}\n #{str}" | ||
| end | ||
| end | ||
|
|
||
| def parse_file_for_line(filename, str) | ||
| match = false | ||
|
|
||
| File.open(File.join(destination_root, filename)) do |f| | ||
| f.each_line do |line| | ||
| match = line if line =~ /(#{Regexp.escape(str)})/mi | ||
| end | ||
| end | ||
| match | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Generators are not automatically loaded by Rails | ||
| require 'rails_helper' | ||
| require 'fileutils' | ||
| require 'generators/graphql_devise/install_generator' | ||
|
|
||
| RSpec.describe GraphqlDevise::InstallGenerator, type: :generator do | ||
| destination File.expand_path("../../../tmp", __FILE__) | ||
| arguments %w(User auth) | ||
|
|
||
| before do | ||
| prepare_destination | ||
| FileUtils::mkdir(config_path) | ||
| File.open(routes_path, "w") do |f| | ||
| f.write('Rails.application.routes.draw do') | ||
| f.write("\nend") | ||
| end | ||
| run_generator | ||
| end | ||
|
|
||
| let(:config_path) { "#{destination_root}/config" } | ||
| let(:routes_path) { "#{config_path}/routes.rb" } | ||
| let(:routes_content) { File.read(routes_path) } | ||
|
|
||
| it 'add the routes to config/routes.rb' do | ||
| generator_added_route = / mount_graphql_devise_for 'User', at: 'auth'/ | ||
| expect(routes_content).to match(generator_added_route) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still support ruby 2.2, if it's giving you an error locally, you have to install rubocop
0.68.1There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know support was dropped for 2.2 but I don't see a reason to make the gem incompatible in the even someone might still be using that ruby version. But do check, if any of our dependencies already dropped support for 2.2 let's remove it. You would also need to change the version in the gemspec file