From 137dae1988825bbf1924acebb684b4ee461b3ec8 Mon Sep 17 00:00:00 2001 From: Mark Burns Date: Thu, 28 Apr 2016 10:17:23 +0900 Subject: [PATCH] Ignore non ActiveRecord sub classes - fixes #44, #47, #48 --- lib/active_mocker/generate.rb | 43 +++++++++++++------ spec/lib/active_mocker/generate_spec.rb | 27 +++++++++++- spec/lib/models/non_active_record_model.rb | 2 + spec/lib/models/some_namespace/some_module.rb | 7 +++ 4 files changed, 64 insertions(+), 15 deletions(-) create mode 100644 spec/lib/models/non_active_record_model.rb create mode 100644 spec/lib/models/some_namespace/some_module.rb diff --git a/lib/active_mocker/generate.rb b/lib/active_mocker/generate.rb index 33a2f054..e2208933 100644 --- a/lib/active_mocker/generate.rb +++ b/lib/active_mocker/generate.rb @@ -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) @@ -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), @@ -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) @@ -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)) diff --git a/spec/lib/active_mocker/generate_spec.rb b/spec/lib/active_mocker/generate_spec.rb index 24cb6fe2..7bd438ca 100644 --- a/spec/lib/active_mocker/generate_spec.rb +++ b/spec/lib/active_mocker/generate_spec.rb @@ -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 @@ -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 "" } @@ -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 @@ -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 diff --git a/spec/lib/models/non_active_record_model.rb b/spec/lib/models/non_active_record_model.rb new file mode 100644 index 00000000..f97d498c --- /dev/null +++ b/spec/lib/models/non_active_record_model.rb @@ -0,0 +1,2 @@ +class NonActiveRecordModel +end diff --git a/spec/lib/models/some_namespace/some_module.rb b/spec/lib/models/some_namespace/some_module.rb new file mode 100644 index 00000000..160505b6 --- /dev/null +++ b/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