magic_grid 0.11.1 → 0.12.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/README.md +1 -0
- data/Rakefile +2 -2
- data/lib/assets/javascripts/magic_grid.js +7 -6
- data/lib/magic_grid/collection.rb +115 -67
- data/lib/magic_grid/column.rb +93 -0
- data/lib/magic_grid/definition.rb +67 -120
- data/lib/magic_grid/helpers.rb +5 -249
- data/lib/magic_grid/html_grid.rb +275 -0
- data/lib/magic_grid/version.rb +1 -1
- data/spec/collection_spec.rb +139 -8
- data/spec/column_spec.rb +72 -0
- data/spec/definition_spec.rb +63 -35
- data/spec/helpers_spec.rb +71 -93
- data/spec/html_grid_spec.rb +117 -0
- data/spec/spec_helper.rb +36 -4
- data/test/dummy/app/controllers/users_controller.rb +4 -0
- metadata +121 -115
data/lib/magic_grid/version.rb
CHANGED
data/spec/collection_spec.rb
CHANGED
@@ -1,21 +1,20 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'magic_grid/collection'
|
3
|
+
require 'magic_grid/column'
|
3
4
|
|
4
5
|
describe MagicGrid::Collection do
|
5
6
|
|
6
7
|
context "via [] class method" do
|
7
8
|
context "when given a MagicGrid::Collection" do
|
8
9
|
let(:actual_collection) { [1,2,3,4] }
|
9
|
-
let(:magic_collection) { MagicGrid::Collection.new(actual_collection
|
10
|
-
subject { MagicGrid::Collection
|
10
|
+
let(:magic_collection) { MagicGrid::Collection.new(actual_collection) }
|
11
|
+
subject { MagicGrid::Collection.create_or_reuse(magic_collection) }
|
11
12
|
its(:collection) { should eq(actual_collection) }
|
12
|
-
its(:grid) { should eq(:new_grid) }
|
13
13
|
end
|
14
14
|
context "when given a basic collection" do
|
15
15
|
let(:actual_collection) { [1,2,3,4] }
|
16
|
-
subject { MagicGrid::Collection
|
16
|
+
subject { MagicGrid::Collection.create_or_reuse(actual_collection) }
|
17
17
|
its(:collection) { should eq(actual_collection) }
|
18
|
-
its(:grid) { should eq(:original_grid) }
|
19
18
|
end
|
20
19
|
end
|
21
20
|
|
@@ -23,7 +22,6 @@ describe MagicGrid::Collection do
|
|
23
22
|
let(:collection) { [1,2,3,4] }
|
24
23
|
subject { MagicGrid::Collection.new(collection, nil) }
|
25
24
|
its(:collection) { should eq(collection) }
|
26
|
-
its(:grid) { should be_nil }
|
27
25
|
end
|
28
26
|
|
29
27
|
context "when based on something sortable" do
|
@@ -37,8 +35,141 @@ describe MagicGrid::Collection do
|
|
37
35
|
ordered = [1,2,3,4,5]
|
38
36
|
collection = MagicGrid::Collection.new(sortable_collection, nil)
|
39
37
|
sortable_collection.should_receive(:order) { ordered }
|
40
|
-
|
38
|
+
column = MagicGrid::FilterOnlyColumn.new("col")
|
39
|
+
collection.apply_sort(column, "order")
|
41
40
|
collection.collection.should == ordered
|
42
41
|
end
|
43
42
|
end
|
44
|
-
|
43
|
+
|
44
|
+
context "when post filter callback is given" do
|
45
|
+
it "should call the given callback when the collection is reduced" do
|
46
|
+
array = [1]
|
47
|
+
callback = double.tap do |cb|
|
48
|
+
cb.should_receive(:call).with(array) { [2] }
|
49
|
+
end
|
50
|
+
collection = MagicGrid::Collection.new(array, nil)
|
51
|
+
collection.add_post_filter_callback(callback)
|
52
|
+
collection.collection.should == [2]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#perform_search" do
|
57
|
+
context "when the collection is searchable" do
|
58
|
+
let(:collection) {
|
59
|
+
fake_active_record_collection('some_table', [:name, :description])
|
60
|
+
}
|
61
|
+
subject {
|
62
|
+
MagicGrid::Collection.new(collection).tap do |c|
|
63
|
+
c.searchable_columns = [MagicGrid::FilterOnlyColumn.new(:name, c)]
|
64
|
+
end
|
65
|
+
}
|
66
|
+
it { subject.should be_filterable }
|
67
|
+
it { subject.should be_searchable }
|
68
|
+
it "should apply a search query when given one" do
|
69
|
+
subject.apply_search("foobar")
|
70
|
+
subject.searches.should_not be_empty
|
71
|
+
end
|
72
|
+
it "should not barf when given a blank search query" do
|
73
|
+
subject.apply_search(nil)
|
74
|
+
subject.apply_search("")
|
75
|
+
subject.searches.should be_empty
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context "when the collection is not searchable" do
|
80
|
+
let(:collection) {
|
81
|
+
fake_active_record_collection('some_table', [:name, :description])
|
82
|
+
}
|
83
|
+
subject { MagicGrid::Collection.new(collection) }
|
84
|
+
it { subject.should be_filterable }
|
85
|
+
it { subject.should_not be_searchable }
|
86
|
+
it "should not apply a search query when given one" do
|
87
|
+
subject.apply_search("foobar")
|
88
|
+
subject.searches.should be_empty
|
89
|
+
end
|
90
|
+
it "should not barf when given a blank search query" do
|
91
|
+
subject.apply_search(nil)
|
92
|
+
subject.apply_search("")
|
93
|
+
subject.searches.should be_empty
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
#TODO these tests will only really work properly once we have the ability
|
99
|
+
# to undefine constants, which looks like it should be coming in a future
|
100
|
+
# version of RSpec (as in post 2.11.1)
|
101
|
+
|
102
|
+
describe "#perform_pagination" do
|
103
|
+
|
104
|
+
context "when #paginate (aka WillPaginate) is available" do
|
105
|
+
it "should call paginate helper when it is detected" do
|
106
|
+
array = [1].tap do |a|
|
107
|
+
a.should_receive(:paginate).with(page: 1, per_page: 1) { a }
|
108
|
+
end
|
109
|
+
collection = MagicGrid::Collection.new(array, nil)
|
110
|
+
collection.per_page = 1
|
111
|
+
collection.apply_pagination 1
|
112
|
+
collection.perform_pagination(array).should == array
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
unless Module.const_defined? :WillPaginate
|
117
|
+
context "when #page (possibly from Kaminari) is available" do
|
118
|
+
it "should call paginate helper when it is detected" do
|
119
|
+
array = [1].tap do |a|
|
120
|
+
a.should_receive(:per).with(1) { array }
|
121
|
+
a.should_receive(:page).with(1) { array }
|
122
|
+
end
|
123
|
+
collection = MagicGrid::Collection.new(array, nil)
|
124
|
+
collection.per_page = 1
|
125
|
+
collection.apply_pagination 1
|
126
|
+
collection.perform_pagination(array).should == array
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
context "when given an Array and Kaminari is available" do
|
131
|
+
it "should call paginate helper when it is detected" do
|
132
|
+
array = Array.new(10) { 1 }
|
133
|
+
kaminaried_array = [1,1].tap do |ka|
|
134
|
+
ka.should_receive(:per).with(1) { ka }
|
135
|
+
ka.should_receive(:page).with(1) { ka }
|
136
|
+
end
|
137
|
+
kaminari = double.tap do |k|
|
138
|
+
k.should_receive(:paginate_array).with(array) { kaminaried_array }
|
139
|
+
end
|
140
|
+
stub_const('Kaminari', kaminari)
|
141
|
+
collection = MagicGrid::Collection.new(array, nil)
|
142
|
+
collection.per_page = 1
|
143
|
+
collection.apply_pagination 1
|
144
|
+
collection.perform_pagination(array).should == kaminaried_array
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context "when no pagination is provided" do
|
150
|
+
# TODO replace this when rspec-mocks add 'hide_const'
|
151
|
+
# before :each do
|
152
|
+
# if Module.const_defined?(:Kaminari)
|
153
|
+
# @kaminari = Module.const_get(:Kaminari)
|
154
|
+
# Object.send(:remove_const, :Kaminari)
|
155
|
+
# end
|
156
|
+
# end
|
157
|
+
# after :each do
|
158
|
+
# Module.const_set(:Kaminari, @kaminari) if @kaminari
|
159
|
+
# end
|
160
|
+
|
161
|
+
#
|
162
|
+
# For now, Travis-CI takes care of this, since we have gem profiles defined
|
163
|
+
# to test all 3 supported pagination scenarios
|
164
|
+
#
|
165
|
+
|
166
|
+
it "should attempt to use Enumerable methods to perform pagination" do
|
167
|
+
array = Array.new(100) { 1 }
|
168
|
+
collection = MagicGrid::Collection.new(array, nil)
|
169
|
+
collection.per_page = 1
|
170
|
+
collection.apply_pagination 1
|
171
|
+
collection.perform_pagination(array).should == [1]
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
data/spec/column_spec.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'magic_grid/column'
|
3
|
+
require 'magic_grid/collection'
|
4
|
+
|
5
|
+
describe MagicGrid::Column do
|
6
|
+
describe "Column.columns_for_collection" do
|
7
|
+
context "for setting searchable columns on column filtering collection" do
|
8
|
+
let(:collection_backing) { fake_active_record_collection }
|
9
|
+
let(:collection) { MagicGrid::Collection.new(collection_backing) }
|
10
|
+
it "enables searching on non-displayed columns" do
|
11
|
+
display = [:name]
|
12
|
+
searchable = [:secret]
|
13
|
+
columns = MagicGrid::Column.columns_for_collection(collection,
|
14
|
+
display,
|
15
|
+
searchable)
|
16
|
+
collection.should be_searchable
|
17
|
+
end
|
18
|
+
it "enables searching on displayed columns" do
|
19
|
+
display = [:name]
|
20
|
+
searchable = [:name]
|
21
|
+
columns = MagicGrid::Column.columns_for_collection(collection,
|
22
|
+
display,
|
23
|
+
searchable)
|
24
|
+
collection.should be_searchable
|
25
|
+
end
|
26
|
+
it "accepts false as a method of disabling search" do
|
27
|
+
display = [:name]
|
28
|
+
searchable = false
|
29
|
+
columns = MagicGrid::Column.columns_for_collection(collection,
|
30
|
+
display,
|
31
|
+
searchable)
|
32
|
+
collection.should_not be_searchable
|
33
|
+
end
|
34
|
+
it "doesn't enable searching when not given a column" do
|
35
|
+
display = [:name]
|
36
|
+
searchable = true
|
37
|
+
columns = MagicGrid::Column.columns_for_collection(collection,
|
38
|
+
display,
|
39
|
+
searchable)
|
40
|
+
collection.should_not be_searchable
|
41
|
+
end
|
42
|
+
end
|
43
|
+
context "for enabling/disabling search on #search() style collection" do
|
44
|
+
let(:collection) {
|
45
|
+
data = [1,2,3].tap do |c|
|
46
|
+
c.stub(:search) { c }
|
47
|
+
end
|
48
|
+
MagicGrid::Collection.new(data, search_method: :search)
|
49
|
+
}
|
50
|
+
# I actually consider this a bug, since it is totally inconsistent, but
|
51
|
+
# it's how things are currently implemented.
|
52
|
+
it "doesn't disable searching, even when told so" do
|
53
|
+
display = [:name]
|
54
|
+
searchable = false
|
55
|
+
collection.should be_searchable
|
56
|
+
columns = MagicGrid::Column.columns_for_collection(collection,
|
57
|
+
display,
|
58
|
+
searchable)
|
59
|
+
collection.should be_searchable
|
60
|
+
end
|
61
|
+
it "enables searching when told so" do
|
62
|
+
collection.should be_searchable
|
63
|
+
display = [:name]
|
64
|
+
searchable = true
|
65
|
+
columns = MagicGrid::Column.columns_for_collection(collection,
|
66
|
+
display,
|
67
|
+
searchable)
|
68
|
+
collection.should be_searchable
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/spec/definition_spec.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'magic_grid/definition'
|
3
|
-
require 'active_support/core_ext/hash/indifferent_access'
|
4
3
|
|
5
4
|
describe MagicGrid::Definition do
|
6
5
|
pending "embarasses me with how tightly it is coupled with.. EVERYTHING"
|
@@ -45,12 +44,37 @@ describe MagicGrid::Definition do
|
|
45
44
|
expect { MagicGrid::Definition.new() }.to raise_error
|
46
45
|
end
|
47
46
|
|
47
|
+
context "options that can't be used" do
|
48
|
+
let(:controller) {
|
49
|
+
double.tap do |c|
|
50
|
+
c.stub(:params) { {} }
|
51
|
+
end
|
52
|
+
}
|
53
|
+
it "doesn't barf when listeners are given for a dumb collection" do
|
54
|
+
expect {
|
55
|
+
MagicGrid::Definition.new([:a], [1], controller, listeners: {a: :a})
|
56
|
+
}.not_to raise_error
|
57
|
+
end
|
58
|
+
|
59
|
+
it "doesn't barf when search columns are given for a dumb collection" do
|
60
|
+
expect {
|
61
|
+
MagicGrid::Definition.new([:a], [1], controller, searchable: [:a])
|
62
|
+
}.not_to raise_error
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
48
66
|
context "when given an empty collection" do
|
49
67
|
subject { MagicGrid::Definition.new(column_list, empty_collection) }
|
50
68
|
its(:base_params) { should include(:magic_grid_id) }
|
51
69
|
its(:collection) { should == empty_collection }
|
52
|
-
its(
|
70
|
+
its('columns.length') { should == column_list.length }
|
53
71
|
it_behaves_like "a basic grid"
|
72
|
+
|
73
|
+
context "when pagination is disabled" do
|
74
|
+
subject { MagicGrid::Definition.new(column_list, empty_collection, nil, per_page: false) }
|
75
|
+
its(:current_page) { should == 1 }
|
76
|
+
its(:collection) { should be_empty }
|
77
|
+
end
|
54
78
|
end
|
55
79
|
|
56
80
|
context "when given a large collection and default options" do
|
@@ -64,7 +88,7 @@ describe MagicGrid::Definition do
|
|
64
88
|
its(:collection) { should_not == empty_collection }
|
65
89
|
|
66
90
|
its(:collection) { should have(MagicGrid::Definition.runtime_defaults[:per_page]).items }
|
67
|
-
its(
|
91
|
+
its('columns.length') { should == column_list.length }
|
68
92
|
end
|
69
93
|
|
70
94
|
context "when given a MagicGrid::Collection" do
|
@@ -83,10 +107,10 @@ describe MagicGrid::Definition do
|
|
83
107
|
subject { MagicGrid::Definition.new(column_list, large_collection, controller, id: :grid, per_page: 17) }
|
84
108
|
its(:collection) { should_not == empty_collection }
|
85
109
|
it "should give a collection with a page worth of items" do
|
86
|
-
subject.per_page.should < large_collection.count
|
87
|
-
subject.collection.should have(subject.per_page).items
|
110
|
+
subject.magic_collection.per_page.should < large_collection.count
|
111
|
+
subject.collection.should have(subject.magic_collection.per_page).items
|
88
112
|
end
|
89
|
-
its(
|
113
|
+
its('columns.length') { should == column_list.length }
|
90
114
|
its(:current_page) { should == 2 }
|
91
115
|
|
92
116
|
it "should know how to extract its params" do
|
@@ -106,7 +130,7 @@ describe MagicGrid::Definition do
|
|
106
130
|
let(:collection) { data }
|
107
131
|
it "should sort collection using #order" do
|
108
132
|
collection.should_receive(:order).with("foo DESC") { data.sort.reverse }
|
109
|
-
grid = MagicGrid::Definition.new([{:
|
133
|
+
grid = MagicGrid::Definition.new([{sql: "foo"}], collection, controller, id: :grid)
|
110
134
|
|
111
135
|
grid.collection.should == data.sort.reverse
|
112
136
|
end
|
@@ -114,24 +138,17 @@ describe MagicGrid::Definition do
|
|
114
138
|
end
|
115
139
|
|
116
140
|
context "filtering with #where" do
|
117
|
-
|
118
|
-
|
119
|
-
controller = double
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
end
|
129
|
-
}
|
130
|
-
subject { MagicGrid::Definition.new([{:sql => "foo"}],
|
131
|
-
collection,
|
132
|
-
controller,
|
133
|
-
id: :grid, listeners: {f1: :f1}) }
|
134
|
-
its(:collection) { should == [1, 7, 1] }
|
141
|
+
it "should use listeners with #where when asked to" do
|
142
|
+
filter_param = HashWithIndifferentAccess.new({f1: 1})
|
143
|
+
controller = double(params: filter_param)
|
144
|
+
collection = [1,56,7,21,1]
|
145
|
+
collection.should_receive(:where).with(filter_param).and_return([1, 7, 1])
|
146
|
+
grid = MagicGrid::Definition.new([{sql: "foo"}],
|
147
|
+
collection,
|
148
|
+
controller,
|
149
|
+
id: :grid, listeners: {f1: :f1})
|
150
|
+
grid.collection.should == [1, 7, 1]
|
151
|
+
end
|
135
152
|
end
|
136
153
|
|
137
154
|
context "filtering with a callback" do
|
@@ -147,7 +164,7 @@ describe MagicGrid::Definition do
|
|
147
164
|
let(:collection) {
|
148
165
|
data
|
149
166
|
}
|
150
|
-
subject { MagicGrid::Definition.new([{:
|
167
|
+
subject { MagicGrid::Definition.new([{sql: "foo"}],
|
151
168
|
collection,
|
152
169
|
controller,
|
153
170
|
id: :grid, listener_handler: filter) }
|
@@ -169,7 +186,7 @@ describe MagicGrid::Definition do
|
|
169
186
|
let(:collection) {
|
170
187
|
data
|
171
188
|
}
|
172
|
-
subject { MagicGrid::Definition.new([{:
|
189
|
+
subject { MagicGrid::Definition.new([{sql: "foo"}],
|
173
190
|
collection,
|
174
191
|
controller,
|
175
192
|
id: :grid, post_filter: filter) }
|
@@ -178,9 +195,6 @@ describe MagicGrid::Definition do
|
|
178
195
|
|
179
196
|
context "post_filtering with a collection post_filter" do
|
180
197
|
data = [1,56,7,21,1]
|
181
|
-
filter = Proc.new do |c|
|
182
|
-
c.select { |i| i > 10 }
|
183
|
-
end
|
184
198
|
let(:controller) {
|
185
199
|
controller = double.tap do |c|
|
186
200
|
c.stub(:params) { HashWithIndifferentAccess.new({f1: 1}) }
|
@@ -193,11 +207,25 @@ describe MagicGrid::Definition do
|
|
193
207
|
end
|
194
208
|
end
|
195
209
|
}
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
210
|
+
it "should use the collection's post_filter method" do
|
211
|
+
grid = MagicGrid::Definition.new([{sql: "foo"}],
|
212
|
+
collection,
|
213
|
+
controller,
|
214
|
+
id: :grid, collection_post_filter: true)
|
215
|
+
|
216
|
+
data.should_receive(:post_filter).with().and_return([1,2,3,4])
|
217
|
+
grid.collection.should == [1,2,3,4]
|
218
|
+
grid.collection.should_not be_empty
|
219
|
+
end
|
220
|
+
it "can be disabled via the collection_post_filter option" do
|
221
|
+
grid = MagicGrid::Definition.new([{sql: "foo"}],
|
222
|
+
collection,
|
223
|
+
controller,
|
224
|
+
id: :grid, collection_post_filter: false)
|
225
|
+
|
226
|
+
data.should_not_receive(:post_filter)
|
227
|
+
grid.collection.should == data
|
228
|
+
end
|
201
229
|
end
|
202
230
|
|
203
231
|
end
|
data/spec/helpers_spec.rb
CHANGED
@@ -1,45 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'magic_grid/helpers'
|
3
|
-
require 'action_controller'
|
4
|
-
require "active_support/core_ext"
|
5
|
-
|
6
|
-
def make_controller
|
7
|
-
request = double.tap { |r|
|
8
|
-
r.stub(:fullpath, "/foo?page=bar")
|
9
|
-
}
|
10
|
-
double.tap { |v|
|
11
|
-
v.stub(:render)
|
12
|
-
v.stub(:params) { {} }
|
13
|
-
v.stub(:request) { request }
|
14
|
-
}
|
15
|
-
end
|
16
|
-
|
17
|
-
def fake_connection
|
18
|
-
double(:connection).tap do |c|
|
19
|
-
c.stub(:quote_column_name) { |col| col.to_s }
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def fake_active_record_collection(table_name = 'some_table',
|
24
|
-
columns = [:name, :description])
|
25
|
-
|
26
|
-
columns = columns.map { |c|
|
27
|
-
double.tap do |col|
|
28
|
-
col.stub(:name) { c }
|
29
|
-
end
|
30
|
-
}
|
31
|
-
(1..1000).to_a.tap do |c|
|
32
|
-
c.stub(:connection => fake_connection)
|
33
|
-
c.stub(:quoted_table_name => table_name)
|
34
|
-
c.stub(:table_name => table_name)
|
35
|
-
c.stub(:where) { c }
|
36
|
-
c.stub(:table) {
|
37
|
-
double.tap do |t|
|
38
|
-
t.stub(:columns) { columns }
|
39
|
-
end
|
40
|
-
}
|
41
|
-
end
|
42
|
-
end
|
43
3
|
|
44
4
|
describe MagicGrid::Helpers do
|
45
5
|
|
@@ -47,31 +7,13 @@ describe MagicGrid::Helpers do
|
|
47
7
|
include MagicGrid::Helpers
|
48
8
|
|
49
9
|
let(:empty_collection) { [] }
|
50
|
-
|
51
10
|
let(:column_list) { [:name, :description] }
|
52
|
-
|
53
11
|
let(:controller) { make_controller }
|
54
12
|
|
55
13
|
# Kaminari uses view_renderer instead of controller
|
56
14
|
let(:view_renderer) { controller }
|
57
15
|
|
58
|
-
describe "#normalize_magic" do
|
59
|
-
|
60
|
-
it "should turn an array into a MagicGrid::Definition" do
|
61
|
-
expect(normalize_magic([])).to be_a(MagicGrid::Definition)
|
62
|
-
end
|
63
|
-
|
64
|
-
it "should give back the MagicGrid::Definition given, if given as any argument" do
|
65
|
-
definition = normalize_magic([])
|
66
|
-
expect(normalize_magic( definition )).to be(definition)
|
67
|
-
expect(normalize_magic( nil, definition )).to be(definition)
|
68
|
-
expect(normalize_magic( nil, nil, definition )).to be(definition)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
16
|
describe "#magic_grid" do
|
73
|
-
pending "DOES WAY TOO MUCH!!"
|
74
|
-
|
75
17
|
let(:emtpy_grid) { magic_grid empty_collection, column_list }
|
76
18
|
|
77
19
|
it "should barf without any arguments" do
|
@@ -90,13 +32,26 @@ describe MagicGrid::Helpers do
|
|
90
32
|
end
|
91
33
|
end
|
92
34
|
|
35
|
+
context "when given an empty collection and an if_empty callback" do
|
36
|
+
it "calls the callback" do
|
37
|
+
if_emtpy_string = "ZOMG! NO ROWS!"
|
38
|
+
callback = double.tap do |cb|
|
39
|
+
cb.should_receive(:call).with(instance_of MagicGrid::Definition) {
|
40
|
+
if_emtpy_string
|
41
|
+
}
|
42
|
+
end
|
43
|
+
grid = magic_grid empty_collection, column_list, if_empty: callback
|
44
|
+
grid.should include(if_emtpy_string)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
93
48
|
context "when given a non-empty collection" do
|
94
49
|
subject { magic_grid( [1, 2], [:to_s] ) }
|
95
50
|
it "should not indicate there is no data" do
|
96
51
|
should_not match(/if-empty/)
|
97
52
|
end
|
98
|
-
it { should
|
99
|
-
it { should
|
53
|
+
it { should match_select("td", text: "1") }
|
54
|
+
it { should match_select("td", text: "2") }
|
100
55
|
end
|
101
56
|
|
102
57
|
context "when given a block" do
|
@@ -108,6 +63,28 @@ describe MagicGrid::Helpers do
|
|
108
63
|
it { should =~ /HOKY_POKY_ALAMO: 1/ }
|
109
64
|
end
|
110
65
|
|
66
|
+
context "when given an array of Symbols as column list" do
|
67
|
+
subject {
|
68
|
+
magic_grid( [1,2], [:col1, :col2]) do |row|
|
69
|
+
"draw a row"
|
70
|
+
end
|
71
|
+
}
|
72
|
+
it { should match(/draw a row/) }
|
73
|
+
it("capitalizes the column name") { should match(/Col1/) }
|
74
|
+
it { should match(/Col2/) }
|
75
|
+
end
|
76
|
+
|
77
|
+
context "when given an array of Strings as column list" do
|
78
|
+
subject {
|
79
|
+
magic_grid( [1,2], ["CamelCase", "lower_case"]) do |row|
|
80
|
+
"draw a row"
|
81
|
+
end
|
82
|
+
}
|
83
|
+
it { should match(/draw a row/) }
|
84
|
+
it("leaves case alone") { should match(/CamelCase/) }
|
85
|
+
it("leaves underscores alone") { should match(/lower_case/) }
|
86
|
+
end
|
87
|
+
|
111
88
|
context "renders top and bottom pagers as told" do
|
112
89
|
large_collection = (1..1000).to_a
|
113
90
|
|
@@ -144,6 +121,20 @@ describe MagicGrid::Helpers do
|
|
144
121
|
end
|
145
122
|
end
|
146
123
|
|
124
|
+
context "searching and paging" do
|
125
|
+
let(:searchabe_collection) {
|
126
|
+
collection = [].tap do |c|
|
127
|
+
c.stub(:search) { collection }
|
128
|
+
end
|
129
|
+
}
|
130
|
+
it "should only render one spinner" do
|
131
|
+
grid = magic_grid(searchabe_collection, column_list, searchable: [:some_col])
|
132
|
+
grid.should match_select("td.searcher", 1)
|
133
|
+
grid.should match_select("td.magic-pager", 1)
|
134
|
+
grid.should match_select(".magic_grid_spinner", 1)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
147
138
|
context "searching" do
|
148
139
|
let(:searchabe_collection) {
|
149
140
|
collection = [].tap do |c|
|
@@ -151,7 +142,7 @@ describe MagicGrid::Helpers do
|
|
151
142
|
end
|
152
143
|
}
|
153
144
|
it "should render a search bar when asked" do
|
154
|
-
grid = magic_grid(searchabe_collection, column_list, :
|
145
|
+
grid = magic_grid(searchabe_collection, column_list, searchable: [:some_col])
|
155
146
|
grid.should match_select('input[type=search]')
|
156
147
|
end
|
157
148
|
|
@@ -159,13 +150,13 @@ describe MagicGrid::Helpers do
|
|
159
150
|
let(:search_param) { 'foobar' }
|
160
151
|
let(:controller) {
|
161
152
|
make_controller.tap { |c|
|
162
|
-
c.stub(:params) { {:
|
153
|
+
c.stub(:params) { {grid_id_q: search_param} }
|
163
154
|
}
|
164
155
|
}
|
165
156
|
it "should search a searchable collection when there are search params" do
|
166
157
|
collection = (1..1000).to_a
|
167
158
|
collection.should_receive(:search).with(search_param) { collection }
|
168
|
-
grid = magic_grid(collection, column_list, :
|
159
|
+
grid = magic_grid(collection, column_list, id: "grid_id", searchable: [:some_col])
|
169
160
|
grid.should match_select('input[type=search]')
|
170
161
|
end
|
171
162
|
|
@@ -177,9 +168,9 @@ describe MagicGrid::Helpers do
|
|
177
168
|
|
178
169
|
collection = fake_active_record_collection(table_name)
|
179
170
|
collection.should_receive(:where).
|
180
|
-
with("#{search_sql} LIKE :search", {:
|
171
|
+
with("#{search_sql} LIKE :search", {search: "%#{search_param}%"})
|
181
172
|
|
182
|
-
grid = magic_grid(collection, column_list, :
|
173
|
+
grid = magic_grid(collection, column_list, id: "grid_id", searchable: [search_col])
|
183
174
|
end
|
184
175
|
|
185
176
|
it "should use custom sql from column for call to where when given" do
|
@@ -189,14 +180,14 @@ describe MagicGrid::Helpers do
|
|
189
180
|
|
190
181
|
collection = fake_active_record_collection(table_name)
|
191
182
|
collection.should_receive(:where).
|
192
|
-
with("#{search_sql} LIKE :search", {:
|
183
|
+
with("#{search_sql} LIKE :search", {search: "%#{search_param}%"})
|
193
184
|
|
194
185
|
magic_grid(collection,
|
195
186
|
[ :name,
|
196
187
|
:description,
|
197
|
-
{:
|
188
|
+
{col: search_col, sql: search_sql}
|
198
189
|
],
|
199
|
-
:
|
190
|
+
id: "grid_id", searchable: [search_col])
|
200
191
|
end
|
201
192
|
|
202
193
|
it "should use column number to look up search column" do
|
@@ -206,14 +197,14 @@ describe MagicGrid::Helpers do
|
|
206
197
|
|
207
198
|
collection = fake_active_record_collection(table_name)
|
208
199
|
collection.should_receive(:where).
|
209
|
-
with("#{search_sql} LIKE :search", {:
|
200
|
+
with("#{search_sql} LIKE :search", {search: "%#{search_param}%"})
|
210
201
|
|
211
202
|
magic_grid(collection,
|
212
203
|
[ :name,
|
213
204
|
:description,
|
214
|
-
{:
|
205
|
+
{col: search_col, sql: search_sql}
|
215
206
|
],
|
216
|
-
:
|
207
|
+
id: "grid_id", searchable: [2])
|
217
208
|
end
|
218
209
|
|
219
210
|
it "should use custom sql for call to where when given" do
|
@@ -224,14 +215,14 @@ describe MagicGrid::Helpers do
|
|
224
215
|
|
225
216
|
collection = fake_active_record_collection(table_name)
|
226
217
|
collection.should_receive(:where).
|
227
|
-
with("#{search_sql} LIKE :search", {:
|
218
|
+
with("#{search_sql} LIKE :search", {search: "%#{search_param}%"})
|
228
219
|
|
229
220
|
magic_grid(collection,
|
230
221
|
[ :name,
|
231
222
|
:description,
|
232
|
-
{:
|
223
|
+
{col: search_col, sql: search_sql}
|
233
224
|
],
|
234
|
-
:
|
225
|
+
id: "grid_id", searchable: [custom_search_col])
|
235
226
|
end
|
236
227
|
|
237
228
|
it "should fail when given bad searchable columns" do
|
@@ -241,7 +232,7 @@ describe MagicGrid::Helpers do
|
|
241
232
|
expect {
|
242
233
|
magic_grid(collection,
|
243
234
|
[ :name, :description],
|
244
|
-
:
|
235
|
+
id: "grid_id", searchable: [nil])
|
245
236
|
}.to raise_error
|
246
237
|
end
|
247
238
|
|
@@ -258,7 +249,7 @@ describe MagicGrid::Helpers do
|
|
258
249
|
end
|
259
250
|
|
260
251
|
expect {
|
261
|
-
magic_grid(collection, column_list, :
|
252
|
+
magic_grid(collection, column_list, id: "grid_id", searchable: [search_col])
|
262
253
|
}.to_not raise_error
|
263
254
|
end
|
264
255
|
end
|
@@ -266,20 +257,7 @@ describe MagicGrid::Helpers do
|
|
266
257
|
end
|
267
258
|
|
268
259
|
context "sorting" do
|
269
|
-
let(:sortable_collection) {
|
270
|
-
columns = column_list.map { |c|
|
271
|
-
double.tap do |col|
|
272
|
-
col.stub(:name) { c }
|
273
|
-
end
|
274
|
-
}
|
275
|
-
collection = fake_active_record_collection.tap do |c|
|
276
|
-
c.stub(:table) {
|
277
|
-
double.tap do |t|
|
278
|
-
t.stub(:columns) { columns }
|
279
|
-
end
|
280
|
-
}
|
281
|
-
end
|
282
|
-
}
|
260
|
+
let(:sortable_collection) { fake_active_record_collection }
|
283
261
|
it "should render sortable column headers when a collection is sortable" do
|
284
262
|
grid = magic_grid(sortable_collection, column_list)
|
285
263
|
grid.should match_select("thead>tr>th.sorter>a>span.ui-icon", column_list.count)
|
@@ -289,14 +267,14 @@ describe MagicGrid::Helpers do
|
|
289
267
|
# let(:search_param) { 'foobar' }
|
290
268
|
# let(:controller) {
|
291
269
|
# make_controller.tap { |c|
|
292
|
-
# c.stub(:params) { {:
|
270
|
+
# c.stub(:params) { {grid_id_col: 1} }
|
293
271
|
# }
|
294
272
|
# }
|
295
273
|
# end
|
296
274
|
# context "when a sort order is given" do
|
297
275
|
# let(:controller) {
|
298
276
|
# make_controller.tap { |c|
|
299
|
-
# c.stub(:params) { {:
|
277
|
+
# c.stub(:params) { {grid_id_order: 1} }
|
300
278
|
# }
|
301
279
|
# }
|
302
280
|
# end
|
@@ -304,7 +282,7 @@ describe MagicGrid::Helpers do
|
|
304
282
|
# let(:search_param) { 'foobar' }
|
305
283
|
# let(:controller) {
|
306
284
|
# make_controller.tap { |c|
|
307
|
-
# c.stub(:params) { {:
|
285
|
+
# c.stub(:params) { {grid_id_q: 1} }
|
308
286
|
# }
|
309
287
|
# }
|
310
288
|
# end
|