datagrid 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,11 +1,9 @@
1
- source "http://rubygems.org"
1
+ source "https://rubygems.org"
2
2
 
3
3
  gem "rails", ">= 3.0"
4
4
 
5
-
6
5
  group :development do
7
6
 
8
- gem "rake"
9
7
  gem "bundler"
10
8
  gem "jeweler"
11
9
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.0.2
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "datagrid"
8
- s.version = "1.0.1"
8
+ s.version = "1.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Bogdan Gusiev"]
12
- s.date = "2013-10-14"
12
+ s.date = "2013-11-25"
13
13
  s.description = "This allows you to easily build datagrid aka data tables with sortable columns and filters"
14
14
  s.email = "agresso@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -114,7 +114,6 @@ Gem::Specification.new do |s|
114
114
 
115
115
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
116
116
  s.add_runtime_dependency(%q<rails>, [">= 3.0"])
117
- s.add_development_dependency(%q<rake>, [">= 0"])
118
117
  s.add_development_dependency(%q<bundler>, [">= 0"])
119
118
  s.add_development_dependency(%q<jeweler>, [">= 0"])
120
119
  s.add_development_dependency(%q<debugger>, [">= 0"])
@@ -129,7 +128,6 @@ Gem::Specification.new do |s|
129
128
  s.add_development_dependency(%q<fastercsv>, [">= 0"])
130
129
  else
131
130
  s.add_dependency(%q<rails>, [">= 3.0"])
132
- s.add_dependency(%q<rake>, [">= 0"])
133
131
  s.add_dependency(%q<bundler>, [">= 0"])
134
132
  s.add_dependency(%q<jeweler>, [">= 0"])
135
133
  s.add_dependency(%q<debugger>, [">= 0"])
@@ -145,7 +143,6 @@ Gem::Specification.new do |s|
145
143
  end
146
144
  else
147
145
  s.add_dependency(%q<rails>, [">= 3.0"])
148
- s.add_dependency(%q<rake>, [">= 0"])
149
146
  s.add_dependency(%q<bundler>, [">= 0"])
150
147
  s.add_dependency(%q<jeweler>, [">= 0"])
151
148
  s.add_dependency(%q<debugger>, [">= 0"])
@@ -15,12 +15,36 @@ module Datagrid
15
15
  class_attribute :default_column_options
16
16
  self.default_column_options = {}
17
17
 
18
+ class_attribute :batch_size
19
+ self.batch_size = 1000
20
+
18
21
  end
19
22
  base.send :include, InstanceMethods
20
23
  end # self.included
21
24
 
22
25
  module ClassMethods
23
26
 
27
+ ##
28
+ # :method: batch_size=
29
+ #
30
+ # :call-seq: batch_size=(size)
31
+ #
32
+ # Specify a default batch size when generating CSV or just data
33
+ # Default: 1000
34
+ #
35
+ # self.batch_size = 500
36
+ # # Disable batches
37
+ # self.batch_size = nil
38
+ #
39
+
40
+ ##
41
+ # :method: batch_size
42
+ #
43
+ # :call-seq: batch_size
44
+ #
45
+ # Returns specified batch_size configuration variable
46
+ # See <tt>batch_size=</tt> for more information
47
+ #
24
48
 
25
49
  ##
26
50
  # :method: default_column_options=
@@ -83,6 +107,7 @@ module Datagrid
83
107
  # Warning: using ruby to order large datasets is very unrecommended.
84
108
  # If set to true - datagrid will use column value to order by this column
85
109
  # If block is given - datagrid will use value returned from block
110
+ # * <tt>:mandatory</tt> - if true, column will never be hidden with #column_names selection
86
111
  # * <tt>:url</tt> - a proc with one argument, pass this option to easily convert the value into an URL
87
112
  # * <tt>:before</tt> - determines the position of this column, by adding it before the column passed here
88
113
  # * <tt>:after</tt> - determines the position of this column, by adding it after the column passed here
@@ -173,8 +198,7 @@ module Datagrid
173
198
  #
174
199
  # * <tt>column_names</tt> - list of column names if you want to limit data only to specified columns
175
200
  def rows(*column_names)
176
- #TODO: find in batches
177
- driver.batch_map(assets) do |asset|
201
+ map_with_batches do |asset|
178
202
  self.row_for(asset, *column_names)
179
203
  end
180
204
  end
@@ -205,7 +229,7 @@ module Datagrid
205
229
  # MyGrid.new.data_hash # => [{:name => "One"}, {:name => "Two"}]
206
230
  #
207
231
  def data_hash
208
- driver.batch_map(assets) do |asset|
232
+ map_with_batches do |asset|
209
233
  hash_for(asset)
210
234
  end
211
235
  end
@@ -297,6 +321,16 @@ module Datagrid
297
321
  end
298
322
  end
299
323
 
324
+ protected
325
+
326
+ def map_with_batches(&block)
327
+ if batch_size && batch_size > 0
328
+ driver.batch_map(assets, batch_size, &block)
329
+ else
330
+ assets.map(&block)
331
+ end
332
+ end
333
+
300
334
  end # InstanceMethods
301
335
 
302
336
  end
@@ -159,6 +159,7 @@ module Datagrid
159
159
  self.class.send :check_scope_defined!, message
160
160
  end
161
161
 
162
+
162
163
  end # InstanceMethods
163
164
  end
164
165
  end
@@ -78,7 +78,7 @@ module Datagrid
78
78
  raise NotImplementedError
79
79
  end
80
80
 
81
- def batch_map(scope)
81
+ def batch_map(scope, batch_size, &block)
82
82
  raise NotImplementedError
83
83
  end
84
84
 
@@ -90,9 +90,9 @@ module Datagrid
90
90
  end
91
91
  end
92
92
 
93
- def batch_map(scope, &block)
93
+ def batch_map(scope, batch_size, &block)
94
94
  result = []
95
- scope.find_each do |record|
95
+ scope.find_each(batch_size ? { :batch_size => batch_size} : {}) do |record|
96
96
  result << yield(record)
97
97
  end
98
98
  result
@@ -65,7 +65,7 @@ module Datagrid
65
65
  []
66
66
  end
67
67
 
68
- def batch_map(scope, &block)
68
+ def batch_map(scope, batch_size, &block)
69
69
  scope.map(&block)
70
70
  end
71
71
  end
@@ -56,8 +56,17 @@ module Datagrid
56
56
  [] # TODO: implement support
57
57
  end
58
58
 
59
- def batch_map(scope, &block)
60
- scope.map(&block)
59
+ def batch_map(scope, batch_size, &block)
60
+ current_page = 0
61
+ result = []
62
+ loop do
63
+ batch = scope.skip(current_page * batch_size).limit(batch_size).to_a
64
+ return result if batch.empty?
65
+ scope.skip(current_page * batch_size).limit(batch_size).each do |item|
66
+ result << yield(item)
67
+ end
68
+ current_page+=1
69
+ end
61
70
  end
62
71
  end
63
72
  end
@@ -79,8 +79,17 @@ module Datagrid
79
79
  return :string
80
80
  end
81
81
 
82
- def batch_map(scope, &block)
83
- scope.map(&block)
82
+ def batch_map(scope, batch_size, &block)
83
+ current_page = 0
84
+ result = []
85
+ loop do
86
+ batch = scope.skip(current_page * batch_size).limit(batch_size).to_a
87
+ return result if batch.empty?
88
+ scope.skip(current_page * batch_size).limit(batch_size).each do |item|
89
+ result << yield(item)
90
+ end
91
+ current_page+=1
92
+ end
84
93
  end
85
94
  end
86
95
  end
@@ -36,17 +36,17 @@ module Datagrid
36
36
 
37
37
  def datagrid_enum_filter(attribute_or_filter, options = {})
38
38
  filter = datagrid_get_filter(attribute_or_filter)
39
- if !options.has_key?(:multiple) && filter.multiple?
40
- options[:multiple] = true
41
- end
42
39
  if filter.checkboxes?
43
- filter.select(options).map do |element|
40
+ filter.select(object).map do |element|
44
41
  text, value = @template.send(:option_text_and_value, element)
45
- id = [object_name, filter.name, value.underscore].join('_')
46
- input = check_box(filter.name, options.merge(datagrid_extra_checkbox_options).reverse_merge(:id => id, :multiple => true), value, nil)
47
- label(filter.name, input + text, :for => id)
42
+ id = [object_name, filter.name, value].join('_').underscore
43
+ input = check_box(filter.name, datagrid_extra_checkbox_options.reverse_merge(:id => id, :multiple => true), value.to_s, nil)
44
+ label(filter.name, input + text, options.reverse_merge(:for => id))
48
45
  end.join("\n").html_safe
49
46
  else
47
+ if !options.has_key?(:multiple) && filter.multiple?
48
+ options[:multiple] = true
49
+ end
50
50
  select filter.name, filter.select(object) || [], {:include_blank => filter.include_blank, :prompt => filter.prompt, :include_hidden => false}, options
51
51
  end
52
52
  end
@@ -177,4 +177,33 @@ describe Datagrid::Columns do
177
177
  report.assets.should == [second, first]
178
178
  end
179
179
  end
180
+
181
+ describe "fetching data in batches" do
182
+ it "should pass the batch size to the find_each method" do
183
+ report = test_report do
184
+ scope { Entry }
185
+ column :id
186
+ self.batch_size = 25
187
+ end
188
+
189
+ fake_assets = double(:assets)
190
+ report.should_receive(:assets) { fake_assets }
191
+ fake_assets.should_receive(:find_each).with(batch_size: 25)
192
+ report.rows
193
+ end
194
+ it "should be able to disable batches" do
195
+ report = test_report do
196
+ scope { Entry }
197
+ column :id
198
+ self.batch_size = 0
199
+ end
200
+
201
+ fake_assets = double(:assets)
202
+
203
+ report.should_receive(:assets) { fake_assets }
204
+ fake_assets.should_receive(:map)
205
+ fake_assets.should_not_receive(:find_each)
206
+ report.rows
207
+ end
208
+ end
180
209
  end
@@ -61,5 +61,15 @@ describe Datagrid::Drivers::Mongoid do
61
61
  end
62
62
  }.should raise_error(Datagrid::OrderUnsupported)
63
63
  end
64
+
65
+ it "should support batch_size" do
66
+ report = test_report do
67
+ scope { MongoidEntry }
68
+ self.batch_size = 1
69
+ column(:name)
70
+ end
71
+
72
+ report.data.should == [["Name"], ["Main First"], ["Main Second"]]
73
+ end
64
74
  end
65
75
  end
@@ -236,8 +236,8 @@ describe Datagrid::FormBuilder do
236
236
  let(:_filter) { :category }
237
237
  it { should equal_to_dom(
238
238
  '
239
- <label for="report_category_first"><input class="category enum_filter" id="report_category_first" name="report[category][]" type="checkbox" value="first" />first</label>
240
- <label for="report_category_second"><input class="category enum_filter" id="report_category_second" name="report[category][]" type="checkbox" value="second" />second</label>
239
+ <label class="category enum_filter" for="report_category_first"><input id="report_category_first" name="report[category][]" type="checkbox" value="first" />first</label>
240
+ <label class="category enum_filter" for="report_category_second"><input id="report_category_second" name="report[category][]" type="checkbox" value="second" />second</label>
241
241
  '
242
242
  )}
243
243
  end
@@ -53,6 +53,26 @@ describe SimpleReport do
53
53
  end
54
54
  end
55
55
 
56
+ describe ".batch_size" do
57
+ context "when not defined on class level" do
58
+ it "returns nil" do
59
+ subject.batch_size.should == 1000
60
+ end
61
+ end
62
+
63
+ context "when defined in the grid class" do
64
+ subject do
65
+ test_report do
66
+ self.batch_size = 25
67
+ end
68
+ end
69
+
70
+ it "returns the configured batch size" do
71
+ subject.batch_size.should == 25
72
+ end
73
+ end
74
+
75
+ end
56
76
 
57
77
 
58
78
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datagrid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-14 00:00:00.000000000 Z
12
+ date: 2013-11-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -27,22 +27,6 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '3.0'
30
- - !ruby/object:Gem::Dependency
31
- name: rake
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: '0'
38
- type: :development
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
46
30
  - !ruby/object:Gem::Dependency
47
31
  name: bundler
48
32
  requirement: !ruby/object:Gem::Requirement
@@ -344,7 +328,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
344
328
  version: '0'
345
329
  segments:
346
330
  - 0
347
- hash: 1961249610796694956
331
+ hash: -4547966346096518826
348
332
  required_rubygems_version: !ruby/object:Gem::Requirement
349
333
  none: false
350
334
  requirements: