Skip to content

Commit

Permalink
Merge branch 'master' into scalar-element-as-hash
Browse files Browse the repository at this point in the history
* master:
  Fix form title
  FormController receives the Formula instance
  Validate minItems and maxItems
  Unify form.yml fixture
  • Loading branch information
imobachgs committed Feb 5, 2019
2 parents c752b8b + dd99997 commit fe2318c
Show file tree
Hide file tree
Showing 23 changed files with 277 additions and 151 deletions.
5 changes: 3 additions & 2 deletions src/lib/y2configuration_management/clients/test_formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def run
pillar_file = "test/fixtures/pillar/test-formula.sls"
pillar = Y2ConfigurationManagement::Salt::Pillar.new(data: {}, path: pillar_file)
pillar.load
formula = Salt::Formula.new(Pathname.new("test/fixtures"), pillar)
controller = Salt::FormController.new(formula.form, pillar)
formula_path = Pathname.pwd.join("test", "fixtures", "formulas-ng", "test-formula")
formula = Salt::Formula.new(formula_path, pillar)
controller = Salt::FormController.new(formula)
controller.show_main_dialog
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/lib/y2configuration_management/salt/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def build(locator)
elements = scalar ? [form_element] : form_element.elements
widgets = Array(elements).map { |e| build_element(e, root_locator) }
Y2ConfigurationManagement::Widgets::Form.new(
widgets, controller, scalar: scalar, title: form_element.name
widgets, controller, scalar: scalar
)
end

Expand Down
35 changes: 22 additions & 13 deletions src/lib/y2configuration_management/salt/form_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ class FormController

# Constructor
#
# @param form [Y2ConfigurationManagement::Salt::Form]
# @param pillar [Y2ConfigurationManagement::Salt::Pillar]
def initialize(form, pillar)
@form = form
@pillar = pillar
data = FormData.from_pillar(form, pillar)
# @param formula [Y2ConfigurationManagement::Salt::Formula]
def initialize(formula)
@formula = formula
data = FormData.from_pillar(formula.form, formula.pillar)
@state = FormControllerState.new(data)
end

Expand All @@ -71,7 +69,7 @@ def show_main_dialog
Yast::Wizard.CreateDialog
ret = Yast::CWM.show(
HBox(form_widget),
caption: form.root.name, next_handler: method(:next_handler)
caption: formula.id, next_handler: method(:next_handler)
)
state.close_form
ret
Expand Down Expand Up @@ -119,15 +117,12 @@ def update_visibility

private

# @return [Form]
attr_reader :form

# @return [Pillar]
attr_reader :pillar

# @return [State]
attr_reader :state

# @return [Formula]
attr_reader :formula

# Returns the form builder
#
# @return [Y2ConfigurationManagement::Salt::FormBuilder]
Expand Down Expand Up @@ -218,6 +213,20 @@ def new_item_locator_for_action(action, relative_locator)
def form_data
@state.form_data
end

# Returns the Salt form
#
# @return [Form]
def form
formula.form
end

# Returns the current pillar
#
# @return [Pillar]
def pillar
formula.pillar
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def sequence_hash
#
# @param formula [Formula]
def configure_formula(formula)
controller = FormController.new(formula.form, formula.pillar)
controller = FormController.new(formula)
controller.show_main_dialog
end
end
Expand Down
33 changes: 33 additions & 0 deletions src/lib/y2configuration_management/widgets/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
require "y2configuration_management/widgets/visibility_switcher"
require "y2configuration_management/widgets/salt_visibility_switcher"

Yast.import "Report"

module Y2ConfigurationManagement
# This module contains the widgets which are used to display forms for Salt formulas
module Widgets
Expand Down Expand Up @@ -117,6 +119,12 @@ def handle(event)
nil
end

def validate
return true if valid_items_qty?
Yast::Report.Error(items_qty_error_message)
false
end

private

# @return [Array<String>] Header identifiers
Expand Down Expand Up @@ -185,6 +193,31 @@ def format_hash_item(item)
headers_ids.map { |h| item[h] }
end
end

# Determines whether the quantity of items is valid or not
#
# @return [Boolean]
def valid_items_qty?
return false if @min_items && value.size < @min_items
return false if @max_items && value.size > @max_items
true
end

# Returns an error message for the valid size of the collection
#
# @return [String]
def items_qty_error_message
if @min_items && !@max_items
# TRANSLATORS: "Expected at least 4 elements for 'Computers'"
format(_("Expected at least %s items for '%s'"), @min_items, label)
elsif !@min_items && @max_items
# TRANSLATORS: "Expected at most 4 elements for 'Computers'"
format(_("Expected at most %s items for '%s'"), @max_items, label)
else
# TRANSLATORS: "Expected between 2 and 4 elements for 'Computers'"
format(_("Expected between %s and %s items for '%s'"), @min_items, @max_items, label)
end
end
end
end
end
107 changes: 0 additions & 107 deletions test/fixtures/form.yml

This file was deleted.

72 changes: 69 additions & 3 deletions test/fixtures/formulas-ng/test-formula/form.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@ person:
email:
"$name": "E-mail"
"$type": email
"$default": "somebody@example.net"
birth_date:
"$type": date
password:
"$type": password
"$default": DefaultSecret
homepage:
"$type": url
"$default": "http://myhomepage.com"
wants_newsletter:
"$type": boolean
"$default": true
newsletter_color:
$type: color
$default: "#73ba25"
address:
"$type": group
street:
Expand All @@ -21,21 +33,75 @@ person:
- Czech Republic
- Germany
- Spain
# Array collection, hash values
computers:
"$type": edit-group
"$minItems": 1
"$maxItems": 4
"$default":
- brand: ACME
disks: []
"$prototype":
$type: group
brand:
"$name": Brand
"$default": Dell
"$type": select
"$values":
- ACME
- Acer
- Dell
- Lenovo
# Array collection, hash values
disks:
"$name": Number of Disks
"$type": number
"$default": 1
"$type": edit-group
"$minItems": 1
"$maxItems": 3
"$prototype":
$type: group
type:
"$name": Type
"$type": select
"$values":
- HDD
- SSD
size:
"$type": text
# Hash collection, hash values
projects:
"$type": edit-group
"$minItems": 1
"$name": Project
"$itemName": ${i} project
"$default":
yast2:
url: https://yast.opensuse.org
"$prototype":
"$type": group
"$key":
"$type": text
"$name": Project name
url:
"$type": text
# Hash collection, scalar values
properties:
"$type": edit-group
"$minItems": 0
"$name": Properties
"$default":
license:
GPL
"$prototype":
"$type": text
"$name": Property
"$key":
"$type": text
# Array collection, scalar values
platforms:
"$type": edit-group
"$minItems": 0
"$name": Platforms
"$default":
- Linux
"$prototype":
"$type": text
4 changes: 3 additions & 1 deletion test/y2configuration_management/salt/form_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
describe Y2ConfigurationManagement::Salt::FormBuilder do
subject(:builder) { described_class.new(controller, form) }
let(:form) do
Y2ConfigurationManagement::Salt::Form.from_file(FIXTURES_PATH.join("form.yml"))
Y2ConfigurationManagement::Salt::Form.from_file(
FIXTURES_PATH.join("formulas-ng", "test-formula", "form.yml")
)
end
let(:element) { form.find_element_by(locator: locator) }
let(:controller) { instance_double(Y2ConfigurationManagement::Salt::FormController) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
let(:form_widget_1) { instance_double(Y2ConfigurationManagement::Widgets::Form) }
let(:locator) { locator_from_string("root#person") }
let(:locator_1) { locator_from_string("root#person#computers[0]#brand") }
let(:form) { Y2ConfigurationManagement::Salt::Form.from_file(FIXTURES_PATH.join("form.yml")) }
let(:form) do
Y2ConfigurationManagement::Salt::Form.from_file(
FIXTURES_PATH.join("formulas-ng", "test-formula", "form.yml")
)
end
let(:pillar) { Y2ConfigurationManagement::Salt::Pillar.new }
let(:data) { Y2ConfigurationManagement::Salt::FormData.from_pillar(form, pillar) }

Expand Down

0 comments on commit fe2318c

Please sign in to comment.