sorting_table_for 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README.mdown +213 -0
- data/Rakefile +11 -0
- data/init.rb +7 -0
- data/lib/model/sorting_table_model_scope.rb +53 -0
- data/lib/sorting_table_for.rb +37 -0
- data/lib/sorting_table_for/i18n.rb +36 -0
- data/lib/sorting_table_for/table_builder.rb +366 -0
- data/lib/sorting_table_for/tools.rb +43 -0
- data/spec/db/database.yml +3 -0
- data/spec/db/schema.rb +20 -0
- data/spec/fixtures/user.rb +24 -0
- data/spec/helpers/builder_spec.rb +47 -0
- data/spec/helpers/cell_value_spec.rb +85 -0
- data/spec/helpers/column_spec.rb +359 -0
- data/spec/helpers/header_spec.rb +337 -0
- data/spec/helpers/i18n_spec.rb +54 -0
- data/spec/locales/test.yml +92 -0
- data/spec/locales/test_rails3.yml +92 -0
- data/spec/model/sorting_table_model_scope_spec.rb +89 -0
- data/spec/spec_helper.rb +70 -0
- metadata +89 -0
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module SortingTableFor
|
4
|
+
module Tools
|
5
|
+
extend self
|
6
|
+
|
7
|
+
##
|
8
|
+
## This module is a copy from Formtastic : http://github.com/justinfrench/formtastic
|
9
|
+
##
|
10
|
+
|
11
|
+
## Rails XSS Safety
|
12
|
+
|
13
|
+
# Returns the given text, marked as being HTML-safe.
|
14
|
+
# With older versions of the Rails XSS-safety mechanism,
|
15
|
+
# this destructively modifies the HTML-safety of `text`.
|
16
|
+
#
|
17
|
+
# @param text [String]
|
18
|
+
# @return [String] `text`, marked as HTML-safe
|
19
|
+
def html_safe(text)
|
20
|
+
return text if !text
|
21
|
+
return text.html_safe if defined?(ActiveSupport::SafeBuffer)
|
22
|
+
return text.html_safe! if text.respond_to?(:html_safe!)
|
23
|
+
text
|
24
|
+
end
|
25
|
+
|
26
|
+
def rails_safe_buffer_class
|
27
|
+
# It's important that we check ActiveSupport first,
|
28
|
+
# because in Rails 2.3.6 ActionView::SafeBuffer exists
|
29
|
+
# but is a deprecated proxy object.
|
30
|
+
return ActiveSupport::SafeBuffer if defined?(ActiveSupport::SafeBuffer)
|
31
|
+
return ActionView::SafeBuffer
|
32
|
+
end
|
33
|
+
|
34
|
+
def rails3?
|
35
|
+
version=
|
36
|
+
if defined?(ActionPack::VERSION::MAJOR)
|
37
|
+
ActionPack::VERSION::MAJOR
|
38
|
+
end
|
39
|
+
!version.blank? && version >= 3
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
data/spec/db/schema.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
|
3
|
+
create_table :users, :force => true do |t|
|
4
|
+
|
5
|
+
t.string :username
|
6
|
+
t.string :firstname
|
7
|
+
t.string :lastname
|
8
|
+
|
9
|
+
t.integer :position
|
10
|
+
t.integer :salary
|
11
|
+
t.integer :price
|
12
|
+
|
13
|
+
t.boolean :active
|
14
|
+
|
15
|
+
t.datetime :created_at
|
16
|
+
t.datetime :updated_at
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class User < ActiveRecord::Base
|
2
|
+
if ::SortingTableFor::Tools::rails3?
|
3
|
+
scope :good_position, :conditions => 'position > 3'
|
4
|
+
scope :set_limit, lambda { |limit| { :limit => limit } }
|
5
|
+
else
|
6
|
+
named_scope :good_position, :conditions => 'position > 3'
|
7
|
+
named_scope :set_limit, lambda { |limit| { :limit => limit } }
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
20.times do |n|
|
13
|
+
User.create(
|
14
|
+
:username => "my_usename_#{n}",
|
15
|
+
:firstname => "my_firstname_#{n}",
|
16
|
+
:lastname => "my_lastname_#{n}",
|
17
|
+
:position => n + 2,
|
18
|
+
:salary => n * 424,
|
19
|
+
:price => n * 3,
|
20
|
+
:active => ((n % 2) == 0) ? true : false,
|
21
|
+
:created_at => DateTime.now - 5.days,
|
22
|
+
:updated_at => DateTime.now - 2.hours
|
23
|
+
)
|
24
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + '/../fixtures/user')
|
5
|
+
|
6
|
+
include SortingTableForSpecHelper
|
7
|
+
|
8
|
+
describe SortingTableFor, :type => :helper do
|
9
|
+
|
10
|
+
before :each do
|
11
|
+
@users = User.all
|
12
|
+
helper.stub!(:url_for).and_return('fake_link')
|
13
|
+
helper.stub!(:params).and_return({ :controller => 'fakes', :action => 'index' })
|
14
|
+
helper.output_buffer = ''
|
15
|
+
end
|
16
|
+
|
17
|
+
describe ' #construct table' do
|
18
|
+
|
19
|
+
it "should raise and error" do
|
20
|
+
expect {
|
21
|
+
helper.sorting_table_for(@users)
|
22
|
+
}.to raise_error { ArgumentError }
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should have balise table with class" do
|
26
|
+
table_html = helper.sorting_table_for(@users) {}
|
27
|
+
helper.output_buffer.concat(table_html)
|
28
|
+
helper.output_buffer.should have_comp_tag("table[class=sorting_table_for]")
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should be custom by html" do
|
32
|
+
table_html = helper.sorting_table_for(@users, :html => { :class => 'hello', :id => 'my_test' }) {}
|
33
|
+
helper.output_buffer.concat(table_html)
|
34
|
+
helper.output_buffer.should have_comp_tag("table[class='hello sorting_table_for'][id='my_test']")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should take another builder" do
|
38
|
+
class MyNewBuilder < SortingTableFor::TableBuilder
|
39
|
+
end
|
40
|
+
helper.sorting_table_for(@users, :builder => MyNewBuilder) do |builder|
|
41
|
+
builder.class.should == MyNewBuilder
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + '/../fixtures/user')
|
5
|
+
|
6
|
+
include SortingTableForSpecHelper
|
7
|
+
|
8
|
+
describe SortingTableFor, :type => :helper do
|
9
|
+
|
10
|
+
before :each do
|
11
|
+
@users = User.all
|
12
|
+
helper.stub!(:url_for).and_return('fake_link')
|
13
|
+
helper.stub!(:params).and_return({ :controller => 'fakes', :action => 'index' })
|
14
|
+
helper.output_buffer = ''
|
15
|
+
SortingTableFor::TableBuilder.default_boolean = [I18n.t(:bool_true, :scope => [:sorting_table_for, :columns]), I18n.t(:bool_false, :scope => [:sorting_table_for, :columns])]
|
16
|
+
SortingTableFor::TableBuilder.i18n_default_format_date = :default
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should format by default values" do
|
20
|
+
helper.sorting_table_for(@users) do |table|
|
21
|
+
html = table.columns
|
22
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(1)", :text => @users.first.username)
|
23
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(2)", :text => @users.first.firstname)
|
24
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(3)", :text => @users.first.lastname)
|
25
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(4)", :text => @users.first.position.to_s)
|
26
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(5)", :text => @users.first.salary.to_s)
|
27
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(6)", :text => number_to_currency(@users.first.price))
|
28
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(7)", :text => "True")
|
29
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(8)", :text => I18n.l(@users.first.created_at, :format => :default))
|
30
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(9)", :text => I18n.l(@users.first.updated_at, :format => :default))
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should works with as" do
|
35
|
+
current_datetime = DateTime.now
|
36
|
+
helper.sorting_table_for(@users) do |table|
|
37
|
+
html = table.columns do |value|
|
38
|
+
table.column :price
|
39
|
+
table.column value.price, :as => :currency
|
40
|
+
table.column current_datetime, :as => :time
|
41
|
+
table.column current_datetime, :as => :date
|
42
|
+
table.column current_datetime, :as => :time, :format => :short
|
43
|
+
table.column current_datetime, :as => :date, :format => :short
|
44
|
+
table.column true
|
45
|
+
table.column :active
|
46
|
+
end
|
47
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(1)", :text => number_to_currency(@users.first.price))
|
48
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(2)", :text => number_to_currency(@users.first.price))
|
49
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(3)", :text => I18n.l(current_datetime, :format => :default))
|
50
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(4)", :text => I18n.l(current_datetime.to_date, :format => :default))
|
51
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(5)", :text => I18n.l(current_datetime, :format => :short))
|
52
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(6)", :text => I18n.l(current_datetime.to_date, :format => :short))
|
53
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(7)", :text => "True")
|
54
|
+
html.should have_comp_tag("tr:nth-child(3) td:nth-child(8)", :text => "False")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should works with boolean option" do
|
59
|
+
SortingTableFor::TableBuilder.default_boolean = ['BoolTrue', 'BoolFalse']
|
60
|
+
helper.sorting_table_for(@users) do |table|
|
61
|
+
html = table.columns do
|
62
|
+
table.column true
|
63
|
+
table.column :active
|
64
|
+
end
|
65
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(1)", :text => "BoolTrue")
|
66
|
+
html.should have_comp_tag("tr:nth-child(3) td:nth-child(2)", :text => "BoolFalse")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should works with format date option" do
|
71
|
+
SortingTableFor::TableBuilder.i18n_default_format_date = :short
|
72
|
+
current_datetime = DateTime.now
|
73
|
+
helper.sorting_table_for(@users) do |table|
|
74
|
+
html = table.columns do |value|
|
75
|
+
table.column :created_at
|
76
|
+
table.column current_datetime, :as => :time
|
77
|
+
table.column current_datetime, :as => :time, :format => :default
|
78
|
+
end
|
79
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(1)", :text => I18n.l(@users.first.created_at, :format => :short))
|
80
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(2)", :text => I18n.l(current_datetime, :format => :short))
|
81
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(3)", :text => I18n.l(current_datetime, :format => :default))
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
@@ -0,0 +1,359 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + '/../fixtures/user')
|
5
|
+
|
6
|
+
include SortingTableForSpecHelper
|
7
|
+
|
8
|
+
describe SortingTableFor, :type => :helper do
|
9
|
+
|
10
|
+
before :each do
|
11
|
+
@users = User.all
|
12
|
+
helper.stub!(:url_for).and_return('fake_link')
|
13
|
+
helper.stub!(:params).and_return({ :controller => 'fakes', :action => 'index' })
|
14
|
+
helper.output_buffer = ''
|
15
|
+
end
|
16
|
+
|
17
|
+
describe ' #default usage' do
|
18
|
+
|
19
|
+
it "should works" do
|
20
|
+
helper.sorting_table_for(@users) do |table|
|
21
|
+
html = table.columns
|
22
|
+
html.should have_comp_tag("tbody", :count => 1)
|
23
|
+
html.should have_comp_tag("tr", :count => (@users.size + 1))
|
24
|
+
html.should have_comp_tag("td", :count => (@users.size * (User.content_columns.size + 2)) + 1)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should add total entries" do
|
29
|
+
helper.sorting_table_for(@users) do |table|
|
30
|
+
table.columns.should match(@users.size.to_s)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should add colspan for total entries" do
|
35
|
+
helper.sorting_table_for(@users) do |table|
|
36
|
+
table.columns.should match("<td colspan=\"" + (User.content_columns.size + 2).to_s + "\">")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should add i18n to total entries" do
|
41
|
+
helper.sorting_table_for(@users) do |table|
|
42
|
+
table.columns.should match("Total Entries " + @users.size.to_s)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should add colspan for total entries" do
|
47
|
+
helper.sorting_table_for(@users) do |table|
|
48
|
+
table.columns.should match("<td colspan=\"" + (User.content_columns.size + 2).to_s + "\">")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should add link columns" do
|
53
|
+
helper.sorting_table_for(@users) do |table|
|
54
|
+
table.columns.should have_comp_tag('a', :count => (@users.size * 2))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should add i18n to link columns" do
|
59
|
+
helper.sorting_table_for(@users) do |table|
|
60
|
+
html = table.columns
|
61
|
+
html.should have_comp_tag('a', :text => 'Edit', :count => @users.size)
|
62
|
+
html.should have_comp_tag('a', :text => 'Delete', :count => @users.size)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should add class odd/even" do
|
67
|
+
helper.sorting_table_for(@users) do |table|
|
68
|
+
html = table.columns
|
69
|
+
html.should have_comp_tag("tr[class=odd]", :count => (@users.size / 2))
|
70
|
+
html.should have_comp_tag("tr[class=even]", :count => (@users.size / 2))
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should customize html" do
|
75
|
+
table_html = helper.sorting_table_for(@users, :html => { :class => 'table_class', :id => 'table_id' }) do |table|
|
76
|
+
html = table.columns :html => { :class => 'hello_class', :id => 'hello_id', :title => 'hello_title' }
|
77
|
+
html.should have_comp_tag("tr:nth-child(2)[class='hello_class odd']")
|
78
|
+
html.should have_comp_tag("tr:nth-child(2)[id=hello_id]")
|
79
|
+
html.should have_comp_tag("tr:nth-child(2)[title=hello_title]")
|
80
|
+
end
|
81
|
+
helper.output_buffer.concat(table_html)
|
82
|
+
helper.output_buffer.should have_comp_tag("table[class='table_class sorting_table_for'][id=table_id]")
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
describe " #table columns" do
|
88
|
+
|
89
|
+
it "should works with one column" do
|
90
|
+
helper.sorting_table_for(@users) do |table|
|
91
|
+
html = table.columns :username
|
92
|
+
html.should have_comp_tag("tbody", :count => 1)
|
93
|
+
html.should have_comp_tag("tr", :count => (@users.size + 1))
|
94
|
+
html.should have_comp_tag("td", :count => (@users.size + 1))
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should have multi columns" do
|
99
|
+
helper.sorting_table_for(@users) do |table|
|
100
|
+
html = table.columns :username, :price
|
101
|
+
html.should have_comp_tag("tbody", :count => 1)
|
102
|
+
html.should have_comp_tag("tr", :count => (@users.size + 1))
|
103
|
+
html.should have_comp_tag("td", :count => ((@users.size * 2) + 1))
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should have multi columns and action" do
|
108
|
+
helper.sorting_table_for(@users) do |table|
|
109
|
+
table.columns(:username, :price, :actions => :edit).should have_comp_tag("td", :count => ((@users.size * 3) + 1))
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should have multi columns and multi action" do
|
114
|
+
helper.sorting_table_for(@users) do |table|
|
115
|
+
table.columns(:username, :price, :actions => [:edit, :delete]).should have_comp_tag("td", :count => ((@users.size * 4) + 1))
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should do nothing if the key isn't correct" do
|
120
|
+
helper.sorting_table_for(@users) do |table|
|
121
|
+
table.columns(:username, :price, :blabla => :edit).should have_comp_tag("td", :count => ((@users.size * 2) + 1))
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should works symbol and non symbol" do
|
126
|
+
helper.sorting_table_for(@users) do |table|
|
127
|
+
html = table.columns(:username, 'hello', image_tag('rails.png'), :actions => :edit)
|
128
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(1)", :text => @users.first.username)
|
129
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(2)", :text => 'hello')
|
130
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(3) img")
|
131
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(4) a")
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should works with non key symbol" do
|
136
|
+
helper.sorting_table_for(@users) do |table|
|
137
|
+
html = table.columns(:username, :test)
|
138
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(1)", :text => @users.first.username)
|
139
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(2)", :text => '')
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should add remote on link" do
|
144
|
+
helper.sorting_table_for(@users, :link_remote => true) do |table|
|
145
|
+
html = table.columns :username, :actions => :edit
|
146
|
+
if SortingTableFor::Tools::rails3?
|
147
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(2) a[data-remote]")
|
148
|
+
else
|
149
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(2) a[onclick]")
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should customize html" do
|
155
|
+
table_html = helper.sorting_table_for(@users, :html => { :class => 'table_class', :id => 'table_id' }) do |table|
|
156
|
+
html = table.columns :username, :price, :html => { :class => 'hello_class', :id => 'hello_id', :title => 'hello_title' }
|
157
|
+
html.should have_comp_tag("tr:nth-child(2)[class='hello_class odd'][id=hello_id][title=hello_title]")
|
158
|
+
end
|
159
|
+
helper.output_buffer.concat(table_html)
|
160
|
+
helper.output_buffer.should have_comp_tag("table[class='table_class sorting_table_for'][id=table_id]")
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
describe " #table column" do
|
166
|
+
|
167
|
+
it "should works with one column" do
|
168
|
+
helper.sorting_table_for(@users) do |table|
|
169
|
+
html = table.columns do
|
170
|
+
table.column :username
|
171
|
+
end
|
172
|
+
html.should have_comp_tag("tbody", :count => 1)
|
173
|
+
html.should have_comp_tag("tr", :count => (@users.size + 1))
|
174
|
+
html.should have_comp_tag("td", :count => (@users.size + 1))
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should have multi columns" do
|
179
|
+
helper.sorting_table_for(@users) do |table|
|
180
|
+
html = table.columns do
|
181
|
+
table.column :username
|
182
|
+
table.column :price
|
183
|
+
end
|
184
|
+
html.should have_comp_tag("tbody", :count => 1)
|
185
|
+
html.should have_comp_tag("tr", :count => (@users.size + 1))
|
186
|
+
html.should have_comp_tag("td", :count => ((@users.size * 2) + 1))
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
it "should works with multi types" do
|
191
|
+
helper.sorting_table_for(@users) do |table|
|
192
|
+
html = table.columns do
|
193
|
+
table.column :username
|
194
|
+
table.column :test
|
195
|
+
table.column 'hello'
|
196
|
+
table.column image_tag('rails.png')
|
197
|
+
table.column :action => :edit
|
198
|
+
end
|
199
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(1)", :text => @users.first.username)
|
200
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(2)", :text => '')
|
201
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(3)", :text => 'hello')
|
202
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(4) img")
|
203
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(5) a")
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
it "should customize html" do
|
208
|
+
table_html = helper.sorting_table_for(@users, :html => { :class => 'table_class', :id => 'table_id' }) do |table|
|
209
|
+
html = table.columns :html => { :class => 'hello_class', :id => 'hello_id', :title => 'hello_title' } do
|
210
|
+
table.column :username, :html => { :class => 'hi_class', :id => 'hi_id', :title => 'hi_title' }
|
211
|
+
end
|
212
|
+
html.should have_comp_tag("tr:nth-child(2)[class='hello_class odd'][id=hello_id][title=hello_title]")
|
213
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(1)[class='hi_class'][id=hi_id][title=hi_title]")
|
214
|
+
end
|
215
|
+
helper.output_buffer.concat(table_html)
|
216
|
+
helper.output_buffer.should have_comp_tag("table[class='table_class sorting_table_for'][id=table_id]")
|
217
|
+
end
|
218
|
+
|
219
|
+
end
|
220
|
+
|
221
|
+
describe " #table column with value" do
|
222
|
+
|
223
|
+
it "should have the row collection" do
|
224
|
+
helper.sorting_table_for(@users) do |table|
|
225
|
+
html = table.columns do |value|
|
226
|
+
table.column value.username
|
227
|
+
end
|
228
|
+
html.should have_comp_tag("tbody", :count => 1)
|
229
|
+
html.should have_comp_tag("tr", :count => (@users.size + 1))
|
230
|
+
html.should have_comp_tag("td", :count => (@users.size + 1))
|
231
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(1)", :text => @users.first.username)
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
it "should have symbol and value" do
|
236
|
+
helper.sorting_table_for(@users) do |table|
|
237
|
+
html = table.columns do |value|
|
238
|
+
table.column :price
|
239
|
+
table.column (value.price - 1)
|
240
|
+
end
|
241
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(1)", :text => number_to_currency(@users.first.price))
|
242
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(2)", :text => (@users.first.price - 1).to_s)
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
end
|
247
|
+
|
248
|
+
describe " #table sub column" do
|
249
|
+
|
250
|
+
it "should not change symbol" do
|
251
|
+
helper.sorting_table_for(@users) do |table|
|
252
|
+
html = table.columns do
|
253
|
+
table.column do
|
254
|
+
:username
|
255
|
+
end
|
256
|
+
table.column do
|
257
|
+
'hello'
|
258
|
+
end
|
259
|
+
end
|
260
|
+
if SortingTableFor::Tools::rails3?
|
261
|
+
html.should have_comp_tag('tr:nth-child(2) td:nth-child(1)', :text => '')
|
262
|
+
else
|
263
|
+
html.should have_comp_tag('tr:nth-child(2) td:nth-child(1)', :text => 'username')
|
264
|
+
end
|
265
|
+
html.should have_comp_tag('tr:nth-child(2) td:nth-child(2)', :text => 'hello')
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
it "should works with value" do
|
270
|
+
helper.sorting_table_for(@users) do |table|
|
271
|
+
html = table.columns do |value|
|
272
|
+
table.column do
|
273
|
+
value.username
|
274
|
+
end
|
275
|
+
end
|
276
|
+
html.should have_comp_tag('tr:nth-child(2) td:nth-child(1)', :text => @users.first.username)
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
it "should customize html" do
|
281
|
+
table_html = helper.sorting_table_for(@users, :html => { :class => 'table_class', :id => 'table_id' }) do |table|
|
282
|
+
html = table.columns :html => { :class => 'hello_class', :id => 'hello_id', :title => 'hello_title' } do
|
283
|
+
table.column :html => { :class => 'hi_class', :id => 'hi_id', :title => 'hi_title' } do
|
284
|
+
'hello'
|
285
|
+
end
|
286
|
+
end
|
287
|
+
html.should have_comp_tag("tr:nth-child(2)[class='hello_class odd'][id=hello_id][title=hello_title]")
|
288
|
+
html.should have_comp_tag("tr:nth-child(2) td:nth-child(1)[class='hi_class'][id=hi_id][title=hi_title]")
|
289
|
+
end
|
290
|
+
helper.output_buffer.concat(table_html)
|
291
|
+
helper.output_buffer.should have_comp_tag("table[class='table_class sorting_table_for'][id=table_id]")
|
292
|
+
end
|
293
|
+
|
294
|
+
end
|
295
|
+
|
296
|
+
describe ' #With editable options' do
|
297
|
+
|
298
|
+
before :each do
|
299
|
+
## Restaure default values
|
300
|
+
SortingTableFor::TableBuilder.show_total_entries = true
|
301
|
+
SortingTableFor::TableBuilder.reserved_columns = [:id, :password, :salt]
|
302
|
+
SortingTableFor::TableBuilder.default_actions = [:edit, :delete]
|
303
|
+
end
|
304
|
+
|
305
|
+
it "should not add total entries" do
|
306
|
+
SortingTableFor::TableBuilder.show_total_entries = false
|
307
|
+
helper.sorting_table_for(@users) do |table|
|
308
|
+
html = table.columns
|
309
|
+
html.should_not match("Total Entries " + @users.size.to_s)
|
310
|
+
html.should_not match("<td colspan=\"" + (User.content_columns.size + 2).to_s + "\">")
|
311
|
+
end
|
312
|
+
end
|
313
|
+
|
314
|
+
it "should add total entries" do
|
315
|
+
SortingTableFor::TableBuilder.show_total_entries = true
|
316
|
+
helper.sorting_table_for(@users) do |table|
|
317
|
+
html = table.columns
|
318
|
+
html.should match("Total Entries " + @users.size.to_s)
|
319
|
+
html.should match("<td colspan=\"" + (User.content_columns.size + 2).to_s + "\">")
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
it "should change reserved columns" do
|
324
|
+
SortingTableFor::TableBuilder.reserved_columns = [:id, :firstname, :lastname, :position, :salary, :price, :active, :created_at, :updated_at]
|
325
|
+
helper.sorting_table_for(@users) do |table|
|
326
|
+
html = table.columns
|
327
|
+
html.should have_comp_tag("tr", :count => (@users.size + 1))
|
328
|
+
html.should have_comp_tag("td", :count => ((@users.size * 3) + 1))
|
329
|
+
end
|
330
|
+
end
|
331
|
+
|
332
|
+
it "Reserved columns should not impact on column" do
|
333
|
+
SortingTableFor::TableBuilder.reserved_columns = [:id, :firstname, :lastname, :position, :salary, :price, :active, :created_at, :updated_at]
|
334
|
+
helper.sorting_table_for(@users) do |table|
|
335
|
+
html = table.columns do
|
336
|
+
table.column :username
|
337
|
+
table.column :price
|
338
|
+
end
|
339
|
+
html.should have_comp_tag("td", :count => ((@users.size * 2) + 1))
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
it "should edit default actions" do
|
344
|
+
SortingTableFor::TableBuilder.default_actions = [:show, :edit_password, :edit, :delete]
|
345
|
+
helper.sorting_table_for(@users) do |table|
|
346
|
+
table.columns.should have_comp_tag("td", :count => (@users.size * (User.content_columns.size + 4)) + 1)
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
it "should edit default actions" do
|
351
|
+
pending('should check if the links are valids')
|
352
|
+
SortingTableFor::TableBuilder.default_actions = [:show, :edit_password, :edit, :delete]
|
353
|
+
helper.sorting_table_for(@users) do |table|
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
end
|
358
|
+
## Add spec for links
|
359
|
+
end
|