Skip to content

Commit

Permalink
Merge pull request #14668 from durandom/dialog_seed
Browse files Browse the repository at this point in the history
allow seeding of dialogs from plugins
  • Loading branch information
bdunne authored Apr 11, 2017
2 parents c4f9ba2 + 833f76d commit 03239d7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
13 changes: 10 additions & 3 deletions app/models/dialog.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class Dialog < ApplicationRecord
DIALOG_DIR = Rails.root.join("product/dialogs/service_dialogs")
DIALOG_DIR_CORE = 'product/dialogs/service_dialogs'.freeze
DIALOG_DIR_PLUGIN = 'content/service_dialogs'.freeze

# The following gets around a glob symbolic link issue
ALL_YAML_FILES = DIALOG_DIR.join("{,*/**/}*.{yaml,yml}")
YAML_FILES_PATTERN = "{,*/**/}*.{yaml,yml}".freeze

has_many :dialog_tabs, -> { order :position }, :dependent => :destroy
validate :validate_children
Expand All @@ -25,9 +26,15 @@ class Dialog < ApplicationRecord
def self.seed
dialog_import_service = DialogImportService.new

Dir.glob(ALL_YAML_FILES).each do |file|
Dir.glob(Rails.root.join(DIALOG_DIR_CORE, YAML_FILES_PATTERN)).each do |file|
dialog_import_service.import_all_service_dialogs_from_yaml_file(file)
end

Vmdb::Plugins.instance.registered_provider_plugins.each do |plugin|
Dir.glob(plugin.root.join(DIALOG_DIR_PLUGIN, YAML_FILES_PATTERN)).each do |file|
dialog_import_service.import_all_service_dialogs_from_yaml_file(file)
end
end
end

def each_dialog_field(&block)
Expand Down
61 changes: 38 additions & 23 deletions spec/models/dialog_spec.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,57 @@
describe Dialog do
describe ".seed" do
let(:dialog_import_service) { double("DialogImportService") }
let(:test_file_path) { Rails.root.join("spec/fixtures/files/dialogs") }
let(:all_yaml_files) { test_file_path.join("{,*/**/}*.{yaml,yml}") }
let(:test_file_path) { "spec/fixtures/files/dialogs" }

before do
allow(DialogImportService).to receive(:new).and_return(dialog_import_service)
allow(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file)
end

it "delegates to the dialog import service with a file in the default directory" do
Dialog.with_constants(:DIALOG_DIR => test_file_path, :ALL_YAML_FILES => all_yaml_files) do
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
test_file_path.join("seed_test.yaml").to_path)
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
test_file_path.join("seed_test.yml").to_path)
Dialog.seed
end
stub_const('Dialog::DIALOG_DIR_CORE', test_file_path)
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
Rails.root.join(test_file_path, "seed_test.yaml").to_path
)
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
Rails.root.join(test_file_path, "seed_test.yml").to_path
)
described_class.seed
end

it "delegates to the dialog import service with a file in a sub directory" do
Dialog.with_constants(:DIALOG_DIR => test_file_path, :ALL_YAML_FILES => all_yaml_files) do
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
test_file_path.join("service_dialogs/service_seed_test.yaml").to_path)
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
test_file_path.join("service_dialogs/service_seed_test.yml").to_path)
Dialog.seed
end
stub_const('Dialog::DIALOG_DIR_CORE', test_file_path)
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
Rails.root.join(test_file_path, "service_dialogs", "service_seed_test.yaml").to_path
)
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
Rails.root.join(test_file_path, "service_dialogs", "service_seed_test.yml").to_path
)
described_class.seed
end

it "delegates to the dialog import service with a symlinked file" do
Dialog.with_constants(:DIALOG_DIR => test_file_path, :ALL_YAML_FILES => all_yaml_files) do
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
test_file_path.join("service_dialog_symlink/service_seed_test.yaml").to_path)
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
test_file_path.join("service_dialog_symlink/service_seed_test.yml").to_path)
Dialog.seed
end
stub_const('Dialog::DIALOG_DIR_CORE', test_file_path)
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
Rails.root.join(test_file_path, "service_dialog_symlink", "service_seed_test.yaml").to_path
)
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
Rails.root.join(test_file_path, "service_dialog_symlink", "service_seed_test.yml").to_path
)
described_class.seed
end

it "seed files from plugins" do
mock_engine = double(:root => Rails.root)
expect(Vmdb::Plugins.instance).to receive(:registered_provider_plugins).and_return([mock_engine])

stub_const('Dialog::DIALOG_DIR_PLUGIN', test_file_path)
stub_const('Dialog::DIALOG_DIR_CORE', 'non-existent-dir')
expect(dialog_import_service).to receive(:import_all_service_dialogs_from_yaml_file).with(
Rails.root.join(test_file_path, "seed_test.yaml").to_path
)
expect(mock_engine).to receive(:root)
described_class.seed
end
end

Expand Down

0 comments on commit 03239d7

Please sign in to comment.