Add artist commentary listing and search

related to #2084, fixes #2085
This commit is contained in:
Toks
2014-03-10 22:56:38 -04:00
parent 424d84661a
commit 2f3f401f3b
10 changed files with 130 additions and 2 deletions

View File

@@ -5,6 +5,46 @@ class ArtistCommentary < ActiveRecord::Base
has_many :versions, :class_name => "ArtistCommentaryVersion", :dependent => :destroy, :foreign_key => :post_id, :primary_key => :post_id, :order => "artist_commentary_versions.id ASC"
after_save :create_version
module SearchMethods
def text_matches(query)
escaped_query = query.to_escaped_for_sql_like
where("original_title ILIKE ? ESCAPE E'\\\\' OR original_description ILIKE ? ESCAPE E'\\\\' OR translated_title ILIKE ? ESCAPE E'\\\\' OR translated_description ILIKE ? ESCAPE E'\\\\'", escaped_query, escaped_query, escaped_query, escaped_query)
end
def post_tags_match(query)
joins(:post).where("posts.tag_index @@ to_tsquery('danbooru', ?)", query.to_escaped_for_tsquery_split)
end
def search(params)
q = scoped
params = {} if params.blank?
if params[:text_matches].present?
q = q.text_matches(params[:text_matches])
end
if params[:original_present] == "yes"
q = q.where("(original_title is not null and original_title != '') or (original_description is not null and original_description != '')")
elsif params[:original_present] == "no"
q = q.where("(original_title is null or original_title = '') and (original_description is null or original_description = '')")
end
if params[:translated_present] == "yes"
q = q.where("(translated_title is not null and translated_title != '') or (translated_description is not null and translated_description != '')")
elsif params[:translated_present] == "no"
q = q.where("(translated_title is null or translated_title = '') and (translated_description is null or translated_description = '')")
end
if params[:post_tags_match].present?
q = q.post_tags_match(params[:post_tags_match])
end
q
end
end
extend SearchMethods
def original_present?
original_title.present? || original_description.present?
end