adds order by score to debates

This commit is contained in:
Juanjo Bazán
2015-09-03 18:13:48 +02:00
parent 858adf891f
commit bcb1f2ba72
7 changed files with 29 additions and 21 deletions

View File

@@ -82,7 +82,7 @@ class DebatesController < ApplicationController
end end
def parse_order def parse_order
@valid_orders = ['total_votes', 'created_at', 'likes'] @valid_orders = ['total_votes', 'created_at', 'score']
@order = @valid_orders.include?(params[:order]) ? params[:order] : 'created_at' @order = @valid_orders.include?(params[:order]) ? params[:order] : 'created_at'
end end

View File

@@ -29,7 +29,7 @@ class Debate < ActiveRecord::Base
scope :flagged, -> { where("flags_count > 0") } scope :flagged, -> { where("flags_count > 0") }
scope :for_render, -> { includes(:tags) } scope :for_render, -> { includes(:tags) }
scope :sort_by_total_votes, -> { reorder(cached_votes_total: :desc) } scope :sort_by_total_votes, -> { reorder(cached_votes_total: :desc) }
scope :sort_by_likes , -> { reorder(cached_votes_up: :desc) } scope :sort_by_score , -> { reorder(cached_votes_score: :desc) }
scope :sort_by_created_at, -> { reorder(created_at: :desc) } scope :sort_by_created_at, -> { reorder(created_at: :desc) }
# Ahoy setup # Ahoy setup

View File

@@ -71,7 +71,7 @@ en:
orders: orders:
created_at: newest created_at: newest
total_votes: most voted total_votes: most voted
likes: best rated score: best rated
filter_topic: filter_topic:
one: "You are seeing one debate with the topic '%{topic}'" one: "You are seeing one debate with the topic '%{topic}'"
other: "You are seeing %{count} debates with the topic '%{topic}'" other: "You are seeing %{count} debates with the topic '%{topic}'"

View File

@@ -71,7 +71,7 @@ es:
orders: orders:
created_at: "más nuevos" created_at: "más nuevos"
total_votes: "más votados" total_votes: "más votados"
likes: "mejor valorados" score: "mejor valorados"
filter_topic: filter_topic:
one: "Estás viendo un debate con el tema ''%{topic}''" one: "Estás viendo un debate con el tema ''%{topic}''"
other: "Estás viendo %{count} debates con el tema '%{topic}'" other: "Estás viendo %{count} debates con el tema '%{topic}'"

View File

@@ -0,0 +1,7 @@
class AddCachedVotesScoreToDebate < ActiveRecord::Migration
def change
add_column :debates, :cached_votes_score, :integer, default: 0
add_index :debates, :cached_votes_score
end
end

View File

@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150903142924) do ActiveRecord::Schema.define(version: 20150903155146) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -65,7 +65,6 @@ ActiveRecord::Schema.define(version: 20150903142924) do
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.datetime "hidden_at" t.datetime "hidden_at"
t.integer "children_count", default: 0
t.integer "flags_count", default: 0 t.integer "flags_count", default: 0
t.datetime "ignored_flag_at" t.datetime "ignored_flag_at"
t.integer "moderator_id" t.integer "moderator_id"
@@ -91,8 +90,8 @@ ActiveRecord::Schema.define(version: 20150903142924) do
t.integer "author_id" t.integer "author_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "visit_id"
t.datetime "hidden_at" t.datetime "hidden_at"
t.string "visit_id"
t.integer "flags_count", default: 0 t.integer "flags_count", default: 0
t.datetime "ignored_flag_at" t.datetime "ignored_flag_at"
t.integer "cached_votes_total", default: 0 t.integer "cached_votes_total", default: 0
@@ -101,9 +100,11 @@ ActiveRecord::Schema.define(version: 20150903142924) do
t.integer "comments_count", default: 0 t.integer "comments_count", default: 0
t.datetime "confirmed_hide_at" t.datetime "confirmed_hide_at"
t.integer "cached_anonymous_votes_total", default: 0 t.integer "cached_anonymous_votes_total", default: 0
t.integer "cached_votes_score", default: 0
end end
add_index "debates", ["cached_votes_down"], name: "index_debates_on_cached_votes_down", using: :btree add_index "debates", ["cached_votes_down"], name: "index_debates_on_cached_votes_down", using: :btree
add_index "debates", ["cached_votes_score"], name: "index_debates_on_cached_votes_score", using: :btree
add_index "debates", ["cached_votes_total"], name: "index_debates_on_cached_votes_total", using: :btree add_index "debates", ["cached_votes_total"], name: "index_debates_on_cached_votes_total", using: :btree
add_index "debates", ["cached_votes_up"], name: "index_debates_on_cached_votes_up", using: :btree add_index "debates", ["cached_votes_up"], name: "index_debates_on_cached_votes_up", using: :btree
add_index "debates", ["hidden_at"], name: "index_debates_on_hidden_at", using: :btree add_index "debates", ["hidden_at"], name: "index_debates_on_hidden_at", using: :btree
@@ -199,12 +200,13 @@ ActiveRecord::Schema.define(version: 20150903142924) do
t.string "unconfirmed_email" t.string "unconfirmed_email"
t.boolean "email_on_debate_comment", default: false t.boolean "email_on_debate_comment", default: false
t.boolean "email_on_comment_reply", default: false t.boolean "email_on_comment_reply", default: false
t.string "phone_number", limit: 30
t.string "official_position" t.string "official_position"
t.integer "official_level", default: 0 t.integer "official_level", default: 0
t.datetime "hidden_at" t.datetime "hidden_at"
t.string "sms_confirmation_code" t.string "phone_number", limit: 30
t.string "username" t.string "username"
t.datetime "confirmed_hide_at"
t.string "sms_confirmation_code"
t.string "document_number" t.string "document_number"
t.string "document_type" t.string "document_type"
t.datetime "residence_verified_at" t.datetime "residence_verified_at"
@@ -216,7 +218,6 @@ ActiveRecord::Schema.define(version: 20150903142924) do
t.string "unconfirmed_phone" t.string "unconfirmed_phone"
t.string "confirmed_phone" t.string "confirmed_phone"
t.datetime "letter_requested_at" t.datetime "letter_requested_at"
t.datetime "confirmed_hide_at"
t.string "letter_verification_code" t.string "letter_verification_code"
end end

View File

@@ -354,9 +354,9 @@ feature 'Debates' do
before do before do
@most_voted_debate = create(:debate) @most_voted_debate = create(:debate)
@most_liked_debate = create(:debate) @most_score_debate = create(:debate)
@most_recent_debate = create(:debate) @most_recent_debate = create(:debate)
create_list(:vote, 2, votable: @most_liked_debate) create_list(:vote, 2, votable: @most_score_debate)
create_list(:vote, 2, votable: @most_voted_debate, vote_flag: false) create_list(:vote, 2, votable: @most_voted_debate, vote_flag: false)
create(:vote, votable: @most_voted_debate) create(:vote, votable: @most_voted_debate)
end end
@@ -365,7 +365,7 @@ feature 'Debates' do
visit debates_path visit debates_path
expect(page).to have_select('order-selector', selected: 'newest') expect(page).to have_select('order-selector', selected: 'newest')
expect(@most_recent_debate.title).to appear_before(@most_liked_debate.title) expect(@most_recent_debate.title).to appear_before(@most_score_debate.title)
end end
scenario 'Debates are ordered by most voted' do scenario 'Debates are ordered by most voted' do
@@ -376,8 +376,8 @@ feature 'Debates' do
expect(find("#debates .debate", match: :first)).to have_content(@most_voted_debate.title) expect(find("#debates .debate", match: :first)).to have_content(@most_voted_debate.title)
expect(current_url).to include('order=total_votes') expect(current_url).to include('order=total_votes')
expect(@most_voted_debate.title).to appear_before(@most_liked_debate.title) expect(@most_voted_debate.title).to appear_before(@most_score_debate.title)
expect(@most_liked_debate.title).to appear_before(@most_recent_debate.title) expect(@most_score_debate.title).to appear_before(@most_recent_debate.title)
end end
scenario 'Debates are ordered by best rated' do scenario 'Debates are ordered by best rated' do
@@ -385,11 +385,11 @@ feature 'Debates' do
select 'best rated', from: 'order-selector' select 'best rated', from: 'order-selector'
expect(page).to have_select('order-selector', selected: 'best rated') expect(page).to have_select('order-selector', selected: 'best rated')
expect(find("#debates .debate", match: :first)).to have_content(@most_liked_debate.title) expect(find("#debates .debate", match: :first)).to have_content(@most_score_debate.title)
expect(current_url).to include('order=likes') expect(current_url).to include('order=score')
expect(@most_liked_debate.title).to appear_before(@most_voted_debate.title) expect(@most_score_debate.title).to appear_before(@most_recent_debate.title)
expect(@most_voted_debate.title).to appear_before(@most_recent_debate.title) expect(@most_recent_debate.title).to appear_before(@most_voted_debate.title)
end end
scenario 'Debates are ordered by newest' do scenario 'Debates are ordered by newest' do
@@ -403,8 +403,8 @@ feature 'Debates' do
expect(find("#debates .debate", match: :first)).to have_content(@most_recent_debate.title) expect(find("#debates .debate", match: :first)).to have_content(@most_recent_debate.title)
expect(current_url).to include('order=created_at') expect(current_url).to include('order=created_at')
expect(@most_recent_debate.title).to appear_before(@most_liked_debate.title) expect(@most_recent_debate.title).to appear_before(@most_score_debate.title)
expect(@most_liked_debate.title).to appear_before(@most_voted_debate.title) expect(@most_score_debate.title).to appear_before(@most_voted_debate.title)
end end
end end
end end