From 00d347c4f4d51875529dc39388e78bb941b7614f Mon Sep 17 00:00:00 2001 From: joshua stein Date: Wed, 28 Jan 2015 15:02:40 -0600 Subject: [PATCH] consolidate sql fragment to cast story/comment score --- app/indices/comment_index.rb | 4 +--- app/indices/story_index.rb | 4 +--- app/models/comment.rb | 5 +++++ app/models/story.rb | 5 +++++ app/models/story_repository.rb | 6 ++---- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/indices/comment_index.rb b/app/indices/comment_index.rb index d248de9..4da56f3 100644 --- a/app/indices/comment_index.rb +++ b/app/indices/comment_index.rb @@ -3,9 +3,7 @@ ThinkingSphinx::Index.define :comment, :with => :active_record do indexes short_id indexes user.username, :as => :author - has "(CAST(upvotes as #{Story.votes_cast_type}) - " << - "CAST(downvotes as #{Story.votes_cast_type}))", :as => :score, - :type => :bigint, :sortable => true + has Comment.score_sql, :as => :score, :type => :bigint, :sortable => true has is_deleted has created_at diff --git a/app/indices/story_index.rb b/app/indices/story_index.rb index c05c780..45eb64f 100644 --- a/app/indices/story_index.rb +++ b/app/indices/story_index.rb @@ -9,9 +9,7 @@ ThinkingSphinx::Index.define :story, :with => :active_record do has created_at, :sortable => true has hotness, is_expired - has "(CAST(upvotes as #{Story.votes_cast_type}) - " << - "CAST(downvotes as #{Story.votes_cast_type}))", :as => :score, - :type => :bigint, :sortable => true + has Story.score_sql, :as => :score, :type => :bigint, :sortable => true set_property :field_weights => { :upvotes => 15, diff --git a/app/models/comment.rb b/app/models/comment.rb index 27ce824..cbfd3b7 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -103,6 +103,11 @@ class Comment < ActiveRecord::Base nil end + def self.score_sql + "(CAST(upvotes AS #{Story.votes_cast_type}) - " << + "CAST(downvotes AS #{Story.votes_cast_type}))" + end + def as_json(options = {}) h = super(:only => [ :short_id, diff --git a/app/models/story.rb b/app/models/story.rb index 0485da5..7eab9df 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -102,6 +102,11 @@ class Story < ActiveRecord::Base true end + def self.score_sql + "(CAST(upvotes AS #{votes_cast_type}) - " << + "CAST(downvotes AS #{votes_cast_type}))" + end + def self.votes_cast_type Story.connection.adapter_name.match(/mysql/i) ? "signed" : "integer" end diff --git a/app/models/story_repository.rb b/app/models/story_repository.rb index 6393d20..3507289 100644 --- a/app/models/story_repository.rb +++ b/app/models/story_repository.rb @@ -86,8 +86,7 @@ class StoryRepository def top(length) top = base_scope.where("created_at >= (NOW() - INTERVAL " << "#{length[:dur]} #{length[:intv].upcase})") - top.order("(CAST(upvotes AS #{Story.votes_cast_type}) - " << - "CAST(downvotes AS #{Story.votes_cast_type})) DESC") + top.order("#{Story.score_sql} DESC") end private @@ -124,8 +123,7 @@ private end def positive_ranked(scope) - scope.where("(CAST(upvotes AS #{Story.votes_cast_type}) - " << - "CAST(downvotes AS #{Story.votes_cast_type})) >= -1") + scope.where("#{Story.score_sql} >= -1") end def filter_tags(scope, tags)