Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## called
- def related_reviews_features(limit)
- terms, like_statement = like_terms
- results = []
- results = Article.find(
- :all,
- :select => "id, type, title, artist, sub_category_id, published_at, abstract, article_pages_data, image_id, source",
- :limit => limit,
- :order => 'published_at DESC, updated_at DESC',
- :conditions => ["(artist #{like_statement.join('OR artist ')}) AND (type = 'RecordReview' OR type = 'TrackReview') " +
- "AND (published_at <= NOW() OR published_at IS NULL) AND approved = 1 AND id != #{self[:id]}"].concat(terms))
- results.concat Feature.find(
- :all,
- :select => "id, type, title, artist, sub_category_id, published_at, abstract, article_pages_data, image_id, source",
- :limit => limit,
- :order => 'published_at DESC, updated_at DESC',
- :conditions => ["((artist #{like_statement.join('OR artist ')}) OR (title #{like_statement.join('OR title ')})) " +
- "AND (published_at <= NOW() OR published_at IS NULL) AND approved = 1 AND id != #{self[:id]}"].concat(terms).concat(terms))
- sort_and_limit_related(results, limit)
- end
- ## which calls
- def self.tokenize_terms(terms)
- terms.split(/[\/\[\]]|OR/).collect { |sub_term| "%#{sub_term.gsub(/and|[&]|the/i, '%').gsub(/ø|Ø|Ǿ|ǿ/, 'o').strip}%" }
- end
- def like_terms
- terms = Article.tokenize_terms(self[(artist and artist != "") ? :artist : :title])
- like_statement = []
- terms.size.times { like_statement << 'LIKE ? ' }
- [terms, like_statement]
- end
- def sort_and_limit_related(results, limit)
- results.sort! do | a, b |
- if a.nil? and not b.nil?
- 1
- elsif b.nil? and not a.nil?
- -1
- elsif a.nil? and b.nil?
- 0
- elsif a.published_at.nil? and not b.published_at.nil?
- 1
- elsif b.published_at.nil? and not a.published_at.nil?
- -1
- elsif a.published_at.nil? and b.published_at.nil?
- 0
- else
- b.published_at <=> a.published_at
- end
- end
- results[0...limit]
- end
Add Comment
Please, Sign In to add comment