Skip to content
This repository has been archived by the owner on Oct 31, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed bug with incorrect sorting by 'tablesorter.js' plugin.
  • Loading branch information
Marcin Brzeziński committed Feb 15, 2013
1 parent 956381e commit 39e58f1
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 50 deletions.
13 changes: 5 additions & 8 deletions app/assets/javascripts/application.js
Expand Up @@ -19,10 +19,12 @@
//= require_self

$(document).ready(function () {
var lang = document.location.pathname.replace(/^\/([^\/]*).*$/, '$1');
var lang = document.location.pathname.replace(/^\/([^\/]*).*$/, '$1')

if (!(lang == "en" || lang == "pl")) {
lang = "en"
}

$('body').on("focus", "input.date_picker", function() {
$(this).datepicker({ dateFormat: "dd.mm.yy" })
$(this).datepicker($.datepicker.setDefaults($.datepicker.regional[lang]))
Expand All @@ -34,13 +36,8 @@ $(document).ready(function () {
}
})

if ($("table.expenses_table").length) {
$("table.expenses_table").tablesorter({
headers: {3: {sorter: false}}
});
}
});
})

$(window).load(function () {
$('div#uvTab').addClass('hidden-phone')
});
})
40 changes: 40 additions & 0 deletions app/assets/javascripts/global/tablesorter.js.coffee
@@ -0,0 +1,40 @@
jQuery ->
$.tablesorter.addParser
id: 'polish'
type: 'text'
is: (s) ->
false
format: (s) ->
s
.toLowerCase()
.replace(/\u0105/g, 'az')
.replace(/\u0119/g, 'ez')
.replace(/\u0107/g, 'cz')
.replace(/\u0144/g, 'nz')
.replace(/ó/g, 'oz')
.replace(/\u0142/g, 'lz')
.replace(/\u015b/g, 'sz')
.replace(/\u017a/g, 'zz') #ź
.replace(/\u017c/g, 'zzz') #ż

lang = document.location.pathname.replace(/^\/([^\/]*).*$/, '$1')

$.tablesorter.addParser
id: 'amount'
type: 'number'
is: (s) ->
false
format: (s) ->
if lang == "en"
parseFloat s.replace(/,/g,'').replace("$",'')
else if lang == "pl"
parseFloat s.replace(',','.').replace(/\s/g,'')
else
s

if $("#expenses_table").length
$("#expenses_table").tablesorter
headers:
0: sorter: 'polish'
1: sorter: 'amount'
3: sorter: false
2 changes: 1 addition & 1 deletion app/views/expenses/index.html.erb
Expand Up @@ -9,7 +9,7 @@
</div>

<div class="hidden-phone">
<table class="expenses_table table table-striped tablesorter">
<table id="expenses_table" class="table table-striped tablesorter">
<thead>
<tr>
<th><%= t('activerecord.attributes.expense.name') %></th>
Expand Down
39 changes: 37 additions & 2 deletions test/fixtures/expenses.yml
Expand Up @@ -130,14 +130,14 @@ expense_aaa:
name: AAA
amount: 100
execution_date: <%= Date.today %>
wallet: wallet_for_destroy_and_check_order_of_expenses
wallet: wallet_for_destroy
user: user_with_expenses

expense_bbb:
name: BBB
amount: 200
execution_date: <%= Date.today %>
wallet: wallet_for_destroy_and_check_order_of_expenses
wallet: wallet_for_destroy
user: user_with_expenses

expense_for_user_with_low_balance:
Expand All @@ -155,3 +155,38 @@ expense_for_user_without_low_balance:
wallet: wallet_for_user_without_low_balance
user: user_without_low_balance
family: family_of_user_without_low_balance

expense_for_user_with_locale_en_1:
name: śniadanie
amount: 12.40
execution_date: <%= 1.days.ago %>
wallet: wallet_for_family_with_users_pl_and_en
user: user_with_locale_en

expense_for_user_with_locale_en_2:
name: bułki
amount: 12.00
execution_date: <%= 1.days.ago %>
wallet: wallet_for_family_with_users_pl_and_en
user: user_with_locale_en

expense_for_user_with_locale_en_3:
name: Bułka
amount: 1.01
execution_date: <%= 1.days.ago %>
wallet: wallet_for_family_with_users_pl_and_en
user: user_with_locale_en

expense_for_user_with_locale_pl_1:
name: szlafrok
amount: 1.12
execution_date: <%= 1.days.ago %>
wallet: wallet_for_family_with_users_pl_and_en
user: user_with_locale_pl

expense_for_user_with_locale_pl_2:
name: audi Q11
amount: 12000121.12
execution_date: <%= 1.days.ago %>
wallet: wallet_for_family_with_users_pl_and_en
user: user_with_locale_pl
9 changes: 4 additions & 5 deletions test/fixtures/families.yml
Expand Up @@ -16,11 +16,10 @@ family_of_user_without_income:
family_of_user_with_wallet_3:
users: user_with_wallet_3

family_of_user_with_locale_pl:
users: user_with_locale_pl

family_of_user_with_locale_en:
users: user_with_locale_en
family_of_users_with_locale_pl_and_en:
users:
- user_with_locale_pl
- user_with_locale_en

family_of_user_with_no_locale:
users: user_with_no_locale
Expand Down
17 changes: 6 additions & 11 deletions test/fixtures/wallets.yml
Expand Up @@ -28,16 +28,6 @@ home_1500_dollars:
amount: 1500
family: family_of_user_with_wallet_3

home_2000_dollars:
name: Home
amount: 2000
family: family_of_user_with_locale_pl

home_2500_dollars:
name: Home
amount: 2500
family: family_of_user_with_locale_en

wallet_without_expenses:
name: Test 10
amount: 1000
Expand All @@ -63,7 +53,7 @@ wallet_for_user_without_low_balance:
amount: 1000
family: family_of_user_without_low_balance

wallet_for_destroy_and_check_order_of_expenses:
wallet_for_destroy:
name: Order
amount: 15000
family: family_of_user_with_expenses
Expand All @@ -72,3 +62,8 @@ empty_wallet_for_destroy:
name: empty
amount: 15000
family: family_of_user_with_expenses

wallet_for_family_with_users_pl_and_en:
name: Food
amount: 10000
family: family_of_users_with_locale_pl_and_en
22 changes: 0 additions & 22 deletions test/integration/expenses_order_test.rb

This file was deleted.

89 changes: 89 additions & 0 deletions test/integration/tablesorter_test.rb
@@ -0,0 +1,89 @@
#encoding: utf-8
require 'test_helper'

class TablesorterTest < ActionDispatch::IntegrationTest
def setup
@user_pl = users(:user_with_locale_pl)
@user_en = users(:user_with_locale_en)
end

test "should sort expenses by name" do
allow_google_login_as(@user_pl)
visit '/pl'
log_in_pl
click_on I18n.t('header.expenses', locale: 'pl')

expenses_asc = [
'audi Q11',
'Bułka',
'bułki',
'szlafrok',
'śniadanie'
]

# ASC ORDER
expenses_table_sort_by 1
expenses_table_check_order 1, expenses_asc

# DESC ORDER
expenses_table_sort_by 1
expenses_table_check_order 1, expenses_asc.reverse
end

test "should sort expenses by amount in pl format" do
allow_google_login_as(@user_pl)
visit '/pl'
log_in_pl
click_on I18n.t('header.expenses', locale: 'pl')

expenses_amounts_asc = [
'1,01 zł',
'1,12 zł',
'12,00 zł',
'12,40 zł',
'12 000 121,12 zł'
]

# ASC ORDER
expenses_table_sort_by 2
expenses_table_check_order 2, expenses_amounts_asc

# DESC ORDER
expenses_table_sort_by 2
expenses_table_check_order 2, expenses_amounts_asc.reverse
end

test "should sort expenses by amount in en format" do
allow_google_login_as(@user_en)
visit '/en'
log_in_en
click_on I18n.t('header.expenses', locale: 'en')

expenses_amounts_asc = [
'$1.01',
'$1.12',
'$12.00',
'$12.40',
'$12,000,121.12'
]

# ASC ORDER
expenses_table_sort_by 2
expenses_table_check_order 2, expenses_amounts_asc

# DESC ORDER
expenses_table_sort_by 2
expenses_table_check_order 2, expenses_amounts_asc.reverse
end

private
def expenses_table_sort_by(column_number)
find(:xpath, "//table[@id='expenses_table']/thead/tr[1]/th[#{column_number}]").click()
end

def expenses_table_check_order(column_number, array)
array.each do |e|
assert_equal e, find(:xpath, "//table[@id='expenses_table']/tbody/tr[#{array.index(e)+1}]/td[#{column_number}]").text
end
end
end

Large diffs are not rendered by default.

0 comments on commit 39e58f1

Please sign in to comment.