aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGareth Rees <gareth@mysociety.org>2014-10-20 10:23:51 +0100
committerGareth Rees <gareth@mysociety.org>2014-10-29 13:01:10 +0000
commit32c019bbae4995bc105c6813a4f839adf2805542 (patch)
tree46362c1763ac1f090f37b0ed168012f76270d6cc
parent962357cd6d65dbc047cfbf3998e03d13dc130bfd (diff)
Improve Comment.find_existing
Use `.where` Extract complex sql to named variable for readability and line length
-rw-r--r--app/models/comment.rb11
1 files changed, 7 insertions, 4 deletions
diff --git a/app/models/comment.rb b/app/models/comment.rb
index 1dd30a271..84674cd91 100644
--- a/app/models/comment.rb
+++ b/app/models/comment.rb
@@ -60,15 +60,18 @@ class Comment < ActiveRecord::Base
text.html_safe
end
- # When posting a new comment, use this to check user hasn't double submitted.
+ # When posting a new comment, use this to check user hasn't double
+ # submitted.
def self.find_existing(info_request_id, body)
# TODO: can add other databases here which have regexp_replace
if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
# Exclude spaces from the body comparison using regexp_replace
- Comment.find(:first, :conditions => [ "info_request_id = ? and regexp_replace(body, '[[:space:]]', '', 'g') = regexp_replace(?, '[[:space:]]', '', 'g')", info_request_id, body ])
+ regex_replace_sql = "regexp_replace(body, '[[:space:]]', '', 'g') = regexp_replace(?, '[[:space:]]', '', 'g')"
+ Comment.where(["info_request_id = ? AND #{ regex_replace_sql }", info_request_id, body ]).first
else
- # For other databases (e.g. SQLite) not the end of the world being space-sensitive for this check
- Comment.find(:first, :conditions => [ "info_request_id = ? and body = ?", info_request_id, body ])
+ # For other databases (e.g. SQLite) not the end of the world being
+ # space-sensitive for this check
+ Comment.where(:info_request_id => info_request_id, :body => body).first
end
end