Skip to content

Commit

Permalink
compatible with composite_primary_keys gem #214
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed May 15, 2015
1 parent 32ce3eb commit 8eff544
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/bullet/ext/object.rb
Expand Up @@ -4,7 +4,9 @@ def bullet_key
end

def primary_key_value
if self.class.respond_to?(:primary_key) && self.class.primary_key
if self.class.respond_to?(:primary_keys) && self.class.primary_keys
self.class.primary_keys.map { |primary_key| self.send primary_key }.join(',')
elsif self.class.respond_to?(:primary_key) && self.class.primary_key
self.send self.class.primary_key
else
self.id
Expand Down
7 changes: 6 additions & 1 deletion spec/bullet/ext/object_spec.rb
Expand Up @@ -25,7 +25,12 @@
post = Post.first
Post.primary_key = 'name'
expect(post.primary_key_value).to eq(post.name)
Post.primary_key = 'id'
end

it "should return value for multiple primary keys" do
post = Post.first
allow(Post).to receive(:primary_keys).and_return([:category_id, :writer_id])
expect(post.primary_key_value).to eq("#{post.category_id},#{post.writer_id}")
end
end
end

0 comments on commit 8eff544

Please sign in to comment.