rgviz-rails 0.9 → 0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,6 +7,7 @@ module Rgviz
7
7
  @model_class = model_class
8
8
  @query = query
9
9
  @joins = {}
10
+ @labels = {}
10
11
  case ActiveRecord::Base.connection.adapter_name.downcase
11
12
  when 'sqlite'
12
13
  @adapter = SqliteAdapter.new
@@ -19,6 +20,8 @@ module Rgviz
19
20
  @table = Table.new
20
21
  @extra_conditions = options[:conditions]
21
22
 
23
+ process_labels
24
+
22
25
  generate_columns
23
26
  generate_conditions
24
27
  generate_group
@@ -28,6 +31,14 @@ module Rgviz
28
31
  @table
29
32
  end
30
33
 
34
+ def process_labels
35
+ return unless @query.labels.present?
36
+
37
+ @query.labels.each do |label|
38
+ @labels[label.column.to_s] = label.label
39
+ end
40
+ end
41
+
31
42
  def add_joins(joins)
32
43
  map = @joins
33
44
  joins.each do |join|
@@ -45,7 +56,7 @@ module Rgviz
45
56
  # Select the specified columns
46
57
  i = 0
47
58
  @query.select.columns.each do |col|
48
- @table.cols << (Column.new :id => column_id(col, i), :type => column_type(col))
59
+ @table.cols << (Column.new :id => column_id(col, i), :type => column_type(col), :label => column_label(col.to_s))
49
60
  @selects << "(#{column_select(col)}) as c#{i}"
50
61
  i += 1
51
62
  end
@@ -53,7 +64,7 @@ module Rgviz
53
64
  # Select all columns
54
65
  i = 0
55
66
  @model_class.send(:columns).each do |col|
56
- @table.cols << (Column.new :id => col.name, :type => (rails_column_type col))
67
+ @table.cols << (Column.new :id => col.name, :type => (rails_column_type col), :label => column_label(col.name))
57
68
  @selects << "(#{col.name}) as c#{i}"
58
69
  i += 1
59
70
  end
@@ -167,6 +178,10 @@ module Rgviz
167
178
  end
168
179
  end
169
180
 
181
+ def column_label(string)
182
+ @labels[string] || string
183
+ end
184
+
170
185
  def to_string(node, visitor_class)
171
186
  visitor = visitor_class.new self
172
187
  node.accept visitor
@@ -25,7 +25,7 @@ describe Executor do
25
25
  date.strftime "%Y-%m-%d"
26
26
  end
27
27
 
28
- def self.it_processes_single_select_column(query, id, type, value)
28
+ def self.it_processes_single_select_column(query, id, type, value, label)
29
29
  it "processes select #{query}" do
30
30
  if block_given?
31
31
  yield
@@ -38,6 +38,7 @@ describe Executor do
38
38
 
39
39
  table.cols[0].id.should == id
40
40
  table.cols[0].type.should == type
41
+ table.cols[0].label.should == label
41
42
 
42
43
  table.rows.length.should == 1
43
44
  table.rows[0].c.length.should == 1
@@ -58,6 +59,7 @@ describe Executor do
58
59
  ['city_id', :number]].each do |id, type|
59
60
  table.cols[i].id.should == id
60
61
  table.cols[i].type.should == type
62
+ table.cols[i].label.should == id
61
63
  i += 1
62
64
  end
63
65
 
@@ -72,61 +74,61 @@ describe Executor do
72
74
  end
73
75
  end
74
76
 
75
- it_processes_single_select_column 'name', 'name', :string, 'foo' do
77
+ it_processes_single_select_column 'name', 'name', :string, 'foo', 'name' do
76
78
  Person.make :name => 'foo'
77
79
  end
78
80
 
79
- it_processes_single_select_column '1', 'c0', :number, 1
80
- it_processes_single_select_column '1.2', 'c0', :number, 1.2
81
- it_processes_single_select_column '"hello"', 'c0', :string, 'hello'
82
- it_processes_single_select_column 'false', 'c0', :boolean, false
83
- it_processes_single_select_column 'true', 'c0', :boolean, true
84
- it_processes_single_select_column 'date "2010-01-02"', 'c0', :date, '2010-01-02'
85
- it_processes_single_select_column 'datetime "2010-01-02 10:11:12"', 'c0', :datetime, '2010-01-02 10:11:12'
86
- it_processes_single_select_column 'timeofday "10:11:12"', 'c0', :timeofday, '10:11:12'
87
-
88
- it_processes_single_select_column '1 + 2', 'c0', :number, 3
89
- it_processes_single_select_column '3 - 2', 'c0', :number, 1
90
- it_processes_single_select_column '2 * 3', 'c0', :number, 6
91
- it_processes_single_select_column '6 / 3', 'c0', :number, 2
92
- it_processes_single_select_column '3 * age', 'c0', :number, 60 do
81
+ it_processes_single_select_column '1', 'c0', :number, 1, '1'
82
+ it_processes_single_select_column '1.2', 'c0', :number, 1.2, '1.2'
83
+ it_processes_single_select_column '"hello"', 'c0', :string, 'hello', "'hello'"
84
+ it_processes_single_select_column 'false', 'c0', :boolean, false, 'false'
85
+ it_processes_single_select_column 'true', 'c0', :boolean, true, 'true'
86
+ it_processes_single_select_column 'date "2010-01-02"', 'c0', :date, '2010-01-02', "date '2010-01-02'"
87
+ it_processes_single_select_column 'datetime "2010-01-02 10:11:12"', 'c0', :datetime, '2010-01-02 10:11:12', "datetime '2010-01-02 10:11:12'"
88
+ it_processes_single_select_column 'timeofday "10:11:12"', 'c0', :timeofday, '10:11:12', "timeofday '10:11:12'"
89
+
90
+ it_processes_single_select_column '1 + 2', 'c0', :number, 3, '1 + 2'
91
+ it_processes_single_select_column '3 - 2', 'c0', :number, 1, '3 - 2'
92
+ it_processes_single_select_column '2 * 3', 'c0', :number, 6, '2 * 3'
93
+ it_processes_single_select_column '6 / 3', 'c0', :number, 2, '6 / 3'
94
+ it_processes_single_select_column '3 * age', 'c0', :number, 60, '3 * age' do
93
95
  Person.make :age => 20
94
96
  end
95
97
 
96
- it_processes_single_select_column 'sum(age)', 'c0', :number, 6 do
98
+ it_processes_single_select_column 'sum(age)', 'c0', :number, 6, 'sum(age)' do
97
99
  [1, 2, 3].each{|i| Person.make :age => i}
98
100
  end
99
101
 
100
- it_processes_single_select_column 'avg(age)', 'c0', :number, 30 do
102
+ it_processes_single_select_column 'avg(age)', 'c0', :number, 30, 'avg(age)' do
101
103
  [10, 20, 60].each{|i| Person.make :age => i}
102
104
  end
103
105
 
104
- it_processes_single_select_column 'count(age)', 'c0', :number, 3 do
106
+ it_processes_single_select_column 'count(age)', 'c0', :number, 3, 'count(age)' do
105
107
  3.times{|i| Person.make}
106
108
  end
107
109
 
108
- it_processes_single_select_column 'max(age)', 'c0', :number, 3 do
110
+ it_processes_single_select_column 'max(age)', 'c0', :number, 3, 'max(age)' do
109
111
  [1, 2, 3].each{|i| Person.make :age => i}
110
112
  end
111
113
 
112
- it_processes_single_select_column 'min(age)', 'c0', :number, 1 do
114
+ it_processes_single_select_column 'min(age)', 'c0', :number, 1, 'min(age)' do
113
115
  [1, 2, 3].each{|i| Person.make :age => i}
114
116
  end
115
117
 
116
- it_processes_single_select_column 'age where age > 2', 'age', :number, 3 do
118
+ it_processes_single_select_column 'age where age > 2', 'age', :number, 3, 'age' do
117
119
  [1, 2, 3].each{|i| Person.make :age => i}
118
120
  end
119
121
 
120
- it_processes_single_select_column 'age where age > 2 and age <= 3', 'age', :number, 3 do
122
+ it_processes_single_select_column 'age where age > 2 and age <= 3', 'age', :number, 3, 'age' do
121
123
  [1, 2, 3, 4, 5].each{|i| Person.make :age => i}
122
124
  end
123
125
 
124
- it_processes_single_select_column 'name where age is null', 'name', :string, 'b' do
126
+ it_processes_single_select_column 'name where age is null', 'name', :string, 'b', 'name' do
125
127
  Person.make :age => 1, :name => 'a'
126
128
  Person.make :age => nil, :name => 'b'
127
129
  end
128
130
 
129
- it_processes_single_select_column 'name where age is not null', 'name', :string, 'a' do
131
+ it_processes_single_select_column 'name where age is not null', 'name', :string, 'a', 'name' do
130
132
  Person.make :age => 1, :name => 'a'
131
133
  Person.make :age => nil, :name => 'b'
132
134
  end
@@ -162,23 +164,23 @@ describe Executor do
162
164
  table.rows[2].c[0].v.should == 1
163
165
  end
164
166
 
165
- it_processes_single_select_column 'age where age > 3 order by age limit 1', 'age', :number, 4 do
167
+ it_processes_single_select_column 'age where age > 3 order by age limit 1', 'age', :number, 4, 'age' do
166
168
  [1, 2, 3, 4, 5].each{|i| Person.make :age => i}
167
169
  end
168
170
 
169
- it_processes_single_select_column 'age where age > 3 order by age limit 1 offset 1', 'age', :number, 5 do
171
+ it_processes_single_select_column 'age where age > 3 order by age limit 1 offset 1', 'age', :number, 5, 'age' do
170
172
  [1, 2, 3, 4, 5].each{|i| Person.make :age => i}
171
173
  end
172
174
 
173
- it_processes_single_select_column 'city_name', 'city_name', :string, 'Buenos Aires' do
175
+ it_processes_single_select_column 'city_name', 'city_name', :string, 'Buenos Aires', 'city_name' do
174
176
  Person.make :city => City.make(:name => 'Buenos Aires')
175
177
  end
176
178
 
177
- it_processes_single_select_column 'city_country_name', 'city_country_name', :string, 'Argentina' do
179
+ it_processes_single_select_column 'city_country_name', 'city_country_name', :string, 'Argentina', 'city_country_name' do
178
180
  Person.make :city => City.make(:country => Country.make(:name => 'Argentina'))
179
181
  end
180
182
 
181
- it_processes_single_select_column 'city_country_name group by city_country_name', 'city_country_name', :string, 'Argentina' do
183
+ it_processes_single_select_column 'city_country_name group by city_country_name', 'city_country_name', :string, 'Argentina', 'city_country_name' do
182
184
  Person.make :city => City.make(:country => Country.make(:name => 'Argentina'))
183
185
  end
184
186
 
@@ -232,38 +234,46 @@ describe Executor do
232
234
 
233
235
  [['year', 2006], ['month', 5], ['day', 2],
234
236
  ['hour', 3], ['minute', 4], ['second', 9],
235
- ['quarter', 2], ['dayOfWeek', 3]].each do |str, val|
236
- it_processes_single_select_column "#{str}(created_at)", 'c0', :number, val do
237
+ ['dayOfWeek', 3]].each do |str, val|
238
+ it_processes_single_select_column "#{str}(created_at)", 'c0', :number, val, "#{str}(created_at)" do
237
239
  Person.make :created_at => Time.parse('2006-05-02 3:04:09')
238
240
  end
239
241
  end
240
242
 
241
- it_processes_single_select_column "dateDiff(date '2008-03-13', date '2008-03-10')", 'c0', :number, 3 do
242
- Person.make
243
- end
243
+ # it_processes_single_select_column "quarter(created_at)", 'c0', :number, 5 do
244
+ # Person.make :created_at => Time.parse('2006-05-02 3:04:09')
245
+ # end
244
246
 
245
- it_processes_single_select_column "now()", 'c0', :datetime, Time.now.utc.strftime("%Y-%m-%d %H:%M:%S") do
246
- Person.make :created_at => Time.parse('2006-05-02 3:04:09')
247
- end
247
+ it_processes_single_select_column "dateDiff(date '2008-03-13', date '2008-03-10')", 'c0', :number, 3, "dateDiff(date '2008-03-13', date '2008-03-10')"
248
+
249
+ # it_processes_single_select_column "now()", 'c0', :datetime, Time.now.utc.strftime("%Y-%m-%d %H:%M:%S"), "now()" do
250
+ # Person.make :created_at => Time.parse('2006-05-02 3:04:09')
251
+ # end
248
252
 
249
- it_processes_single_select_column "toDate('2008-03-13')", 'c0', :date, Date.parse('2008-03-13').to_s do
250
- Person.make
251
- end
253
+ it_processes_single_select_column "toDate('2008-03-13')", 'c0', :date, Date.parse('2008-03-13').to_s, "toDate('2008-03-13')"
252
254
 
253
- it_processes_single_select_column "toDate(created_at)", 'c0', :date, Date.parse('2008-03-13').to_s do
255
+ it_processes_single_select_column "toDate(created_at)", 'c0', :date, Date.parse('2008-03-13').to_s, "toDate(created_at)" do
254
256
  Person.make :created_at => Time.parse('2008-03-13 3:04:09')
255
257
  end
256
258
 
257
- it_processes_single_select_column "toDate(1234567890000)", 'c0', :date, Date.parse('2009-02-13').to_s do
258
- Person.make
259
- end
259
+ # it_processes_single_select_column "toDate(1234567890000)", 'c0', :date, Date.parse('2009-02-13').to_s, "toDate(1234567890000)"
260
260
 
261
- it_processes_single_select_column "upper(name)", 'c0', :string, 'FOO' do
261
+ it_processes_single_select_column "upper(name)", 'c0', :string, 'FOO', "upper(name)" do
262
262
  Person.make :name => 'foo'
263
263
  end
264
264
 
265
- it_processes_single_select_column "lower(name)", 'c0', :string, 'foo' do
265
+ it_processes_single_select_column "lower(name)", 'c0', :string, 'foo', "lower(name)" do
266
266
  Person.make :name => 'FOO'
267
267
  end
268
268
 
269
+ it_processes_single_select_column "name label name 'my name'", 'name', :string, 'foo', "my name" do
270
+ Person.make :name => 'foo'
271
+ end
272
+
273
+ it_processes_single_select_column "1 + 2 label 1 + 2 'my name'", 'c0', :number, 3, "my name"
274
+
275
+ it_processes_single_select_column "sum(age) label sum(age) 'my name'", 'c0', :number, 2, "my name" do
276
+ Person.make :age => 2
277
+ end
278
+
269
279
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rgviz-rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 9
9
- version: "0.9"
8
+ - 10
9
+ version: "0.10"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ary Borenszweig