consolidate sql fragment to cast story/comment score
This commit is contained in:
parent
6aba206eba
commit
00d347c4f4
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue