datagrid 1.5.9 → 1.6.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/Readme.markdown +6 -4
  4. data/app/views/datagrid/_table.html.erb +1 -1
  5. data/datagrid.gemspec +29 -166
  6. data/lib/datagrid/column_names_attribute.rb +15 -16
  7. data/lib/datagrid/columns.rb +34 -37
  8. data/lib/datagrid/columns/column.rb +4 -0
  9. data/lib/datagrid/core.rb +13 -1
  10. data/lib/datagrid/drivers/active_record.rb +2 -1
  11. data/lib/datagrid/drivers/mongoid.rb +5 -1
  12. data/lib/datagrid/filters.rb +3 -6
  13. data/lib/datagrid/filters/base_filter.rb +8 -0
  14. data/lib/datagrid/filters/composite_filters.rb +4 -4
  15. data/lib/datagrid/form_builder.rb +18 -15
  16. data/lib/datagrid/helper.rb +3 -3
  17. data/lib/datagrid/renderer.rb +2 -5
  18. data/lib/datagrid/utils.rb +2 -6
  19. data/lib/datagrid/version.rb +3 -0
  20. metadata +15 -201
  21. data/.document +0 -5
  22. data/.rspec +0 -1
  23. data/.travis.yml +0 -21
  24. data/Gemfile +0 -29
  25. data/Rakefile +0 -43
  26. data/VERSION +0 -1
  27. data/spec/datagrid/active_model_spec.rb +0 -33
  28. data/spec/datagrid/column_names_attribute_spec.rb +0 -86
  29. data/spec/datagrid/columns/column_spec.rb +0 -19
  30. data/spec/datagrid/columns_spec.rb +0 -592
  31. data/spec/datagrid/core_spec.rb +0 -196
  32. data/spec/datagrid/drivers/active_record_spec.rb +0 -79
  33. data/spec/datagrid/drivers/array_spec.rb +0 -106
  34. data/spec/datagrid/drivers/mongo_mapper_spec.rb +0 -101
  35. data/spec/datagrid/drivers/mongoid_spec.rb +0 -109
  36. data/spec/datagrid/drivers/sequel_spec.rb +0 -111
  37. data/spec/datagrid/filters/base_filter_spec.rb +0 -19
  38. data/spec/datagrid/filters/boolean_enum_filter_spec.rb +0 -5
  39. data/spec/datagrid/filters/composite_filters_spec.rb +0 -65
  40. data/spec/datagrid/filters/date_filter_spec.rb +0 -195
  41. data/spec/datagrid/filters/date_time_filter_spec.rb +0 -157
  42. data/spec/datagrid/filters/dynamic_filter_spec.rb +0 -175
  43. data/spec/datagrid/filters/enum_filter_spec.rb +0 -51
  44. data/spec/datagrid/filters/extended_boolean_filter_spec.rb +0 -46
  45. data/spec/datagrid/filters/float_filter_spec.rb +0 -15
  46. data/spec/datagrid/filters/integer_filter_spec.rb +0 -144
  47. data/spec/datagrid/filters/string_filter_spec.rb +0 -35
  48. data/spec/datagrid/filters_spec.rb +0 -332
  49. data/spec/datagrid/form_builder_spec.rb +0 -614
  50. data/spec/datagrid/helper_spec.rb +0 -640
  51. data/spec/datagrid/ordering_spec.rb +0 -150
  52. data/spec/datagrid/scaffold_spec.rb +0 -45
  53. data/spec/datagrid/stylesheet_spec.rb +0 -12
  54. data/spec/datagrid/utils_spec.rb +0 -19
  55. data/spec/datagrid_spec.rb +0 -83
  56. data/spec/spec_helper.rb +0 -114
  57. data/spec/support/active_record.rb +0 -38
  58. data/spec/support/configuration.rb +0 -28
  59. data/spec/support/i18n_helpers.rb +0 -6
  60. data/spec/support/matchers.rb +0 -101
  61. data/spec/support/mongo_mapper.rb +0 -32
  62. data/spec/support/mongoid.rb +0 -36
  63. data/spec/support/sequel.rb +0 -39
  64. data/spec/support/simple_report.rb +0 -64
  65. data/spec/support/test_partials/_actions.html.erb +0 -1
  66. data/spec/support/test_partials/client/datagrid/_form.html.erb +0 -13
  67. data/spec/support/test_partials/client/datagrid/_head.html.erb +0 -9
  68. data/spec/support/test_partials/client/datagrid/_order_for.html.erb +0 -11
  69. data/spec/support/test_partials/client/datagrid/_row.html.erb +0 -6
  70. data/spec/support/test_partials/client/datagrid/_table.html.erb +0 -19
  71. data/spec/support/test_partials/custom_checkboxes/_enum_checkboxes.html.erb +0 -1
  72. data/spec/support/test_partials/custom_form/_form.html.erb +0 -7
  73. data/spec/support/test_partials/custom_range/_range_filter.html.erb +0 -1
data/.document DELETED
@@ -1,5 +0,0 @@
1
- lib/**/*.rb
2
- bin/*
3
- -
4
- features/**/*.feature
5
- LICENSE.txt
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --color
data/.travis.yml DELETED
@@ -1,21 +0,0 @@
1
- language: ruby
2
- #bundler_args: --without development
3
- rvm:
4
- - 2.2.2
5
- - 2.3.0
6
- - 2.4.1
7
- - 2.5.1
8
- #- jruby
9
- #- jruby-head
10
- #env: JRUBY_OPTS="--server -J-Xms512m -J-Xmx1024m"
11
- #matrix:
12
- #allow_failures:
13
- #- rvm: jruby-head
14
- #- gemfile: gemfiles/rails41.gemfile
15
- notifications:
16
- email: false
17
- services:
18
- - mongodb
19
- gemfile:
20
- - Gemfile
21
- #- gemfiles/rails41.gemfile
data/Gemfile DELETED
@@ -1,29 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem "rails", ">= 4.0"
4
-
5
- group :development do
6
-
7
- gem "bundler"
8
- if RUBY_VERSION >= "2.3"
9
- gem "jeweler", ">= 2.1.2", platform: [:ruby_23, :ruby_24, :ruby_25]
10
- end
11
-
12
-
13
- #gem "json", ">= 1.9"
14
- gem "pry-byebug", :platform => [:ruby_24, :ruby_25, :ruby_22, :ruby_23] & Bundler::Dsl::VALID_PLATFORMS
15
-
16
- gem "rspec", ">= 3"
17
- gem "nokogiri" # used to test html output
18
-
19
- gem "sqlite3", "~> 1.3.6"
20
- gem "sequel"
21
-
22
- group :mongo do
23
- gem "mongoid"
24
- #gem "mongo_mapper", "~> 0.13.0"
25
- gem "bson"
26
- gem "bson_ext"
27
- end
28
-
29
- end
data/Rakefile DELETED
@@ -1,43 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'rubygems'
4
- require 'bundler'
5
- begin
6
- Bundler.setup(:default, :development)
7
- rescue Bundler::BundlerError => e
8
- $stderr.puts e.message
9
- $stderr.puts "Run `bundle install` to install missing gems"
10
- exit e.status_code
11
- end
12
- require 'rake'
13
-
14
- if RUBY_VERSION >= "2.3"
15
- require 'jeweler'
16
- Jeweler::Tasks.new do |gem|
17
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
18
- gem.name = "datagrid"
19
- gem.homepage = "http://github.com/bogdan/datagrid"
20
- gem.license = "MIT"
21
- gem.summary = %Q{Ruby gem to create datagrids}
22
- gem.description = %Q{This allows you to easily build datagrid aka data tables with sortable columns and filters}
23
- gem.email = "agresso@gmail.com"
24
- gem.authors = ["Bogdan Gusiev"]
25
- gem.required_ruby_version = '>= 2.0'
26
- # dependencies defined in Gemfile
27
- end
28
- Jeweler::RubygemsDotOrgTasks.new
29
- end
30
-
31
- require 'rspec/core'
32
- require 'rspec/core/rake_task'
33
- RSpec::Core::RakeTask.new(:spec) do |spec|
34
- spec.pattern = FileList['spec/**/*_spec.rb']
35
- end
36
-
37
- RSpec::Core::RakeTask.new(:rcov) do |spec|
38
- spec.pattern = 'spec/**/*_spec.rb'
39
- spec.rcov = true
40
- end
41
-
42
- task :default => :spec
43
-
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 1.5.9
@@ -1,33 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Datagrid::ActiveModel do
4
-
5
- class ActiveReport
6
- include Datagrid::ActiveModel
7
- end
8
-
9
- module Grid
10
- class ActiveReport
11
- include Datagrid::ActiveModel
12
- end
13
- end
14
-
15
- describe ".model_name" do
16
- it "should be generate from class name " do
17
- expect(ActiveReport.model_name).to eq("ActiveReport")
18
- end
19
- it "should have ActiveModel naming conventions" do
20
- expect(ActiveReport.model_name.i18n_key).to eq(:active_report)
21
- end
22
- end
23
-
24
- describe ".param_name" do
25
- it "should make right param key from simple class name" do
26
- expect(ActiveReport.param_name).to eq('active_report')
27
- end
28
- it "should make right param key from class of module" do
29
- expect(Grid::ActiveReport.param_name).to eq('grid_active_report')
30
- end
31
- end
32
-
33
- end
@@ -1,86 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Datagrid::ColumnNamesAttribute do
4
-
5
- let(:column_names_filter_options) do
6
- {}
7
- end
8
-
9
- let(:report) do
10
- options = column_names_filter_options
11
- test_report do
12
- scope { Entry }
13
- column_names_filter(options)
14
- column(:id)
15
- column(:name, :mandatory => true)
16
- column(:category)
17
- end
18
- end
19
- subject { report }
20
-
21
-
22
- let!(:entry) do
23
- Entry.create!(:name => 'hello', :category => 'greeting')
24
- end
25
-
26
- it "should work" do
27
- subject.column_names = [:id]
28
- expect(subject.mandatory_columns.map(&:name)).to eq([:name])
29
- expect(subject.optional_columns.map(&:name)).to eq([:id, :category])
30
- expect(subject.data).to eq([["Id", "Name"], [entry.id, "hello"]])
31
- columns_filter = subject.filter_by_name(:column_names)
32
- expect(columns_filter).not_to be_nil
33
- expect(columns_filter.select(subject)).to eq([["Id", :id], ["Category", :category]])
34
- end
35
-
36
- it "should show only mandatory columns by default" do
37
- expect(subject.row_for(entry)).to eq([ "hello" ])
38
- subject.column_names = ["name", "category"]
39
- expect(subject.row_for(entry)).to eq(["hello", "greeting"])
40
- end
41
-
42
- it "should show mandatory columns even if they are unselected" do
43
- subject.column_names = ["category"]
44
- expect(subject.row_for(entry)).to eq(["hello", "greeting"])
45
- expect(subject.data).to eq([["Name", "Category"], ["hello", "greeting"]])
46
- end
47
-
48
- it "should find any column by name" do
49
- expect(subject.column_by_name(:id)).not_to be_nil
50
- expect(subject.column_by_name(:name)).not_to be_nil
51
- expect(subject.column_by_name(:category)).not_to be_nil
52
- end
53
-
54
-
55
- context "when default option is passed to column_names_filter" do
56
- let(:column_names_filter_options) do
57
- { :default => [:id] }
58
- end
59
-
60
- describe '#data' do
61
- subject { super().data }
62
- it { should == [["Id", "Name"], [entry.id, 'hello']] }
63
- end
64
-
65
- end
66
-
67
- context "when some columns are disabled" do
68
- subject do
69
- test_report do
70
- scope {Entry}
71
- column(:id, :mandatory => true)
72
- column(:name)
73
- column(:category, if: proc { false })
74
- column(:group, :mandatory => true, if: proc { false })
75
- end
76
- end
77
-
78
- it "excludes them from mandatory_columns" do
79
- expect(subject.mandatory_columns.map(&:name)).to eq([:id])
80
- end
81
-
82
- it "excludes them from optional_columns" do
83
- expect(subject.optional_columns.map(&:name)).to eq([:name])
84
- end
85
- end
86
- end
@@ -1,19 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Datagrid::Columns::Column do
4
-
5
- describe ".inspect" do
6
- subject do
7
- class ColumnInspectTest
8
- include Datagrid
9
- scope {Entry}
10
- column(:id, header: "ID")
11
- end
12
- ColumnInspectTest.column_by_name(:id)
13
- end
14
-
15
- it "shows inspect information" do
16
- expect(subject.inspect).to eq("#<Datagrid::Columns::Column ColumnInspectTest#id {:header=>\"ID\"}>")
17
- end
18
- end
19
- end
@@ -1,592 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Datagrid::Columns do
4
-
5
- let(:group) { Group.create!(:name => "Pop") }
6
-
7
- subject do
8
- SimpleReport.new
9
- end
10
-
11
- describe "basic methods" do
12
-
13
- let!(:entry) { Entry.create!(
14
- :group => group,
15
- :name => "Star",
16
- :disabled => false,
17
- :confirmed => false,
18
- :category => "first",
19
- :access_level => 'admin',
20
- :pet => 'rottweiler',
21
- :shipping_date => Date.new(2013, 8, 1)
22
- ) }
23
- let(:date) { Date.new(2013, 8, 1) }
24
-
25
- it "should have data columns without html columns" do
26
- grid = test_report do
27
- scope {Entry}
28
- column(:name)
29
- column(:action, :html => true) do
30
- 'dummy'
31
- end
32
- end
33
- expect(grid.data_columns.map(&:name)).to eq([:name])
34
- expect(grid.html_columns.map(&:name)).to eq([:name, :action])
35
- end
36
- it "should build rows of data" do
37
- grid = test_report do
38
- scope {Entry}
39
- column(:name)
40
- column(:action, :html => true) do
41
- 'dummy'
42
- end
43
- end
44
- expect(grid.rows).to eq([["Star"]])
45
- end
46
- it "should generate header without html columns" do
47
- grid = test_report do
48
- scope {Entry}
49
- column(:name)
50
- column(:action, :html => true) do
51
- 'dummy'
52
- end
53
- end
54
- expect(grid.header).to eq(["Name"])
55
- end
56
-
57
- describe "translations" do
58
-
59
- module ::Ns45
60
- class TranslatedReport
61
- include Datagrid
62
- scope { Entry }
63
- column(:name)
64
- end
65
- end
66
- it "translates column-header with namespace" do
67
- store_translations(:en, datagrid: {:"ns45/translated_report" => {columns: {name: "Navn"}}}) do
68
- expect(Ns45::TranslatedReport.new.header.first).to eq("Navn")
69
- end
70
- end
71
-
72
- it "translates column-header without namespace" do
73
- class Report27
74
- include Datagrid
75
- scope {Entry}
76
- column(:name)
77
- end
78
-
79
- store_translations(:en, datagrid: {:"report27" => {columns: {name: "Nombre"}}}) do
80
- expect(Report27.new.header.first).to eq("Nombre")
81
- end
82
- end
83
-
84
- it "translates column-header in using defaults namespace" do
85
- class Report27
86
- include Datagrid
87
- scope {Entry}
88
- column(:name)
89
- end
90
-
91
- store_translations(:en, datagrid: {defaults: {columns: {name: "Nombre"}}}) do
92
- expect(Report27.new.header.first).to eq("Nombre")
93
- end
94
- end
95
-
96
- end
97
-
98
- it "should return html_columns" do
99
- report = test_report do
100
- scope {Entry}
101
- column(:id)
102
- column(:name, :html => false)
103
- end
104
- expect(report.html_columns.map(&:name)).to eq([:id])
105
- end
106
-
107
- it "should return html_columns when column definition has 2 arguments" do
108
- report = test_report(:name => "Hello") do
109
- scope {Entry}
110
- filter(:name)
111
- column(:id)
112
- column(:name, :html => false) do |model, grid|
113
- "'#{model.name}' filtered by '#{grid.name}'"
114
- end
115
- end
116
- entry = Entry.create!(:name => "Hello World")
117
- expect(report.row_for(entry)).to eq([entry.id, "'Hello World' filtered by 'Hello'"])
118
- end
119
-
120
- it "should generate table data" do
121
- expect(subject.data).to eq([
122
- subject.header,
123
- subject.row_for(entry)
124
- ])
125
- end
126
-
127
- it "supports dynamic header" do
128
- grid = test_report do
129
- scope {Entry}
130
- column(:id, header: proc { rand(10**9) })
131
- end
132
-
133
- expect(grid.header).to_not eq(grid.header)
134
- end
135
-
136
- it "should generate hash for given asset" do
137
- expect(subject.hash_for(entry)).to eq({
138
- :group => "Pop",
139
- :name => "Star",
140
- :access_level => 'admin',
141
- :pet => 'ROTTWEILER',
142
- :shipping_date => date
143
- })
144
- end
145
-
146
- it "should support csv export" do
147
- expect(subject.to_csv).to eq("Shipping date,Group,Name,Access level,Pet\n#{date},Pop,Star,admin,ROTTWEILER\n")
148
- end
149
-
150
- it "should support csv export of particular columns" do
151
- expect(subject.to_csv(:name)).to eq("Name\nStar\n")
152
- end
153
-
154
- it "should support csv export options" do
155
- expect(subject.to_csv(:col_sep => ";")).to eq("Shipping date;Group;Name;Access level;Pet\n#{date};Pop;Star;admin;ROTTWEILER\n")
156
- end
157
-
158
- end
159
-
160
- it "should support columns with model and report arguments" do
161
- report = test_report(:category => "foo") do
162
- scope {Entry.order(:category)}
163
- filter(:category) do |value|
164
- where("category LIKE '%#{value}%'")
165
- end
166
-
167
- column(:exact_category) do |entry, grid|
168
- entry.category == grid.category
169
- end
170
- end
171
- Entry.create!(:category => "foo")
172
- Entry.create!(:category => "foobar")
173
- expect(report.rows.first.first).to eq(true)
174
- expect(report.rows.last.first).to eq(false)
175
- end
176
-
177
- it "should inherit columns correctly" do
178
- parent = Class.new do
179
- include Datagrid
180
- scope { Entry }
181
- column(:name)
182
- end
183
-
184
- child = Class.new(parent) do
185
- column(:group_id)
186
- end
187
- expect(parent.column_by_name(:name)).not_to be_nil
188
- expect(parent.column_by_name(:group_id)).to be_nil
189
- expect(child.column_by_name(:name)).not_to be_nil
190
- expect(child.column_by_name(:group_id)).not_to be_nil
191
- end
192
- it "should support defining a query for a column" do
193
- report = test_report do
194
- scope {Entry}
195
- filter(:name)
196
- column(:id)
197
- column(:sum_group_id, 'sum(group_id)')
198
- end
199
- Entry.create!(:group => group)
200
- expect(report.assets.first.sum_group_id).to eq(group.id)
201
- end
202
-
203
- it "should support post formatting for column defined with query" do
204
- report = test_report do
205
- scope {Group.joins(:entries).group("groups.id")}
206
- filter(:name)
207
- column(:entries_count, 'count(entries.id)') do |model|
208
- format("(#{model.entries_count})") do |value|
209
- content_tag(:span, value)
210
- end
211
- end
212
- end
213
- 3.times { Entry.create!(group: group) }
214
- expect(report.rows).to eq([["(3)"]])
215
- end
216
- it "should support hidding columns through if and unless" do
217
- report = test_report do
218
- scope {Entry}
219
- column(:id, :if => :show?)
220
- column(:name, :unless => proc {|grid| !grid.show? })
221
- column(:category)
222
-
223
- def show?
224
- false
225
- end
226
- end
227
- expect(report.columns(:id)).to eq([])
228
- expect(report.columns(:name)).to eq([])
229
- expect(report.available_columns.map(&:name)).to eq([:category])
230
- end
231
-
232
- it "raises when incorrect unless option is given" do
233
- expect do
234
- test_report do
235
- column(:id, if: Object.new)
236
- end
237
- end.to raise_error(Datagrid::ConfigurationError)
238
- end
239
-
240
- it "raises when :before and :after used together" do
241
- expect do
242
- test_report do
243
- column(:id)
244
- column(:name, :before => :id, :after => :name)
245
- end
246
- end.to raise_error(Datagrid::ConfigurationError)
247
- end
248
-
249
- describe ".column_names attributes" do
250
- let(:grid) do
251
- test_report(:column_names => ["id", "name"]) do
252
- scope { Entry }
253
- column(:id)
254
- column(:name)
255
- column(:category)
256
- end
257
- end
258
- let!(:entry) do
259
- Entry.create!(:name => 'hello')
260
- end
261
- it "should be suppored in header" do
262
- expect(grid.header).to eq(["Id", "Name"])
263
- end
264
- it "should be suppored in rows" do
265
- expect(grid.rows).to eq([[entry.id, "hello"]])
266
- end
267
-
268
- it "should be suppored in csv" do
269
- expect(grid.to_csv).to eq("Id,Name\n#{entry.id},hello\n")
270
- end
271
-
272
- it "should support explicit overwrite" do
273
- expect(grid.header(:id, :name, :category)).to eq(%w(Id Name Category))
274
- end
275
-
276
- end
277
-
278
-
279
- context "when grid has formatted column" do
280
- it "should output correct data" do
281
- report = test_report do
282
- scope {Entry}
283
- column(:name) do |entry|
284
- format(entry.name) do |value|
285
- "<strong>#{value}</strong"
286
- end
287
- end
288
- end
289
- Entry.create!(:name => "Hello World")
290
- expect(report.rows).to eq([["Hello World"]])
291
- end
292
- end
293
-
294
- describe ".default_column_options" do
295
- it "should pass default options to each column definition" do
296
- report = test_report do
297
- scope {Entry}
298
- self.default_column_options = {:order => false}
299
- column(:id)
300
- column(:name, :order => "name")
301
- end
302
- first = Entry.create(:name => '1st')
303
- second = Entry.create(:name => '2nd')
304
- expect do
305
- report.attributes = {:order => :id}
306
- report.assets
307
- end.to raise_error(Datagrid::OrderUnsupported)
308
- report.attributes = {:order => :name, :descending => true}
309
- expect(report.assets).to eq([second, first])
310
- end
311
- end
312
-
313
- describe "fetching data in batches" do
314
- it "should pass the batch size to the find_each method" do
315
- report = test_report do
316
- scope { Entry }
317
- column :id
318
- self.batch_size = 25
319
- end
320
-
321
- fake_assets = double(:assets)
322
- expect(report).to receive(:assets) { fake_assets }
323
- expect(fake_assets).to receive(:find_each).with(batch_size: 25)
324
- expect(fake_assets).to receive(:limit_value).and_return(nil)
325
- report.rows
326
- end
327
- it "should be able to disable batches" do
328
- report = test_report do
329
- scope { Entry }
330
- column :id
331
- self.batch_size = 0
332
- end
333
-
334
- fake_assets = double(:assets)
335
-
336
- expect(report).to receive(:assets) { fake_assets }
337
- expect(fake_assets).to receive(:each)
338
- expect(fake_assets).not_to receive(:find_each)
339
- report.rows
340
- end
341
-
342
- it "should support instance level batch size" do
343
- grid = test_report do
344
- scope {Entry}
345
- column :id
346
- self.batch_size = 25
347
- end
348
- grid.batch_size = 0
349
- fake_assets = double(:assets)
350
-
351
- expect(grid).to receive(:assets) { fake_assets }
352
- expect(fake_assets).to receive(:each)
353
- expect(fake_assets).not_to receive(:find_each)
354
- grid.rows
355
- end
356
- end
357
-
358
- describe ".data_row" do
359
- it "should give access to column values via an object" do
360
- grid = test_report do
361
- scope { Entry }
362
- column(:id)
363
- column(:name) do
364
- name.capitalize
365
- end
366
- column(:actions, html: true) do
367
- "some link here"
368
- end
369
- end
370
- entry = Entry.create!(name: 'hello')
371
- row = grid.data_row(entry)
372
- expect(row.id).to eq(entry.id)
373
- expect(row.name).to eq("Hello")
374
- expect {
375
- row.actions
376
- }.to raise_error(RuntimeError)
377
- end
378
- end
379
-
380
- describe "column value" do
381
- it "should support conversion" do
382
- group = Group.create!
383
- Entry.create(:group => group)
384
- Entry.create(:group => group)
385
- grid = test_report do
386
- scope { Group }
387
- column(:entries_count) do |g|
388
- g.entries.count
389
- end
390
- column(:odd_entries) do |_, _, row|
391
- row.entries_count.odd?
392
- end
393
- end
394
-
395
- expect(grid.row_for(group)).to eq([2, false])
396
- end
397
- end
398
-
399
- describe "instance level column definition" do
400
- let(:modified_grid) do
401
- grid = test_report do
402
- scope { Entry }
403
- column(:id)
404
- end
405
- grid.column(:name)
406
- grid
407
- end
408
-
409
- let(:basic_grid) { modified_grid.class.new }
410
- let!(:entry) { Entry.create!(:name => "Hello", :category => 'first') }
411
-
412
- it "should have correct columns" do
413
- expect(modified_grid.columns.size).to eq(2)
414
- expect(basic_grid.class.columns.size).to eq(1)
415
- expect(basic_grid.columns.size).to eq(1)
416
- end
417
-
418
- it "should give correct header" do
419
- expect(modified_grid.header).to eq(["Id", "Name"])
420
- expect(basic_grid.header).to eq(["Id"])
421
- end
422
-
423
- it "should give correct rows" do
424
- expect(modified_grid.rows).to eq([[entry.id, 'Hello']])
425
- expect(basic_grid.rows).to eq([[entry.id]])
426
- end
427
-
428
- it "should support :before column name" do
429
- modified_grid.column(:category, :before => :name)
430
- expect(modified_grid.header).to eq(["Id", "Category", "Name"])
431
- end
432
-
433
- it "should support :before all" do
434
- modified_grid.column(:category, :before => true)
435
- expect(modified_grid.header).to eq(["Category", "Id", "Name"])
436
- end
437
-
438
- it "should support columns block" do
439
- modified_grid.column(:category) do
440
- category.capitalize
441
- end
442
- expect(modified_grid.rows).to eq([[entry.id, "Hello", 'First']])
443
- end
444
-
445
- it "should support column_names accessor" do
446
- modified_grid.column_names = [:name]
447
- expect(modified_grid.rows).to eq([['Hello']])
448
- modified_grid.column_names = [:id]
449
- expect(modified_grid.rows).to eq([[entry.id]])
450
- end
451
- it "should support column_names accessor with mandatory columns" do
452
- modified_grid.column(:category, :mandatory => true)
453
- modified_grid.column_names = [:name]
454
- expect(modified_grid.rows).to eq([['Hello', 'first']])
455
- basic_grid.column_names = [:id]
456
- expect(basic_grid.rows).to eq([[entry.id]])
457
- end
458
-
459
- it "should support available columns" do
460
- modified_grid.column(:category, :mandatory => true)
461
- expect(modified_grid.available_columns.map(&:name)).to eq([:id, :name, :category])
462
- end
463
-
464
- it "should respect column availability criteria" do
465
- modified_grid.column(:category, :if => proc { false })
466
- expect(modified_grid.columns.map(&:name)).to eq([:id, :name])
467
- end
468
- end
469
-
470
- describe ".data_value" do
471
- it "should return value" do
472
- grid = test_report do
473
- scope {Entry }
474
- column(:name)
475
- end
476
- expect(grid.data_value(:name, Entry.create!(:name => 'Hello'))).to eq('Hello')
477
- end
478
- it "should raise for disabled columns" do
479
- grid = test_report do
480
- scope {Entry }
481
- column(:name, :if => proc { false })
482
- end
483
- expect {
484
- grid.data_value(:name, Entry.create!(:name => 'Hello'))
485
- }.to raise_error(Datagrid::ColumnUnavailableError)
486
- end
487
- end
488
-
489
- describe "caching" do
490
- it "should work when enabled in class" do
491
- grid = test_report do
492
- scope {Entry}
493
- self.cached = true
494
- column(:random1) {rand(10**9)}
495
- column(:random2) {rand(10**9)}
496
- end
497
-
498
- row = grid.data_row(Entry.create!)
499
- expect(row.random1).to eq(row.random1)
500
- expect(row.random2).to_not eq(row.random1)
501
- grid.cached = false
502
- expect(row.random2).to_not eq(row.random2)
503
- expect(row.random2).to_not eq(row.random1)
504
- end
505
- end
506
-
507
- describe "decoration" do
508
- class EntryDecorator
509
- attr_reader :model
510
- def initialize(model)
511
- @model = model
512
- end
513
- def capitalized_name
514
- model.name.capitalize
515
- end
516
- end
517
-
518
- let!(:entry) do
519
- Entry.create!(name: 'hello', category: 'first')
520
- end
521
-
522
- it "delegates column values to decorator" do
523
- grid = test_report do
524
- scope { Entry }
525
- decorate { |model| EntryDecorator.new(model) }
526
- column(:capitalized_name)
527
- column(:category) do |presenter|
528
- presenter.model.category
529
- end
530
- column(:capitalized_name_dup) do |_, _, row|
531
- row.capitalized_name
532
- end
533
- end
534
-
535
- expect(grid.rows).to eq([['Hello', 'first', 'Hello']])
536
- end
537
-
538
- it "allows class decorator" do
539
- grid = test_report do
540
- scope { Entry }
541
- decorate { EntryDecorator }
542
- column(:capitalized_name)
543
- end
544
- expect(grid.rows).to eq([['Hello']])
545
- end
546
- end
547
-
548
- describe "column scope" do
549
- it "appends preload as non block" do
550
- grid = test_report do
551
- scope { Entry }
552
- column(:id, preload: [:group])
553
- end
554
- expect(grid.assets.preload_values).to_not be_blank
555
- end
556
-
557
- it "appends preload with no args" do
558
- grid = test_report do
559
- scope { Entry }
560
- column(:id, preload: -> { order(:id) })
561
- end
562
- expect(grid.assets.order_values).to_not be_blank
563
- end
564
-
565
- it "appends preload with arg" do
566
- grid = test_report do
567
- scope { Entry }
568
- column(:id, preload: ->(a) { a.order(:id) })
569
- end
570
- expect(grid.assets.order_values).to_not be_blank
571
- end
572
-
573
- it "appends preload as true value" do
574
- grid = test_report do
575
- scope { Entry }
576
- column(:group, preload: true)
577
- end
578
- expect(grid.assets.preload_values).to eq([:group])
579
- end
580
-
581
- it "doesn't append preload when column is invisible" do
582
- grid = test_report do
583
- scope { Entry }
584
- column(:id1, preload: ->(a) { a.order(:id) })
585
- column(:id2, preload: ->(a) { a.order(:id) }, if: ->(a) { false })
586
- column(:name)
587
- end
588
- grid.column_names = [:name]
589
- expect(grid.assets.order_values).to be_blank
590
- end
591
- end
592
- end