Skip to content

Commit

Permalink
Merge pull request #15216 from lfu/container_template_dialog
Browse files Browse the repository at this point in the history
Add new class Dialog::ContainerTemplateServiceDialog.
  • Loading branch information
gmcculloug authored Jun 7, 2017
2 parents 3af0dbb + 05c9598 commit e73c175
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
48 changes: 48 additions & 0 deletions app/models/dialog/container_template_service_dialog.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions spec/factories/container_template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FactoryGirl.define do
factory :container_template do
end
end
4 changes: 4 additions & 0 deletions spec/factories/container_template_parameter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FactoryGirl.define do
factory :container_template_parameter do
end
end
59 changes: 59 additions & 0 deletions spec/models/dialog/container_template_service_dialog_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e73c175

Please sign in to comment.