Guest User

Untitled

a guest
Feb 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. ## called
  2.  
  3.  
  4. def related_reviews_features(limit)
  5. terms, like_statement = like_terms
  6.  
  7. results = []
  8.  
  9. results = Article.find(
  10. :all,
  11. :select => "id, type, title, artist, sub_category_id, published_at, abstract, article_pages_data, image_id, source",
  12. :limit => limit,
  13. :order => 'published_at DESC, updated_at DESC',
  14. :conditions => ["(artist #{like_statement.join('OR artist ')}) AND (type = 'RecordReview' OR type = 'TrackReview') " +
  15. "AND (published_at <= NOW() OR published_at IS NULL) AND approved = 1 AND id != #{self[:id]}"].concat(terms))
  16.  
  17. results.concat Feature.find(
  18. :all,
  19. :select => "id, type, title, artist, sub_category_id, published_at, abstract, article_pages_data, image_id, source",
  20. :limit => limit,
  21. :order => 'published_at DESC, updated_at DESC',
  22. :conditions => ["((artist #{like_statement.join('OR artist ')}) OR (title #{like_statement.join('OR title ')})) " +
  23. "AND (published_at <= NOW() OR published_at IS NULL) AND approved = 1 AND id != #{self[:id]}"].concat(terms).concat(terms))
  24.  
  25. sort_and_limit_related(results, limit)
  26. end
  27.  
  28. ## which calls
  29.  
  30. def self.tokenize_terms(terms)
  31. terms.split(/[\/\[\]]|OR/).collect { |sub_term| "%#{sub_term.gsub(/and|[&]|the/i, '%').gsub(/ø|Ø|Ǿ|ǿ/, 'o').strip}%" }
  32. end
  33.  
  34. def like_terms
  35. terms = Article.tokenize_terms(self[(artist and artist != "") ? :artist : :title])
  36. like_statement = []
  37. terms.size.times { like_statement << 'LIKE ? ' }
  38. [terms, like_statement]
  39. end
  40.  
  41. def sort_and_limit_related(results, limit)
  42. results.sort! do | a, b |
  43. if a.nil? and not b.nil?
  44. 1
  45. elsif b.nil? and not a.nil?
  46. -1
  47. elsif a.nil? and b.nil?
  48. 0
  49. elsif a.published_at.nil? and not b.published_at.nil?
  50. 1
  51. elsif b.published_at.nil? and not a.published_at.nil?
  52. -1
  53. elsif a.published_at.nil? and b.published_at.nil?
  54. 0
  55. else
  56. b.published_at <=> a.published_at
  57. end
  58. end
  59.  
  60. results[0...limit]
  61. end
Add Comment
Please, Sign In to add comment