Skip to content

Commit

Permalink
rails generator fix on zeitwerk (#1714)
Browse files Browse the repository at this point in the history
* generator file structure revert

* removed extra scope

* corrected generators spec

* ignored railtie and rails dir

* corrected railtie spec

* moved rails specific loading to railtie

* corrected spec and load order

* code cleanup
  • Loading branch information
mrhardikjoshi authored Dec 29, 2023
1 parent dba8fa2 commit 43951b0
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 75 deletions.
2 changes: 1 addition & 1 deletion docs/activegraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
END
end

gsub_file 'config/application.rb', "require 'rails'", ''
gsub_file 'config/application.rb', 'require "rails"', ''

generator = %q[
# Enable ActiveGraph generators, e.g: rails generate model Admin --parent User
Expand Down
16 changes: 2 additions & 14 deletions lib/active_graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,9 @@
require 'sorted_set'
require 'yaml'

if defined?(Rails)
# Need the action_dispatch railtie to have action_dispatch.rescue_responses initialized correctly
require 'action_dispatch/railtie'
require 'rails/generators'
require 'rails/generators/active_model'
require 'rails/generators/named_base'
require 'rails/railtie'
end

loader = Zeitwerk::Loader.for_gem
unless defined?(Rails)
loader.ignore(File.expand_path('active_graph/generators', __dir__))
loader.ignore(File.expand_path('active_graph/railtie.rb', __dir__))
end
loader.ignore(File.expand_path('rails', __dir__))
loader.ignore(File.expand_path('active_graph/railtie.rb', __dir__))
loader.inflector.inflect("ansi" => "ANSI")
loader.setup
# loader.eager_load
Expand All @@ -49,6 +38,5 @@
Neo4j::Driver::Types::Node.wrapper_callback(&ActiveGraph::Node::Wrapping.method(:wrapper))
Neo4j::Driver::Types::Relationship.wrapper_callback(&ActiveGraph::Relationship::Wrapping.method(:wrapper))
SecureRandom.singleton_class.prepend ActiveGraph::SecureRandomExt
Rails::Generators::GeneratedAttribute.include ActiveGraph::Generators::GeneratedAttribute if defined?(Rails)

load 'active_graph/tasks/migration.rake'
33 changes: 0 additions & 33 deletions lib/active_graph/generators/active_model.rb

This file was deleted.

17 changes: 0 additions & 17 deletions lib/active_graph/generators/generated_attribute.rb

This file was deleted.

11 changes: 11 additions & 0 deletions lib/active_graph/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
require 'active_graph'

if defined?(Rails)
# Need the action_dispatch railtie to have action_dispatch.rescue_responses initialized correctly
require 'action_dispatch/railtie'
require 'rails/generators'
require 'rails/generators/active_model'
require 'rails/generators/named_base'
require 'rails/railtie'
require File.expand_path('../rails/generators/migration_helper.rb', __dir__)
Rails::Generators::GeneratedAttribute.include ActiveGraph::Generators::GeneratedAttribute
end

module ActiveGraph
class Railtie < ::Rails::Railtie
def empty_config
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require_relative '../../migration_helper'
require_relative '../../source_path_helper'

module ActiveGraph
module Generators
class MigrationGenerator < ::Rails::Generators::NamedBase
include ::ActiveGraph::Generators::SourcePathHelper
include ::ActiveGraph::Generators::MigrationHelper
include ActiveGraph::Generators::SourcePathHelper
include ActiveGraph::Generators::MigrationHelper

def create_migration_file
migration_template 'migration.erb'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require_relative '../../migration_helper'
require_relative '../../source_path_helper'

class ActiveGraph::Generators::ModelGenerator < Rails::Generators::NamedBase #:nodoc:
include ::ActiveGraph::Generators::SourcePathHelper
include ::ActiveGraph::Generators::MigrationHelper
include ActiveGraph::Generators::SourcePathHelper
include ActiveGraph::Generators::MigrationHelper

argument :attributes, type: :array, default: [], banner: 'field:type field:type'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require_relative '../../migration_helper'
require_relative '../../source_path_helper'

module ActiveGraph
module Generators
class UpgradeV8Generator < ::Rails::Generators::Base
include ::ActiveGraph::Generators::SourcePathHelper
include ::ActiveGraph::Generators::MigrationHelper
include ActiveGraph::Generators::SourcePathHelper
include ActiveGraph::Generators::MigrationHelper

def create_upgrade_v8_file
@schema = load_all_models_schema!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module ActiveGraph
module Generators #:nodoc:
end
end

module ActiveGraph::Generators::MigrationHelper
extend ActiveSupport::Concern

Expand Down Expand Up @@ -44,3 +49,55 @@ def migration_template(template_name, prefix = '')
FileUtils.rm(real_file_name) if @behavior == :revoke
end
end

class ActiveGraph::Generators::ActiveModel < Rails::Generators::ActiveModel #:nodoc:
def self.all(klass)
"#{klass}.all"
end

def self.find(klass, params = nil)
"#{klass}.find(#{params})"
end

def self.build(klass, params = nil)
if params
"#{klass}.new(#{params})"
else
"#{klass}.new"
end
end

def save
"#{name}.save"
end

def update_attributes(params = nil)
"#{name}.update_attributes(#{params})"
end

def errors
"#{name}.errors"
end

def destroy
"#{name}.destroy"
end
end

module ActiveGraph
module Generators
module GeneratedAttribute #:nodoc:
def type_class
case type.to_s.downcase
when 'any' then 'any'
when 'datetime' then 'DateTime'
when 'date' then 'Date'
when 'integer', 'number', 'fixnum' then 'Integer'
when 'float' then 'Float'
else
'String'
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ActiveGraph::Generators::SourcePathHelper
module ClassMethods
def source_root
@_neo4j_source_root ||= File.expand_path(File.join(File.dirname(__FILE__),
generator_name, 'templates'))
'active_graph', generator_name, 'templates'))
end
end
end
11 changes: 8 additions & 3 deletions spec/e2e/generators_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
require 'rails/generators'
require 'rails/generators/active_graph/model/model_generator'
require 'rails/generators/active_graph/migration/migration_generator'
require 'rails/generators/active_graph/upgrade_v8/upgrade_v8_generator'

describe 'Generators' do
around do |example|
Timecop.freeze(Time.parse('1990-12-10 00:00:00 -0000')) { example.run }
end

describe ActiveGraph::Generators::ModelGenerator do
it 'has a `source_root`' do
expect(described_class.source_root).to include('active_graph/generators/model/templates')
expect(described_class.source_root).to include('rails/generators/active_graph/model/templates')
end

it 'creates a model and a migration file' do
Expand All @@ -17,7 +22,7 @@

describe ActiveGraph::Generators::MigrationGenerator do
it 'has a `source_root`' do
expect(described_class.source_root).to include('active_graph/generators/migration/templates')
expect(described_class.source_root).to include('rails/generators/active_graph/migration/templates')
end

it 'creates a migration file' do
Expand All @@ -38,7 +43,7 @@
end

it 'has a `source_root`' do
expect(described_class.source_root).to include('active_graph/generators/upgrade_v8/templates')
expect(described_class.source_root).to include('rails/generators/active_graph/upgrade_v8/templates')
end

it 'creates a migration file' do
Expand Down
2 changes: 2 additions & 0 deletions spec/e2e/railtie_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'active_graph/railtie'

module Rails
describe 'railtie' do
after(:context) { set_default_driver }
Expand Down

0 comments on commit 43951b0

Please sign in to comment.