Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
.ruby-gemset

.env
/spec/tmp/config/routes.rb
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Rails:
Enabled: true

AllCops:
TargetRubyVersion: 2.2
TargetRubyVersion: 2.3
Copy link
Copy Markdown
Member

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.1

Copy link
Copy Markdown
Member

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

DisplayCopNames: true
Exclude:
- bin/**/*
Expand Down
1 change: 1 addition & 0 deletions graphql_devise.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rubocop-rspec'
spec.add_development_dependency 'sqlite3', '~> 1.3'
spec.add_development_dependency 'github_changelog_generator'
spec.add_development_dependency 'generator_spec'
end
60 changes: 60 additions & 0 deletions lib/generators/graphql_devise/install_generator.rb
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
28 changes: 28 additions & 0 deletions spec/generators/graphql_devise/install_generator_spec.rb
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
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# Add additional requires below this line. Rails is not loaded until this point!
require 'factory_bot'
require 'faker'
require 'generator_spec'

# Load RSpec helpers.
Dir[File.join(ENGINE_ROOT, 'spec/support/**/*.rb')].each { |f| require f }
Expand Down