Skip to content

Commit

Permalink
Fix default value for Select widgets
Browse files Browse the repository at this point in the history
when adding a new collection member
  • Loading branch information
mvidner committed Jan 8, 2019
1 parent 6ebe727 commit e7291dc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
17 changes: 12 additions & 5 deletions src/lib/y2configuration_management/widgets/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,26 @@ def initialize(spec, controller)
@id = spec.id
@controller = controller
self.widget_id = "select:#{spec.id}"
# Allow #value= before #init.
# Wrap the :value accessor to add an "uninitialized" state
# (which the YUI/CWM widget does not have)
# so that we can give it a default
@value = nil
end

def value
@value = super
end

# Workaround for modifying the current value. It modifies also the
# default value for initializing it correctly when no value is defined
def value=(value)
@default = value
@value = value
super
end

# @see CWM::AbstractWidget
def init
return if default.nil?
self.value = default
return if default.nil? # combo cannot have no value; prevent YUI error
self.value = @value || default
end

private
Expand Down
7 changes: 3 additions & 4 deletions test/y2configuration_management/widgets/select_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@
end

describe "#value=" do
it "modifies the default value with the value given" do
expect(selector.default).to_not eql("Spain")
selector.value = "Spain"
expect(selector.default).to eql("Spain")
it "modifies the cache @value with the value given" do
expect { selector.value = "Spain" }
.to change { selector.instance_variable_get(:@value) }.to("Spain")
end
end
end

0 comments on commit e7291dc

Please sign in to comment.