Skip to content

Commit

Permalink
Merge pull request #1 from robschwartz/master
Browse files Browse the repository at this point in the history
Added List to ignore when merging 2 documents
  • Loading branch information
fallanic authored Nov 16, 2016
2 parents caff08b + 75e2ade commit 6f39366
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ B attributes will be merged into A.

B is also destroyed after the merge has been successful.

## Testing

In the terminal, you'll need to start a mongoDB connection

To do this, you'll need to run `mongod` while specifying a local folder

EX:

$ mkdir tmpdb
$ mongod --dbpath ./tmpdb

Open another tab and run `rspec`


That's all folks!

Expand Down
22 changes: 14 additions & 8 deletions lib/merge-mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ module Document
module Mergeable

# Merge a mongoid document into another.
# ignored_attributes: Array of attributes you want to ignore when merging 2 documents
# A.merge!(B)
def merge!(another_document, arr_of_hash_uniq = {})
def merge!(another_document, arr_of_hash_uniq = {}, ignored_attributes = [])
if another_document.nil?
raise "Cannot merge a nil document."
elsif (self.class <=> another_document.class).nil?
Expand All @@ -20,13 +21,18 @@ def merge!(another_document, arr_of_hash_uniq = {})
#
# We iterate on B attributes :
another_document.attributes.keys.each do |key|
self[key] = merge_attributes(self[key],another_document[key],arr_of_hash_uniq[key])
#mongoid dirty checks don't always work correctly for arrays and maybe hashes, so we'll give it a little help
if self[key].is_a? Array
self.changed_attributes[key] = nil
elsif self[key].is_a? Hash
self.changed_attributes[key] = nil
end
# If the current key is included in the ignore list, skip it.
if ignored_attributes.include?(key)
next
else
self[key] = merge_attributes(self[key],another_document[key],arr_of_hash_uniq[key])
#mongoid dirty checks don't always work correctly for arrays and maybe hashes, so we'll give it a little help
if self[key].is_a? Array
self.changed_attributes[key] = nil
elsif self[key].is_a? Hash
self.changed_attributes[key] = nil
end
end
end

# saving the A model
Expand Down
16 changes: 16 additions & 0 deletions spec/merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,21 @@ class MyRandomclass
end


it "should not merge attributes that are included in the ignore_attributes array" do
@A.array_hashes = [{test1: "test1"}]
@B.array_hashes = [{test2: "test2"}]
@A.merge!(@B, {}, ['array_hashes'])

@A.array_hashes.size.should == 1
end

it "should merge attributes that are not included in the ignore_attributes array" do
@A.array_hashes = [{test1: "test1"}]
@B.array_hashes = [{test2: "test2"}]
@A.merge!(@B, {}, ['not_array_hashes'])

@A.array_hashes.size.should == 2
end

end
end

0 comments on commit 6f39366

Please sign in to comment.