Skip to content

Commit

Permalink
Merge pull request #164 from amco/fix_update_properties
Browse files Browse the repository at this point in the history
Fix issue with nil properties not being set and passing hash with string keys issue
  • Loading branch information
ErickFabian authored May 11, 2021
2 parents e24f842 + 5aadf26 commit ecccc4c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/dolly/property_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def update_attribute
end
end

def write_attribute key, value
def write_attribute(key, value)
value = set_property_value(key, value)
instance_variable_set(:"@#{key}", value)
update_doc(key, value) unless value.nil?
update_doc(key, value)
end

def valid_property?(name)
properties.include? name
properties.include?(name)
end

def update_doc(key, value)
Expand Down
18 changes: 18 additions & 0 deletions test/property_manager_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'test_helper'

class TestDoc < Dolly::Document
property :name, class_name: String
property :email, class_name: String
property :last_name, class_name: String
end

class PropertyManagerTest < Test::Unit::TestCase
test 'write_attribute with nil value' do
doc = TestDoc.new(name: 'name', last_name: nil, email: 'does not change')
assert_equal(doc.name, 'name')
doc.update_properties(name: nil)
assert_equal(doc.name, nil)
assert_equal(doc.last_name, nil)
assert_equal(doc.email, 'does not change')
end
end

0 comments on commit ecccc4c

Please sign in to comment.