diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 3845afebf..5219c1308 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -82,7 +82,7 @@ class DebatesController < ApplicationController end def parse_order - @valid_orders = ['total_votes', 'created_at', 'likes'] + @valid_orders = ['created_at', 'score', 'most_commented', 'random'] @order = @valid_orders.include?(params[:order]) ? params[:order] : 'created_at' end diff --git a/app/models/debate.rb b/app/models/debate.rb index 61e6546b4..25ba8aca8 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -28,9 +28,10 @@ class Debate < ActiveRecord::Base scope :with_ignored_flag, -> { where("ignored_flag_at IS NOT NULL AND hidden_at IS NULL") } scope :flagged, -> { where("flags_count > 0") } scope :for_render, -> { includes(:tags) } - 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_most_commented, -> { reorder(comments_count: :desc) } + scope :sort_by_random, -> { reorder("RANDOM()") } # Ahoy setup visitable # Ahoy will automatically assign visit_id on create diff --git a/config/locales/en.yml b/config/locales/en.yml index 23c7a25e9..c4583ffd8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -70,8 +70,9 @@ en: select_order_long: Order debates by orders: created_at: newest - total_votes: most voted - likes: best rated + score: best rated + most_commented: most commented + random: random filter_topic: one: "You are seeing one debate with the topic '%{topic}'" other: "You are seeing %{count} debates with the topic '%{topic}'" diff --git a/config/locales/es.yml b/config/locales/es.yml index 0c400b4b0..62ab28997 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -70,8 +70,9 @@ es: select_order_long: Estás viendo los debates orders: created_at: "más nuevos" - total_votes: "más votados" - likes: "mejor valorados" + score: "mejor valorados" + most_commented: "más comentados" + random: "aleatorio" filter_topic: one: "Estás viendo un debate con el tema ''%{topic}''" other: "Estás viendo %{count} debates con el tema '%{topic}'" diff --git a/db/migrate/20150903155146_add_cached_votes_score_to_debate.rb b/db/migrate/20150903155146_add_cached_votes_score_to_debate.rb new file mode 100644 index 000000000..093b13671 --- /dev/null +++ b/db/migrate/20150903155146_add_cached_votes_score_to_debate.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 64eb92dda..d561bcccd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # 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 enable_extension "plpgsql" @@ -65,7 +65,6 @@ ActiveRecord::Schema.define(version: 20150903142924) do t.datetime "created_at" t.datetime "updated_at" t.datetime "hidden_at" - t.integer "children_count", default: 0 t.integer "flags_count", default: 0 t.datetime "ignored_flag_at" t.integer "moderator_id" @@ -91,8 +90,8 @@ ActiveRecord::Schema.define(version: 20150903142924) do t.integer "author_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.string "visit_id" t.datetime "hidden_at" + t.string "visit_id" t.integer "flags_count", default: 0 t.datetime "ignored_flag_at" t.integer "cached_votes_total", default: 0 @@ -101,9 +100,11 @@ ActiveRecord::Schema.define(version: 20150903142924) do t.integer "comments_count", default: 0 t.datetime "confirmed_hide_at" t.integer "cached_anonymous_votes_total", default: 0 + t.integer "cached_votes_score", default: 0 end 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_up"], name: "index_debates_on_cached_votes_up", 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.boolean "email_on_debate_comment", default: false t.boolean "email_on_comment_reply", default: false - t.string "phone_number", limit: 30 t.string "official_position" t.integer "official_level", default: 0 t.datetime "hidden_at" - t.string "sms_confirmation_code" + t.string "phone_number", limit: 30 t.string "username" + t.datetime "confirmed_hide_at" + t.string "sms_confirmation_code" t.string "document_number" t.string "document_type" t.datetime "residence_verified_at" @@ -216,7 +218,6 @@ ActiveRecord::Schema.define(version: 20150903142924) do t.string "unconfirmed_phone" t.string "confirmed_phone" t.datetime "letter_requested_at" - t.datetime "confirmed_hide_at" t.string "letter_verification_code" end diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index 935da21f6..d45e1c46f 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -353,31 +353,22 @@ feature 'Debates' do feature 'Debate index order filters', :js do before do - @most_voted_debate = create(:debate) - @most_liked_debate = create(:debate) - @most_recent_debate = create(:debate) - create_list(:vote, 2, votable: @most_liked_debate) - create_list(:vote, 2, votable: @most_voted_debate, vote_flag: false) - create(:vote, votable: @most_voted_debate) + @most_commented_debate = create(:debate) + @most_score_debate = create(:debate) + @most_recent_debate = create(:debate) + create_list(:comment, 2, commentable: @most_commented_debate) + create_list(:vote, 2, votable: @most_score_debate) + create_list(:vote, 2, votable: @most_recent_debate, vote_flag: false) + create(:vote, votable: @most_recent_debate) + create(:comment, commentable: @most_recent_debate) end scenario 'Default order is created_at' do visit debates_path expect(page).to have_select('order-selector', selected: 'newest') - expect(@most_recent_debate.title).to appear_before(@most_liked_debate.title) - end - - scenario 'Debates are ordered by most voted' do - visit debates_path - - select 'most voted', from: 'order-selector' - expect(page).to have_select('order-selector', selected: 'most voted') - expect(find("#debates .debate", match: :first)).to have_content(@most_voted_debate.title) - - expect(current_url).to include('order=total_votes') - expect(@most_voted_debate.title).to appear_before(@most_liked_debate.title) - expect(@most_liked_debate.title).to appear_before(@most_recent_debate.title) + expect(@most_recent_debate.title).to appear_before(@most_score_debate.title) + expect(@most_score_debate.title).to appear_before(@most_commented_debate.title) end scenario 'Debates are ordered by best rated' do @@ -385,26 +376,57 @@ feature 'Debates' do select 'best rated', from: 'order-selector' 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(@most_liked_debate.title).to appear_before(@most_voted_debate.title) - expect(@most_voted_debate.title).to appear_before(@most_recent_debate.title) + expect(current_url).to include('order=score') + expect(@most_score_debate.title).to appear_before(@most_commented_debate.title) + expect(@most_commented_debate.title).to appear_before(@most_recent_debate.title) + end + + scenario 'Debates are ordered by most commented' do + visit debates_path + + select 'most commented', from: 'order-selector' + expect(page).to have_select('order-selector', selected: 'most commented') + expect(find("#debates .debate", match: :first)).to have_content(@most_commented_debate.title) + + expect(current_url).to include('order=most_commented') + expect(@most_commented_debate.title).to appear_before(@most_recent_debate.title) + expect(@most_recent_debate.title).to appear_before(@most_score_debate.title) end scenario 'Debates are ordered by newest' do visit debates_path - select 'most voted', from: 'order-selector' - expect(find("#debates .debate", match: :first)).to have_content(@most_voted_debate.title) + select 'best rated', from: 'order-selector' + expect(find("#debates .debate", match: :first)).to have_content(@most_score_debate.title) select 'newest', from: 'order-selector' expect(page).to have_select('order-selector', selected: 'newest') expect(find("#debates .debate", match: :first)).to have_content(@most_recent_debate.title) expect(current_url).to include('order=created_at') - expect(@most_recent_debate.title).to appear_before(@most_liked_debate.title) - expect(@most_liked_debate.title).to appear_before(@most_voted_debate.title) + expect(@most_recent_debate.title).to appear_before(@most_score_debate.title) + expect(@most_score_debate.title).to appear_before(@most_commented_debate.title) + end + + scenario 'Debates are ordered randomly' do + create_list(:debate, 12) + visit debates_path + + select 'random', from: 'order-selector' + expect(page).to have_select('order-selector', selected: 'random') + debates_first_time = find("#debates").text + + select 'most commented', from: 'order-selector' + expect(page).to have_select('order-selector', selected: 'most commented') + expect(find("#debates .debate", match: :first)).to have_content(@most_commented_debate.title) + + select 'random', from: 'order-selector' + expect(page).to have_select('order-selector', selected: 'random') + debates_second_time = find("#debates").text + + expect(debates_first_time).to_not eq(debates_second_time) end end end