Skip to content

Commit

Permalink
Ignore non ActiveRecord sub classes - fixes #44, #47, #48
Browse files Browse the repository at this point in the history
  • Loading branch information
markburns committed Apr 28, 2016
1 parent 65cf783 commit 137dae1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
43 changes: 30 additions & 13 deletions lib/active_mocker/generate.rb
Expand Up @@ -12,13 +12,15 @@ def initialize
def call
clean_up
progress_init
models_paths.each do |file|
model_name = model_name(file)
model = get_model_const(model_name)

active_record_models_with_files.each do |model, file|
model_name = model.name

mock_file_name = "#{model_name.underscore}_#{config.mock_append_name.underscore}.rb"
mock_file_path = File.join(Config.mock_dir, mock_file_name)
assure_dir_path_exists(mock_file_path)
schema_scrapper = ActiveRecordSchemaScrapper.new(model: model)

File.open(mock_file_path, 'w') do |file_out|
begin
result = create_mock(file, file_out, schema_scrapper)
Expand All @@ -28,21 +30,30 @@ def call
rescue_clean_up(e, file_out, model_name)
end
end

progress.increment
end

display_errors.display_errors
self
end

def get_model_const(model_name)
model_name.constantize
constant = model_name.constantize
return unless constant.ancestors.include?(ActiveRecord::Base)
constant
rescue StandardError, LoadError => e
display_errors.wrap_an_exception(e, model_name)
nil
end

def active_record_models
@active_record_models ||= active_record_models_with_files.map(&:first)
end

private

attr_reader :display_errors
attr_reader :display_errors, :progress

def create_mock(file, file_out, schema_scrapper)
MockCreator.new(file: File.open(file),
Expand All @@ -54,9 +65,11 @@ def create_mock(file, file_out, schema_scrapper)
end

OtherErrors = Struct.new(:successful?)

def collect_errors(mock_file_path, create_mock_errors, schema_scrapper, model_name)
display_errors.wrap_errors(schema_scrapper.associations.errors, model_name, type: :associations)
display_errors.wrap_errors(schema_scrapper.attributes.errors, model_name, type: :attributes)

if create_mock_errors.present? || schema_scrapper.attributes.errors.any? { |e| e.level == :error }
display_errors.failed_models << model_name
File.delete(mock_file_path) if File.exists?(mock_file_path)
Expand All @@ -74,26 +87,30 @@ def rescue_clean_up(e, file_out, model_name)
display_errors.wrap_an_exception(e, model_name)
end

def model_name(file)
FilePathToRubyClass.new(base_path: config.model_dir, class_path: file).to_s
def progress_init
@progress = config.progress_class.create(active_record_models.count)
end

def model_names
@model_names ||= models_paths.map { |p| model_name(p) }
@model_names ||= active_record_models.map(&:name)
end

def progress
@progress
end

def progress_init
@progress = config.progress_class.create(models_paths.count)
def active_record_models_with_files
@active_record_models_with_files ||= models_paths.map do |file|
model = get_model_const(model_name_for(file))
[model, file] if model
end.compact
end

def models_paths
@models_paths ||= Dir.glob(config.single_model_path || File.join(config.model_dir, "**/*.rb"))
end

def model_name_for(file)
FilePathToRubyClass.new(base_path: config.model_dir, class_path: file).to_s
end

def assure_dir_path_exists(file)
unless File.exists?(File.dirname(file))
FileUtils::mkdir_p(File.dirname(file))
Expand Down
27 changes: 25 additions & 2 deletions spec/lib/active_mocker/generate_spec.rb
Expand Up @@ -9,11 +9,14 @@

before do
ActiveMocker::Config.set do |config|
config.model_dir = File.join(File.expand_path("../", __FILE__))
config.model_dir = File.expand_path("../", __FILE__)
config.mock_dir = not_found_dir
config.error_verbosity = 0
config.progress_bar = false
end
stub_const("ActiveRecord::Base", class_double("ActiveRecord::Base"))
stub_const("Model", class_double("Model", ancestors: [ActiveRecord::Base]))
stub_const("SomeNamespace::SomeModule", class_double("SomeNamespace::SomeModule", ancestors: [Module]))
end

before do
Expand Down Expand Up @@ -73,7 +76,6 @@
let(:models_dir) { File.join(File.expand_path("../../", __FILE__), "models") }

before do
stub_const("ActiveRecord::Base", class_double("ActiveRecord::Base"))
FileUtils::mkdir_p(not_found_dir)
File.open(old_mock_path, "w") { |w| w.write "" }
File.open(current_mock_path, "w") { |w| w.write "" }
Expand All @@ -95,11 +97,31 @@
end
end

describe "#active_record_models" do
let(:models_dir) { File.join(File.expand_path("../../", __FILE__), "models") }

before do
ActiveMocker::Config.model_dir = models_dir
end

context "with some non ActiveRecord subclasses" do
before do
stub_const("NonActiveRecordModel", class_double("NonActiveRecordModel", ancestors: [Object]))
end

it "ignores non ActiveRecord subclasses" do
result = described_class.new.call
expect(result.active_record_models).to eq [Model]
end
end
end

describe "ActiveMocker::Config.disable_modules_and_constants" do

before do
ActiveMocker::Config.disable_modules_and_constants = set_to
ActiveMocker::Config.progress_bar = false
ActiveMocker::Config.model_dir = File.join(File.expand_path("../../", __FILE__), "models")
ActiveMocker::Config.single_model_path = File.join(File.expand_path("../../", __FILE__), "models/model.rb")
end

Expand All @@ -126,6 +148,7 @@

before do
ActiveMocker::Config.progress_bar = false
ActiveMocker::Config.model_dir = File.join(File.expand_path("../../", __FILE__), "models")
ActiveMocker::Config.single_model_path = File.join(File.expand_path("../../models", __FILE__), "model.rb")
end

Expand Down
2 changes: 2 additions & 0 deletions spec/lib/models/non_active_record_model.rb
@@ -0,0 +1,2 @@
class NonActiveRecordModel
end
7 changes: 7 additions & 0 deletions spec/lib/models/some_namespace/some_module.rb
@@ -0,0 +1,7 @@
module SomeNamespace
module SomeModule
def some_method
"some return value"
end
end
end

0 comments on commit 137dae1

Please sign in to comment.