From f7d15b2120f19af02a705154d0f76854edcdfdfc Mon Sep 17 00:00:00 2001 From: Lucy Fu Date: Wed, 24 May 2017 11:43:17 -0400 Subject: [PATCH 1/2] Add new class Dialog::ContainerTemplateServiceDialog. https://www.pivotaltracker.com/story/show/145899183 --- .../container_template_service_dialog.rb | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 app/models/dialog/container_template_service_dialog.rb 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 From 05c9598ed421c48e6923c33801508e7bb1ec1c81 Mon Sep 17 00:00:00 2001 From: Lucy Fu Date: Wed, 24 May 2017 11:44:09 -0400 Subject: [PATCH 2/2] Add specs for new class Dialog::ContainerTemplateServiceDialog. https://www.pivotaltracker.com/story/show/145899183 --- spec/factories/container_template.rb | 4 ++ .../factories/container_template_parameter.rb | 4 ++ .../container_template_service_dialog_spec.rb | 59 +++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 spec/factories/container_template.rb create mode 100644 spec/factories/container_template_parameter.rb create mode 100644 spec/models/dialog/container_template_service_dialog_spec.rb 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