diff options
Diffstat (limited to 'app/models/node.rb')
| -rw-r--r-- | app/models/node.rb | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/app/models/node.rb b/app/models/node.rb index 9cb4840..24f3cd0 100644 --- a/app/models/node.rb +++ b/app/models/node.rb | |||
| @@ -345,6 +345,28 @@ class Node < ApplicationRecord | |||
| 345 | .distinct | 345 | .distinct |
| 346 | end | 346 | end |
| 347 | 347 | ||
| 348 | # This one is for admin-only views, where finding a draft is the point. | ||
| 349 | # Substring match on whichever of head/draft is present. | ||
| 350 | def self.editor_search(term) | ||
| 351 | words = term.to_s.split(/\s+/).reject(&:blank?) | ||
| 352 | return none if words.empty? | ||
| 353 | |||
| 354 | conditions = [] | ||
| 355 | binds = {} | ||
| 356 | |||
| 357 | words.each_with_index do |word, i| | ||
| 358 | key = "term#{i}" | ||
| 359 | binds[key.to_sym] = "%#{sanitize_sql_like(word)}%" | ||
| 360 | conditions << "(head_translations.title ILIKE :#{key} OR head_translations.abstract ILIKE :#{key} " \ | ||
| 361 | "OR draft_translations.title ILIKE :#{key} OR draft_translations.abstract ILIKE :#{key})" | ||
| 362 | end | ||
| 363 | |||
| 364 | joins("LEFT JOIN page_translations head_translations ON head_translations.page_id = nodes.head_id") | ||
| 365 | .joins("LEFT JOIN page_translations draft_translations ON draft_translations.page_id = nodes.draft_id") | ||
| 366 | .where(conditions.join(" AND "), binds) | ||
| 367 | .distinct | ||
| 368 | end | ||
| 369 | |||
| 348 | protected | 370 | protected |
| 349 | def lock_for! current_user | 371 | def lock_for! current_user |
| 350 | self.lock_owner = current_user | 372 | self.lock_owner = current_user |
