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 short_id
|
||||||
indexes user.username, :as => :author
|
indexes user.username, :as => :author
|
||||||
|
|
||||||
has "(CAST(upvotes as #{Story.votes_cast_type}) - " <<
|
has Comment.score_sql, :as => :score, :type => :bigint, :sortable => true
|
||||||
"CAST(downvotes as #{Story.votes_cast_type}))", :as => :score,
|
|
||||||
:type => :bigint, :sortable => true
|
|
||||||
|
|
||||||
has is_deleted
|
has is_deleted
|
||||||
has created_at
|
has created_at
|
||||||
|
|
|
@ -9,9 +9,7 @@ ThinkingSphinx::Index.define :story, :with => :active_record do
|
||||||
|
|
||||||
has created_at, :sortable => true
|
has created_at, :sortable => true
|
||||||
has hotness, is_expired
|
has hotness, is_expired
|
||||||
has "(CAST(upvotes as #{Story.votes_cast_type}) - " <<
|
has Story.score_sql, :as => :score, :type => :bigint, :sortable => true
|
||||||
"CAST(downvotes as #{Story.votes_cast_type}))", :as => :score,
|
|
||||||
:type => :bigint, :sortable => true
|
|
||||||
|
|
||||||
set_property :field_weights => {
|
set_property :field_weights => {
|
||||||
:upvotes => 15,
|
:upvotes => 15,
|
||||||
|
|
|
@ -103,6 +103,11 @@ class Comment < ActiveRecord::Base
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.score_sql
|
||||||
|
"(CAST(upvotes AS #{Story.votes_cast_type}) - " <<
|
||||||
|
"CAST(downvotes AS #{Story.votes_cast_type}))"
|
||||||
|
end
|
||||||
|
|
||||||
def as_json(options = {})
|
def as_json(options = {})
|
||||||
h = super(:only => [
|
h = super(:only => [
|
||||||
:short_id,
|
:short_id,
|
||||||
|
|
|
@ -102,6 +102,11 @@ class Story < ActiveRecord::Base
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.score_sql
|
||||||
|
"(CAST(upvotes AS #{votes_cast_type}) - " <<
|
||||||
|
"CAST(downvotes AS #{votes_cast_type}))"
|
||||||
|
end
|
||||||
|
|
||||||
def self.votes_cast_type
|
def self.votes_cast_type
|
||||||
Story.connection.adapter_name.match(/mysql/i) ? "signed" : "integer"
|
Story.connection.adapter_name.match(/mysql/i) ? "signed" : "integer"
|
||||||
end
|
end
|
||||||
|
|
|
@ -86,8 +86,7 @@ class StoryRepository
|
||||||
def top(length)
|
def top(length)
|
||||||
top = base_scope.where("created_at >= (NOW() - INTERVAL " <<
|
top = base_scope.where("created_at >= (NOW() - INTERVAL " <<
|
||||||
"#{length[:dur]} #{length[:intv].upcase})")
|
"#{length[:dur]} #{length[:intv].upcase})")
|
||||||
top.order("(CAST(upvotes AS #{Story.votes_cast_type}) - " <<
|
top.order("#{Story.score_sql} DESC")
|
||||||
"CAST(downvotes AS #{Story.votes_cast_type})) DESC")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -124,8 +123,7 @@ private
|
||||||
end
|
end
|
||||||
|
|
||||||
def positive_ranked(scope)
|
def positive_ranked(scope)
|
||||||
scope.where("(CAST(upvotes AS #{Story.votes_cast_type}) - " <<
|
scope.where("#{Story.score_sql} >= -1")
|
||||||
"CAST(downvotes AS #{Story.votes_cast_type})) >= -1")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter_tags(scope, tags)
|
def filter_tags(scope, tags)
|
||||||
|
|
Loading…
Reference in a new issue