Sha256: e3eb41e3c14d9e308eb86dd24568d0af32ea2dd4ec4b6ee29cf5dd44e1613752
Contents?: true
Size: 1.99 KB
Versions: 13
Compression:
Stored size: 1.99 KB
Contents
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 execute_devise_installer generate 'devise:install' end def execute_dta_installer generate 'devise_token_auth:install', "#{user_class} #{mount_path}" end def mount_resource_route routes_file = 'config/routes.rb' routes_path = File.join(destination_root, routes_file) gem_helper = 'mount_graphql_devise_for' gem_route = "#{gem_helper} '#{user_class}', at: '#{mount_path}'" dta_route = "mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'" file_start = 'Rails.application.routes.draw do' if File.exist?(routes_path) current_route = parse_file_for_line(routes_path, gem_route) if current_route.present? say_status('skipped', "Routes already exist for #{user_class} at #{mount_path}") else current_dta_route = parse_file_for_line(routes_path, dta_route) if current_dta_route.present? replace_line(routes_path, dta_route, gem_route) else insert_text_after_line(routes_path, file_start, gem_route) end end else say_status('skipped', "#{routes_file} not found. Add \"#{gem_route}\" 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 replace_line(filename, old_line, new_line) gsub_file(filename, /(#{Regexp.escape(old_line)})/mi, " #{new_line}") end def parse_file_for_line(filename, str) match = false File.open(filename) do |f| f.each_line do |line| match = line if line =~ /(#{Regexp.escape(str)})/mi end end match end end end
Version data entries
13 entries across 13 versions & 1 rubygems