Skip to content

Commit

Permalink
fixes issue #11 by prioritizing #attributes over #to_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
abrandoned committed Nov 24, 2013
1 parent 0db7786 commit e3644fb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/active_remote/bulk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ def destroy_all(*records)
#
def parse_records(*records)
records.flatten!
records.collect!(&:to_hash)

if records.first.respond_to?(:attributes)
records.collect!(&:attributes)
else
records.collect!(&:to_hash)
end

return records.first if records.first.has_key?(:records)

Expand Down
14 changes: 13 additions & 1 deletion spec/lib/active_remote/bulk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,24 @@

describe ".parse_records" do
let(:records) { [ Hash.new ] }
let(:attribute_record) {
record = double(Hash)
record.stub(:attributes) { {} }
record
}
let(:records) { [ Hash.new ] }

it "preps records to be built into a bulk request" do
parsed_records = { :records => records }
Tag.parse_records(records).should eq parsed_records
end

it "preps records to be built into a bulk request (prioritizing :attributes over :to_hash)" do
attribute_record.should_receive(:attributes)
parsed_records = { :records => [ {} ] }
Tag.parse_records([ attribute_record ]).should eq parsed_records
end

context "when given a bulk message" do
let(:records) { [ tag.to_hash ] }
let(:tag) { Generic::Remote::Tag.new }
Expand All @@ -71,4 +83,4 @@
Tag.update_all(records)
end
end
end
end

0 comments on commit e3644fb

Please sign in to comment.