/
columns_spec.rb
95 lines (79 loc) · 2.45 KB
/
columns_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require 'spec_helper'
describe Datagrid::Columns do
let(:group) { Group.create!(:name => "Pop") }
subject do
SimpleReport.new
end
describe "basic methods" do
let!(:entry) { Entry.create!(
:group => group,
:name => "Star",
:disabled => false,
:confirmed => false,
:category => "first",
:access_level => 'admin',
:pet => 'rottweiler'
) }
it "should have data columns without html columns" do
subject.data_columns.size.should == subject.columns.size - 1
end
it "should build rows of data" do
subject.rows.should == [["Pop", "Star", "admin", "ROTTWEILER"]]
end
it "should generate header" do
subject.header.should == ["Group", "Name", "Access level", "Pet"]
end
it "should generate table data" do
subject.data.should == [
subject.header,
subject.row_for(entry)
]
end
it "should generate hash for given asset" do
subject.hash_for(entry).should == {
:group => "Pop",
:name => "Star",
:access_level => 'admin',
:pet => 'ROTTWEILER'
}
end
it "should support csv export" do
subject.to_csv.should == "Group,Name,Access level,Pet\nPop,Star,admin,ROTTWEILER\n"
end
it "should support csv export of particular columns" do
subject.to_csv(:name).should == "Name\nStar\n"
end
it "should support csv export options" do
subject.to_csv(:col_sep => ";").should == "Group;Name;Access level;Pet\nPop;Star;admin;ROTTWEILER\n"
end
end
it "should support columns with model and report arguments" do
report = test_report(:category => "foo") do
scope {Entry.order(:category)}
filter(:category) do |value|
where("category LIKE '%#{value}%'")
end
column(:exact_category) do |entry, grid|
entry.category == grid.category
end
end
Entry.create!(:category => "foo")
Entry.create!(:category => "foobar")
report.rows.first.first.should be_true
report.rows.last.first.should be_false
end
it "should inherit columns correctly" do
parent = Class.new do
include Datagrid
scope { Entry }
column(:name)
end
child = Class.new(parent) do
column(:group_id)
end
parent.column_by_name(:name).should_not be_nil
parent.column_by_name(:group_id).should be_nil
child.column_by_name(:name).should_not be_nil
child.column_by_name(:group_id).should_not be_nil
end
end