cast story.{upvotes,downvotes} to signed on mysql, integer on postgres

old mysql doesn't support 'cast(1 as integer)', but new/mariadb does

postgres doesn't support 'cast(1 as signed)'

should fix #145
This commit is contained in:
joshua stein 2014-10-03 17:02:02 -05:00
parent 8b3e62d010
commit 9e8b89be1c
4 changed files with 14 additions and 8 deletions

View file

@ -1,11 +1,11 @@
ThinkingSphinx::Index.define :comment, :with => :active_record do
indexes comment
indexes short_id
indexes user.username, :as => :author
has "(cast(upvotes as integer) - cast(downvotes as integer))",
:as => :score, :type => :bigint, :sortable => true
has "(CAST(upvotes as #{Story.votes_cast_type}) - " <<
"CAST(downvotes as #{Story.votes_cast_type}))", :as => :score,
:type => :bigint, :sortable => true
has is_deleted
has created_at

View file

@ -1,4 +1,3 @@
ThinkingSphinx::Index.define :story, :with => :active_record do
indexes description
indexes short_id
@ -9,8 +8,9 @@ ThinkingSphinx::Index.define :story, :with => :active_record do
has created_at, :sortable => true
has hotness, is_expired
has "(cast(upvotes as integer) - cast(downvotes as integer))",
:as => :score, :type => :bigint, :sortable => true
has "(CAST(upvotes as #{Story.votes_cast_type}) - " <<
"CAST(downvotes as #{Story.votes_cast_type}))", :as => :score,
:type => :bigint, :sortable => true
set_property :field_weights => {
:upvotes => 15,

View file

@ -101,6 +101,10 @@ class Story < ActiveRecord::Base
true
end
def self.votes_cast_type
Story.connection.adapter_name.match(/mysql/i) ? "signed" : "integer"
end
def as_json(options = {})
h = super(:only => [
:short_id,

View file

@ -86,7 +86,8 @@ class StoryRepository
def top(length)
top = base_scope.where("created_at >= (NOW() - INTERVAL " <<
"#{length[:dur]} #{length[:intv].upcase})")
top.order("(CAST(upvotes AS integer) - CAST(downvotes AS integer)) DESC")
top.order("(CAST(upvotes AS #{Story.votes_cast_type}) - " <<
"CAST(downvotes AS #{Story.votes_cast_type})) DESC")
end
private
@ -123,7 +124,8 @@ private
end
def positive_ranked(scope)
scope.where("(CAST(upvotes AS integer) - CAST(downvotes AS integer)) >= -1")
scope.where("(CAST(upvotes AS #{Story.votes_cast_type}) - " <<
"CAST(downvotes AS #{Story.votes_cast_type})) >= -1")
end
def filter_tags(scope, tags)