Skip to content

Commit

Permalink
move the code to model
Browse files Browse the repository at this point in the history
- added tests
  • Loading branch information
lslezak committed Jun 20, 2014
1 parent 03d7536 commit 6726a3d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
13 changes: 13 additions & 0 deletions src/lib/registration/addon.rb
Expand Up @@ -105,5 +105,18 @@ def label
(friendly_name && !friendly_name.empty?) ? friendly_name : name
end

# can be the addon selected in UI or should it be disabled?
# return [Boolean] true if it should be enabled
def selectable?
# Do not support unregister
return false if registered?
# Do not allow to select child without selected or already registered parent
return false if depends_on && !(depends_on.selected? || depends_on.registered?)
# Do not allow to unselect parent if any children is selected
return false if children.any?(&:selected?)

return true
end

end
end
13 changes: 1 addition & 12 deletions src/lib/registration/ui/addon_selection_dialog.rb
Expand Up @@ -180,21 +180,10 @@ def show_addon_details(addon)

def reactivate_dependencies
@addons.each do |addon|
Yast::UI.ChangeWidget(Id(addon.identifier), :Enabled, enable_addon?(addon))
Yast::UI.ChangeWidget(Id(addon.identifier), :Enabled, addon.selectable?)
end
end

def enable_addon?(addon)
# Do not support unregister
return false if addon.registered?
# Do not allow to select child without selected or already registered parent
return false if addon.depends_on && (!addon.depends_on.selected? || !addon.depends_on.registered?)
# Do not allow to unselect parent if any children is selected
return false if addon.children.any?(&:selected?)

return true
end

# the maximum number of reg. codes displayed vertically,
# this is the limit for 80x25 textmode UI
MAX_REGCODES_PER_COLUMN = 8
Expand Down
37 changes: 37 additions & 0 deletions test/addon_spec.rb
Expand Up @@ -116,4 +116,41 @@
end
end

describe "#selectable?" do
let(:addons) do
Registration::Addon.find_all(double(:get_addon_list => [addon_with_child_generator]))
end

let(:parent) { addons.first }
let(:child) { parent.children.first }

it "cannot be selected when the addon has been already registered" do
addon.registered
expect(addon.selectable?).to be_false
end

it "can be selected when the addon has not been already registered" do
expect(addon.selectable?).to be_true
end

it "cannot be selected when the parent is not selected or registered" do
expect(child.selectable?).to be_false
end

it "can be selected when the parent is selected" do
parent.selected
expect(child.selectable?).to be_true
end

it "can be selected when the parent is registered" do
parent.registered
expect(child.selectable?).to be_true
end

it "cannot be changed when any child is selected" do
child.selected
expect(parent.selectable?).to be_false
end
end

end

0 comments on commit 6726a3d

Please sign in to comment.