Add to javascript to handle debates order change

This commit is contained in:
Marko Lovic
2015-08-27 14:54:04 +02:00
parent bfeff8d241
commit e8004ca1d3
6 changed files with 41 additions and 16 deletions

View File

@@ -32,6 +32,7 @@ var initialize_modules = function() {
App.Tags.initialize();
App.Stats.initialize();
App.LocaleSwitcher.initialize();
App.DebatesOrderSelector.initialize();
};
$(function(){

View File

@@ -0,0 +1,17 @@
App.DebatesOrderSelector =
href_with_params: (query_params) ->
loc = window.location
loc.protocol + "//" + loc.hostname +
(if loc.port then ':' + loc.port else '') +
loc.pathname +
loc.hash +
'?' + $.param(query_params)
initialize: ->
$('.js-order-selector').on 'change', ->
query_params = window.getQueryParameters()
query_params['order'] = $(this).val()
window.location.assign(App.DebatesOrderSelector.href_with_params(query_params))

View File

@@ -0,0 +1,12 @@
module DebatesHelper
def available_options_for_order_selector(valid_orders, current_order)
options_for_select(available_order_filters_array(valid_orders), current_order)
end
private
def available_order_filters_array(orders)
orders.map { |f| [t("debates.index.order_#{f}"), f] }
end
end

View File

@@ -1,7 +1,5 @@
<% options = @valid_orders.map { |f| [t("debates.index.order_#{f}"), f] } %>
<%= form_tag('/debates', method: 'get', class: 'inline-block') do %>
<%= select_tag 'order', options_for_select(options, @order), onchange: "this.form.submit()" %>
<% end %>
<form class="inline-block">
<select class="js-order-selector" name="order-selector">
<%= available_options_for_order_selector(@valid_orders, @order) %>
</select>
</form>

View File

@@ -365,17 +365,15 @@ feature 'Debates' do
scenario 'Default order is created_at' do
visit debates_path
expect(page).to have_select('order', selected: 'the newest')
#expect_debate_order([2, 1, 0])
expect(page).to have_select('order-selector', selected: 'the newest')
expect(@most_recent_debate.title).to appear_before(@most_liked_debate.title)
expect(@most_liked_debate.title).to appear_before(@most_voted_debate.title)
end
scenario 'Debates are ordered by most voted' do
visit debates_path
select 'the most voted', from: 'order'
expect(page).to have_select('order', selected: 'the most voted')
select 'the most voted', from: 'order-selector'
expect(page).to have_select('order-selector', selected: 'the most voted')
expect(find("#debates .debate", match: :first)).to have_content(@most_voted_debate.title) # Necessary to force capybara to wait for redirect
expect(current_url).to include('order=total_votes')
@@ -386,7 +384,7 @@ feature 'Debates' do
scenario 'Debates are ordered by best rated' do
visit debates_path
select 'the best rated', from: 'order'
select 'the best rated', from: 'order-selector'
expect(find("#debates .debate", match: :first)).to have_content(@most_liked_debate.title)
expect(current_url).to include('order=likes')
@@ -397,10 +395,10 @@ feature 'Debates' do
scenario 'Debates are ordered by newest' do
visit debates_path
select 'the most voted', from: 'order'
select 'the most voted', from: 'order-selector'
expect(find("#debates .debate", match: :first)).to have_content(@most_voted_debate.title)
select 'the newest', from: 'order'
select 'the newest', from: 'order-selector'
expect(find("#debates .debate", match: :first)).to have_content(@most_recent_debate.title)
expect(current_url).to include('order=created_at')

View File

@@ -1,5 +1,4 @@
require 'rails_helper'
require 'support/matchers/appear_before'
feature 'Admin' do
let(:user) { create(:user) }