Skip to content

Commit c43e1d0

Browse files
committed
Merge pull request activeadmin#1063 from jancel/34_maintain_backwards_compatibility
Fix string vs integer comparison on postgres.
2 parents 82a13de + 5642fe2 commit c43e1d0

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/active_admin/comments/comment.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,27 @@ class Comment < ActiveRecord::Base
1919
def self.resource_type(record)
2020
record.class.base_class.name.to_s
2121
end
22+
23+
def self.resource_id_cast(record)
24+
# Postgres adapters won't compare strings to numbers (issue 34)
25+
if resource_id_type == :string
26+
record.id.to_s
27+
else
28+
record.id
29+
end
30+
end
2231

2332
def self.find_for_resource_in_namespace(resource, namespace)
2433
where(:resource_type => resource_type(resource),
25-
:resource_id => resource.id,
34+
:resource_id => resource_id_cast(resource),
2635
:namespace => namespace.to_s)
2736
end
2837

38+
39+
def self.resource_id_type
40+
columns.select { |i| i.name == "resource_id" }.first.type
41+
end
42+
2943
end
3044

3145
end

spec/unit/comments_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@
3737
end
3838
end
3939

40+
describe ".resource_id_cast" do
41+
let(:post) { Post.create!(:title => "Testing.") }
42+
let(:namespace_name) { "admin" }
43+
44+
it "should cast resource_id as string" do
45+
comment = ActiveAdmin::Comment.create! :resource => post,
46+
:body => "Another Comment",
47+
:namespace => namespace_name
48+
ActiveAdmin::Comment.resource_id_cast(comment).class.should eql String
49+
end
50+
end
51+
52+
describe ".resource_id_type" do
53+
it "should be :string" do
54+
ActiveAdmin::Comment.resource_id_type.should eql :string
55+
end
56+
end
57+
4058
describe "Commenting on resource with string id" do
4159
let(:tag){ Tag.create!(:name => "cooltags") }
4260
let(:namespace_name){ "admin" }

0 commit comments

Comments
 (0)