improved search methods

This commit is contained in:
albert
2013-01-11 17:13:55 -05:00
parent c7ffda81bf
commit 72b7651169
14 changed files with 93 additions and 4 deletions

View File

@@ -35,7 +35,7 @@ class Comment < ActiveRecord::Base
end
def for_creator_name(user_name)
where("creator_id = (select _.id from users _ where lower(_.name) = lower(?))", user_name)
where("creator_id = (select _.id from users _ where lower(_.name) = lower(?))", user_name.downcase)
end
def search(params)

View File

@@ -14,6 +14,17 @@ class CommentVote < ActiveRecord::Base
destroy_all("created_at < ?", 14.days.ago)
end
def self.search(params)
q = scoped
return q if params.blank?
if params[:comment_id]
q = q.where("comment_id = ?", params[:comment_id].to_i)
end
q
end
def validate_user_can_vote
if !user.can_comment_vote?
errors.add :user, "can not comment vote"

View File

@@ -21,7 +21,7 @@ class ForumPost < ActiveRecord::Base
end
def creator_name(name)
where("forum_posts.creator_id = (select _.id from users _ where lower(_.name) = ?)", name)
where("forum_posts.creator_id = (select _.id from users _ where lower(_.name) = ?)", name.downcase)
end
def active

View File

@@ -15,6 +15,10 @@ class JanitorTrial < ActiveRecord::Base
q = q.where("user_id = (select _.id from users _ where lower(_.name) = ?)", params[:user_name].downcase)
end
if params[:user_id]
q = q.where("user_id = ?", params[:user_id].to_i)
end
q
end

View File

@@ -27,7 +27,7 @@ class Note < ActiveRecord::Base
end
def creator_name(name)
where("creator_id = (select _.id from users _ where lower(_.name) = ?)", name)
where("creator_id = (select _.id from users _ where lower(_.name) = ?)", name.downcase)
end
def search(params)
@@ -46,6 +46,10 @@ class Note < ActiveRecord::Base
q = q.creator_name(params[:creator_name])
end
if params[:creator_id]
q = q.where("creator_id = ?", params[:creator_id].to_i)
end
q
end
end

View File

@@ -15,6 +15,10 @@ class NoteVersion < ActiveRecord::Base
q = q.where("post_id = ?", params[:post_id].to_i)
end
if params[:note_id]
q = q.where("note_id = ?", params[:note_id].to_i)
end
q
end

View File

@@ -33,7 +33,11 @@ class Pool < ActiveRecord::Base
end
if params[:creator_name]
q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name])
q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].downcase)
end
if params[:creator_id]
q = q.where("creator_id = ?", params[:creator_id].to_i)
end
q

View File

@@ -19,6 +19,14 @@ class PoolVersion < ActiveRecord::Base
q = q.for_user(params[:updater_id].to_i)
end
if params[:updater_name]
q = q.where("updater_id = (select _.id from users _ where lower(_.name) = ?)", params[:updater_name].downcase)
end
if params[:pool_id]
q = q.where("pool_id = ?", params[:pool_id].to_i)
end
q
end
end

View File

@@ -22,6 +22,14 @@ class PostAppeal < ActiveRecord::Base
q = scoped
return q if params.blank?
if params[:creator_id]
q = q.for_user(params[:creator_id].to_i)
end
if params[:creator_name]
q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].downcase)
end
if params[:post_id]
q = q.where("post_id = ?", params[:post_id].to_i)
end

View File

@@ -27,6 +27,14 @@ class PostFlag < ActiveRecord::Base
q = scoped
return q if params.blank?
if params[:creator_id]
q = q.where("creator_id = ?", params[:creator_id].to_i)
end
if params[:creator_name]
q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].downcase)
end
if params[:post_id]
q = q.where("post_id = ?", params[:post_id].to_i)
end

View File

@@ -45,6 +45,16 @@ class TagSubscription < ActiveRecord::Base
def self.search(params)
q = scoped
return q if params.blank?
if params[:creator_id]
q = q.where("creator_id = ?", params[:creator_id].to_i)
end
if params[:creator_name]
q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].downcase)
end
q
end
def self.visible_to(user)

View File

@@ -318,6 +318,10 @@ class Upload < ActiveRecord::Base
q = q.uploaded_by(params[:uploader_id].to_i)
end
if params[:uploader_name]
q = q.where("uploader_id = (select _.id from users _ where lower(_.name) = ?)", params[:uploader_name].downcase)
end
if params[:source]
q = q.where("source = ?", params[:source])
end

View File

@@ -32,6 +32,18 @@ class UserFeedback < ActiveRecord::Base
q = q.for_user(params[:user_id].to_i)
end
if params[:user_name]
q = q.where("user_id = (select _.id from users _ where lower(_.name) = ?)", params[:user_name].downcase)
end
if params[:creator_id]
q = q.where("creator_id = ?", params[:creator_id].to_i)
end
if params[:creator_name]
q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].downcase)
end
q
end
end

View File

@@ -19,6 +19,10 @@ class WikiPage < ActiveRecord::Base
def recent
order("updated_at DESC").limit(25)
end
def body_matches(query)
where("body_index @@ plainto_tsquery(?)", query.scan(/\S+/).join(" & "))
end
def search(params = {})
q = scoped
@@ -31,6 +35,14 @@ class WikiPage < ActiveRecord::Base
if params[:creator_id]
q = q.where("creator_id = ?", params[:creator_id])
end
if params[:body_matches]
q = q.body_matches(params[:body_matches])
end
if params[:creator_name]
q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].downcase)
end
q
end