diff --git a/app/models/dialog/container_template_service_dialog.rb b/app/models/dialog/container_template_service_dialog.rb new file mode 100644 index 00000000000..5c4833f9619 --- /dev/null +++ b/app/models/dialog/container_template_service_dialog.rb @@ -0,0 +1,48 @@ +class Dialog + class ContainerTemplateServiceDialog + def self.create_dialog(label, parameters) + new.create_dialog(label, parameters) + end + + # This dialog is to be used by a container template service + def create_dialog(label, parameters) + Dialog.new(:label => label, :buttons => "submit,cancel").tap do |dialog| + tab = dialog.dialog_tabs.build(:display => "edit", :label => "Basic Information", :position => 0) + add_parameters_group(tab, 0, parameters) + dialog.save! + end + end + + private + + def add_parameters_group(tab, position, parameters) + tab.dialog_groups.build( + :display => "edit", + :label => "Parameters", + :position => position + ).tap do |dialog_group| + parameters.each_with_index do |param, index| + add_parameter_field(param, dialog_group, index) + end + end + end + + def add_parameter_field(param, group, position) + options = { + :type => "DialogFieldTextBox", + :name => "param_#{param.name}", + :data_type => "string", + :display => "edit", + :required => param.required, + :default_value => param.value, + :label => param.name.tr("_", " ").titleize, + :description => param.description, + :position => position, + :dialog_group => group + } + options[:label] += " (Auto-generated if empty)" if param.generate == 'expression' + + group.dialog_fields.build(options) + end + end +end diff --git a/spec/factories/container_template.rb b/spec/factories/container_template.rb new file mode 100644 index 00000000000..5ee3254ad76 --- /dev/null +++ b/spec/factories/container_template.rb @@ -0,0 +1,4 @@ +FactoryGirl.define do + factory :container_template do + end +end diff --git a/spec/factories/container_template_parameter.rb b/spec/factories/container_template_parameter.rb new file mode 100644 index 00000000000..4fe17307a18 --- /dev/null +++ b/spec/factories/container_template_parameter.rb @@ -0,0 +1,4 @@ +FactoryGirl.define do + factory :container_template_parameter do + end +end diff --git a/spec/models/dialog/container_template_service_dialog_spec.rb b/spec/models/dialog/container_template_service_dialog_spec.rb new file mode 100644 index 00000000000..7569cf432de --- /dev/null +++ b/spec/models/dialog/container_template_service_dialog_spec.rb @@ -0,0 +1,59 @@ +describe Dialog::ContainerTemplateServiceDialog do + describe "#create_dialog" do + let(:container_template) { FactoryGirl.create(:container_template) } + let(:params) { [] } + before do + 3.times do |n| + params << FactoryGirl.create(:container_template_parameter, + :name => "name_#{n + 1}", + :value => "value_#{n + 1}", + :required => true) + end + end + + it "creates a dialog for a container template with parameters" do + container_template.container_template_parameters = params + + dialog = described_class.create_dialog("mydialog1", container_template.container_template_parameters) + expect(dialog).to have_attributes(:label => 'mydialog1', :buttons => "submit,cancel") + + tabs = dialog.dialog_tabs + expect(tabs.size).to eq(1) + assert_main_tab(tabs[0]) + end + + it "raises an error for a container template with no parameters" do + expect { described_class.create_dialog("mydialog2", container_template.container_template_parameters) } + .to raise_error(ActiveRecord::RecordInvalid) + end + end + + def assert_main_tab(tab) + assert_tab_attributes(tab) + + groups = tab.dialog_groups + expect(groups.size).to eq(1) + + assert_parameters_group(groups[0]) + end + + def assert_tab_attributes(tab) + expect(tab).to have_attributes(:label => "Basic Information", :display => "edit") + end + + def assert_field(field, clss, attributes) + expect(field).to be_kind_of clss + expect(field).to have_attributes(attributes) + end + + def assert_parameters_group(group) + expect(group).to have_attributes(:label => "Parameters", :display => "edit") + + fields = group.dialog_fields + expect(fields.size).to eq(3) + + assert_field(fields[0], DialogFieldTextBox, :name => "param_#{params[0].name}", :default_value => params[0].value, :data_type => 'string') + assert_field(fields[1], DialogFieldTextBox, :name => "param_#{params[1].name}", :default_value => params[1].value, :data_type => 'string') + assert_field(fields[2], DialogFieldTextBox, :name => "param_#{params[2].name}", :default_value => params[2].value, :data_type => 'string') + end +end