Skip to content

Commit

Permalink
Improvements from code review
Browse files Browse the repository at this point in the history
- Yast.import at top level, like `require`
- Don't `include Yast`
- Less nesting
- Specify return value in a corner case
- UIShortcuts via require "yast/rspec"
  • Loading branch information
mvidner committed Mar 7, 2016
1 parent e0df2d7 commit 75884e4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
47 changes: 19 additions & 28 deletions src/lib/installation/select_system_role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@

require "yast"
require "ui/installation_dialog"
Yast.import "ProductControl"
Yast.import "ProductFeatures"

module Installation
class SelectSystemRole < ::UI::InstallationDialog
include Yast

def initialize
super

textdomain "installation"
Yast.import "ProductControl"
Yast.import "ProductFeatures"
end

def run
Expand All @@ -40,8 +38,6 @@ def run
super
end

public # called by parent class

def dialog_title
_("System Role")
end
Expand All @@ -51,32 +47,27 @@ def help_text
end

def dialog_content
RadioButtonGroup(
Id(:roles),
VBox(
* ui_roles.map do |r|
VBox(
Left(RadioButton(Id(r[:id]), r[:label])),
HBox(
HSpacing(4),
Left(Label(r[:description]))
),
VSpacing(2)
)
end
ui_roles = role_attributes.each_with_object(VBox()) do |r, vbox|
vbox << Left(RadioButton(Id(r[:id]), r[:label]))
vbox << HBox(
HSpacing(4),
Left(Label(r[:description]))
)
)
vbox << VSpacing(2)
end

RadioButtonGroup(Id(:roles), ui_roles)
end

def create_dialog
clear_role
ok = super
UI.ChangeWidget(Id(:roles), :CurrentButton, ui_roles.first[:id])
Yast::UI.ChangeWidget(Id(:roles), :CurrentButton, role_attributes.first[:id])
ok
end

def next_handler
role_id = UI.QueryWidget(Id(:roles), :CurrentButton)
role_id = Yast::UI.QueryWidget(Id(:roles), :CurrentButton)
apply_role(role_id)

super
Expand All @@ -85,15 +76,15 @@ def next_handler
private

def clear_role
ProductFeatures.ClearOverlay
Yast::ProductFeatures.ClearOverlay
end

def apply_role(role_id)
log.info "Applying system role '#{role_id}'"
features = raw_roles.find { |r| r["id"] == role_id }
features = features.dup
features.delete("id")
ProductFeatures.SetOverlay(features)
Yast::ProductFeatures.SetOverlay(features)
end

# the contents is an overlay for ProductFeatures sections
Expand All @@ -103,17 +94,17 @@ def apply_role(role_id)
# ]
# @return [Array<Hash{String => Object}>]
def raw_roles
ProductControl.productControl.fetch("system_roles", [])
Yast::ProductControl.productControl.fetch("system_roles", [])
end

def ui_roles
def role_attributes
raw_roles.map do |r|
id = r["id"]

{
id: id,
label: ProductControl.GetTranslatedText(id),
description: ProductControl.GetTranslatedText(id + "_description")
label: Yast::ProductControl.GetTranslatedText(id),
description: Yast::ProductControl.GetTranslatedText(id + "_description")
}
end
end
Expand Down
6 changes: 2 additions & 4 deletions test/select_system_role_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
.and_return("system_roles" => [])
end

it "does not display dialog" do
it "does not display dialog, and returns :auto" do
expect(Yast::Wizard).to_not receive(:SetContents)
subject.run
expect(subject.run).to eq(:auto)
end
end

context "when some roles are defined" do
include Yast::UIShortcuts

let(:control_file_roles) do
[
{ "id" => "foo", "partitioning" => { "format" => true } },
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ENV["Y2DIR"] = y2dirs.unshift(srcdir).join(":")

require "yast"
require "yast/rspec"

# fake AutoinstConfigClass class which is not supported by Ubuntu
module Yast
Expand Down

0 comments on commit 75884e4

Please sign in to comment.