magic_grid 0.11.0 → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,7 +17,7 @@ module MagicGrid
17
17
  @paginations = []
18
18
  end
19
19
 
20
- delegate :map, :count, :to => :collection
20
+ delegate :quoted_table_name, :map, :count, :to => :collection
21
21
 
22
22
  attr_accessor :grid
23
23
  attr_reader :current_page, :original_count, :total_pages
@@ -31,6 +31,10 @@ module MagicGrid
31
31
  end
32
32
  end
33
33
 
34
+ def column_names
35
+ @collection.table.columns.map {|c| c.name}
36
+ end
37
+
34
38
  def quote_column_name(col)
35
39
  @collection.connection.quote_column_name(col.to_s)
36
40
  end
@@ -75,6 +79,7 @@ module MagicGrid
75
79
  end
76
80
 
77
81
  def apply_sort(col, dir)
82
+ @reduced_collection = nil
78
83
  @sorts << "#{col} #{dir}"
79
84
  self
80
85
  end
@@ -84,6 +89,7 @@ module MagicGrid
84
89
  end
85
90
 
86
91
  def apply_search(q)
92
+ @reduced_collection = nil
87
93
  @searches << q
88
94
  self
89
95
  end
@@ -101,6 +107,7 @@ module MagicGrid
101
107
 
102
108
  def apply_filter(filters = {})
103
109
  if @collection.respond_to? :where
110
+ @reduced_collection = nil
104
111
  @filters << filters
105
112
  end
106
113
  self
@@ -108,6 +115,7 @@ module MagicGrid
108
115
 
109
116
  def apply_filter_callback(callback)
110
117
  if callback.respond_to? :call
118
+ @reduced_collection = nil
111
119
  @filter_callbacks << callback
112
120
  end
113
121
  self
@@ -118,12 +126,14 @@ module MagicGrid
118
126
  end
119
127
 
120
128
  def apply_post_filter
129
+ @reduced_collection = nil
121
130
  @post_filters << :post_filter
122
131
  self
123
132
  end
124
133
 
125
134
  def apply_pagination(current_page, per_page)
126
135
  if per_page
136
+ @reduced_collection = nil
127
137
  @paginations << {:current_page => current_page, :per_page => per_page}
128
138
  end
129
139
  self
@@ -6,6 +6,10 @@ module MagicGrid
6
6
  attr_reader :columns, :magic_id, :options, :params,
7
7
  :current_sort_col, :current_order, :default_order, :per_page
8
8
 
9
+ def magic_collection
10
+ @collection
11
+ end
12
+
9
13
  def collection
10
14
  @collection.collection
11
15
  end
@@ -64,9 +68,10 @@ module MagicGrid
64
68
  begin
65
69
  #if @collection.respond_to? :table
66
70
  table_name = @collection.quoted_table_name
67
- table_columns = @collection.table.columns.map {|c| c.name}
71
+ table_columns = @collection.column_names
68
72
  rescue
69
- MagicGrid.logger.debug "Given collection doesn't respond to :table well"
73
+ msg = "Given collection doesn't respond to :quoted_table_name or :table well: "
74
+ MagicGrid.logger.debug("#{msg} - #{$!}")
70
75
  table_name = nil
71
76
  table_columns = @columns.each_index.to_a
72
77
  end
@@ -81,7 +86,7 @@ module MagicGrid
81
86
  c[:id] = i
82
87
  i += 1
83
88
  if c.key?(:col) and c[:col].is_a?(Symbol) and table_columns.include?(c[:col])
84
- c[:sql] = "#{table_name}.#{@collection.connection.quote_column_name(c[:col].to_s)}" unless c.key?(:sql)
89
+ c[:sql] = "#{table_name}.#{@collection.quote_column_name(c[:col].to_s)}" unless c.key?(:sql)
85
90
  end
86
91
  c[:label] = c[:col].to_s.titleize if not c.key? :label
87
92
  hash << c[:label]
@@ -227,11 +227,11 @@ module MagicGrid
227
227
  def magic_paginate(collection, opts={})
228
228
  if respond_to? :will_paginate
229
229
  # WillPaginate
230
- will_paginate collection, opts
230
+ will_paginate collection.collection, opts
231
231
  #alias_method :magic_paginate, :will_paginate
232
232
  elsif respond_to? :paginate
233
233
  #Kaminari, or something else..
234
- paginate collection, opts
234
+ paginate collection.collection, opts
235
235
  #alias_method :magic_paginate, :paginate
236
236
  else
237
237
  ("<!-- page #{collection.current_page} of #{collection.total_pages} -->" +
@@ -243,7 +243,7 @@ module MagicGrid
243
243
  content_tag('tr') do
244
244
  content_tag('td', :class => 'full-width ui-widget-header magic-pager',
245
245
  :colspan => grid.columns.count) do
246
- pager = magic_paginate(grid.collection,
246
+ pager = magic_paginate(grid.magic_collection,
247
247
  :param_name => grid.param_key(:page),
248
248
  :params => base_params
249
249
  )
@@ -1,3 +1,3 @@
1
1
  module MagicGrid
2
- VERSION = "0.11.0"
2
+ VERSION = "0.11.1"
3
3
  end
data/spec/helpers_spec.rb CHANGED
@@ -20,15 +20,27 @@ def fake_connection
20
20
  end
21
21
  end
22
22
 
23
- def fake_active_record_collection(table_name = 'some_table')
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
+ }
24
31
  (1..1000).to_a.tap do |c|
25
32
  c.stub(:connection => fake_connection)
33
+ c.stub(:quoted_table_name => table_name)
26
34
  c.stub(:table_name => table_name)
27
35
  c.stub(:where) { c }
36
+ c.stub(:table) {
37
+ double.tap do |t|
38
+ t.stub(:columns) { columns }
39
+ end
40
+ }
28
41
  end
29
42
  end
30
43
 
31
-
32
44
  describe MagicGrid::Helpers do
33
45
 
34
46
  # Let's use the helpers the way they're meant to be used!
@@ -252,6 +264,52 @@ describe MagicGrid::Helpers do
252
264
  end
253
265
  end
254
266
  end
267
+
268
+ 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
+ }
283
+ it "should render sortable column headers when a collection is sortable" do
284
+ grid = magic_grid(sortable_collection, column_list)
285
+ grid.should match_select("thead>tr>th.sorter>a>span.ui-icon", column_list.count)
286
+ end
287
+
288
+ # context "when a sort column is given" do
289
+ # let(:search_param) { 'foobar' }
290
+ # let(:controller) {
291
+ # make_controller.tap { |c|
292
+ # c.stub(:params) { {:grid_id_col => 1} }
293
+ # }
294
+ # }
295
+ # end
296
+ # context "when a sort order is given" do
297
+ # let(:controller) {
298
+ # make_controller.tap { |c|
299
+ # c.stub(:params) { {:grid_id_order => 1} }
300
+ # }
301
+ # }
302
+ # end
303
+ # context "when a sort order and column are given" do
304
+ # let(:search_param) { 'foobar' }
305
+ # let(:controller) {
306
+ # make_controller.tap { |c|
307
+ # c.stub(:params) { {:grid_id_q => 1} }
308
+ # }
309
+ # }
310
+ # end
311
+ end
312
+
255
313
  end
256
314
 
257
315
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magic_grid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.11.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -211,7 +211,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
211
211
  version: '0'
212
212
  segments:
213
213
  - 0
214
- hash: 3180457643805959296
214
+ hash: 4254976689102437121
215
215
  required_rubygems_version: !ruby/object:Gem::Requirement
216
216
  none: false
217
217
  requirements:
@@ -220,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
220
220
  version: '0'
221
221
  segments:
222
222
  - 0
223
- hash: 3180457643805959296
223
+ hash: 4254976689102437121
224
224
  requirements: []
225
225
  rubyforge_project:
226
226
  rubygems_version: 1.8.23