rgviz-rails 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +243 -0
- data/lib/rgviz_rails/executor.rb +304 -0
- data/lib/rgviz_rails.rb +1 -0
- data/spec/blueprints.rb +25 -0
- data/spec/models/city.rb +4 -0
- data/spec/models/country.rb +3 -0
- data/spec/models/person.rb +3 -0
- data/spec/rgviz/executor_spec.rb +185 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +47 -0
- metadata +75 -0
data/README
ADDED
@@ -0,0 +1,243 @@
|
|
1
|
+
== Welcome to Rails
|
2
|
+
|
3
|
+
Rails is a web-application framework that includes everything needed to create
|
4
|
+
database-backed web applications according to the Model-View-Control pattern.
|
5
|
+
|
6
|
+
This pattern splits the view (also called the presentation) into "dumb" templates
|
7
|
+
that are primarily responsible for inserting pre-built data in between HTML tags.
|
8
|
+
The model contains the "smart" domain objects (such as Account, Product, Person,
|
9
|
+
Post) that holds all the business logic and knows how to persist themselves to
|
10
|
+
a database. The controller handles the incoming requests (such as Save New Account,
|
11
|
+
Update Product, Show Post) by manipulating the model and directing data to the view.
|
12
|
+
|
13
|
+
In Rails, the model is handled by what's called an object-relational mapping
|
14
|
+
layer entitled Active Record. This layer allows you to present the data from
|
15
|
+
database rows as objects and embellish these data objects with business logic
|
16
|
+
methods. You can read more about Active Record in
|
17
|
+
link:files/vendor/rails/activerecord/README.html.
|
18
|
+
|
19
|
+
The controller and view are handled by the Action Pack, which handles both
|
20
|
+
layers by its two parts: Action View and Action Controller. These two layers
|
21
|
+
are bundled in a single package due to their heavy interdependence. This is
|
22
|
+
unlike the relationship between the Active Record and Action Pack that is much
|
23
|
+
more separate. Each of these packages can be used independently outside of
|
24
|
+
Rails. You can read more about Action Pack in
|
25
|
+
link:files/vendor/rails/actionpack/README.html.
|
26
|
+
|
27
|
+
|
28
|
+
== Getting Started
|
29
|
+
|
30
|
+
1. At the command prompt, start a new Rails application using the <tt>rails</tt> command
|
31
|
+
and your application name. Ex: rails myapp
|
32
|
+
2. Change directory into myapp and start the web server: <tt>script/server</tt> (run with --help for options)
|
33
|
+
3. Go to http://localhost:3000/ and get "Welcome aboard: You're riding the Rails!"
|
34
|
+
4. Follow the guidelines to start developing your application
|
35
|
+
|
36
|
+
|
37
|
+
== Web Servers
|
38
|
+
|
39
|
+
By default, Rails will try to use Mongrel if it's are installed when started with script/server, otherwise Rails will use WEBrick, the webserver that ships with Ruby. But you can also use Rails
|
40
|
+
with a variety of other web servers.
|
41
|
+
|
42
|
+
Mongrel is a Ruby-based webserver with a C component (which requires compilation) that is
|
43
|
+
suitable for development and deployment of Rails applications. If you have Ruby Gems installed,
|
44
|
+
getting up and running with mongrel is as easy as: <tt>gem install mongrel</tt>.
|
45
|
+
More info at: http://mongrel.rubyforge.org
|
46
|
+
|
47
|
+
Say other Ruby web servers like Thin and Ebb or regular web servers like Apache or LiteSpeed or
|
48
|
+
Lighttpd or IIS. The Ruby web servers are run through Rack and the latter can either be setup to use
|
49
|
+
FCGI or proxy to a pack of Mongrels/Thin/Ebb servers.
|
50
|
+
|
51
|
+
== Apache .htaccess example for FCGI/CGI
|
52
|
+
|
53
|
+
# General Apache options
|
54
|
+
AddHandler fastcgi-script .fcgi
|
55
|
+
AddHandler cgi-script .cgi
|
56
|
+
Options +FollowSymLinks +ExecCGI
|
57
|
+
|
58
|
+
# If you don't want Rails to look in certain directories,
|
59
|
+
# use the following rewrite rules so that Apache won't rewrite certain requests
|
60
|
+
#
|
61
|
+
# Example:
|
62
|
+
# RewriteCond %{REQUEST_URI} ^/notrails.*
|
63
|
+
# RewriteRule .* - [L]
|
64
|
+
|
65
|
+
# Redirect all requests not available on the filesystem to Rails
|
66
|
+
# By default the cgi dispatcher is used which is very slow
|
67
|
+
#
|
68
|
+
# For better performance replace the dispatcher with the fastcgi one
|
69
|
+
#
|
70
|
+
# Example:
|
71
|
+
# RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
|
72
|
+
RewriteEngine On
|
73
|
+
|
74
|
+
# If your Rails application is accessed via an Alias directive,
|
75
|
+
# then you MUST also set the RewriteBase in this htaccess file.
|
76
|
+
#
|
77
|
+
# Example:
|
78
|
+
# Alias /myrailsapp /path/to/myrailsapp/public
|
79
|
+
# RewriteBase /myrailsapp
|
80
|
+
|
81
|
+
RewriteRule ^$ index.html [QSA]
|
82
|
+
RewriteRule ^([^.]+)$ $1.html [QSA]
|
83
|
+
RewriteCond %{REQUEST_FILENAME} !-f
|
84
|
+
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
|
85
|
+
|
86
|
+
# In case Rails experiences terminal errors
|
87
|
+
# Instead of displaying this message you can supply a file here which will be rendered instead
|
88
|
+
#
|
89
|
+
# Example:
|
90
|
+
# ErrorDocument 500 /500.html
|
91
|
+
|
92
|
+
ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
|
93
|
+
|
94
|
+
|
95
|
+
== Debugging Rails
|
96
|
+
|
97
|
+
Sometimes your application goes wrong. Fortunately there are a lot of tools that
|
98
|
+
will help you debug it and get it back on the rails.
|
99
|
+
|
100
|
+
First area to check is the application log files. Have "tail -f" commands running
|
101
|
+
on the server.log and development.log. Rails will automatically display debugging
|
102
|
+
and runtime information to these files. Debugging info will also be shown in the
|
103
|
+
browser on requests from 127.0.0.1.
|
104
|
+
|
105
|
+
You can also log your own messages directly into the log file from your code using
|
106
|
+
the Ruby logger class from inside your controllers. Example:
|
107
|
+
|
108
|
+
class WeblogController < ActionController::Base
|
109
|
+
def destroy
|
110
|
+
@weblog = Weblog.find(params[:id])
|
111
|
+
@weblog.destroy
|
112
|
+
logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
The result will be a message in your log file along the lines of:
|
117
|
+
|
118
|
+
Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1
|
119
|
+
|
120
|
+
More information on how to use the logger is at http://www.ruby-doc.org/core/
|
121
|
+
|
122
|
+
Also, Ruby documentation can be found at http://www.ruby-lang.org/ including:
|
123
|
+
|
124
|
+
* The Learning Ruby (Pickaxe) Book: http://www.ruby-doc.org/docs/ProgrammingRuby/
|
125
|
+
* Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
|
126
|
+
|
127
|
+
These two online (and free) books will bring you up to speed on the Ruby language
|
128
|
+
and also on programming in general.
|
129
|
+
|
130
|
+
|
131
|
+
== Debugger
|
132
|
+
|
133
|
+
Debugger support is available through the debugger command when you start your Mongrel or
|
134
|
+
Webrick server with --debugger. This means that you can break out of execution at any point
|
135
|
+
in the code, investigate and change the model, AND then resume execution!
|
136
|
+
You need to install ruby-debug to run the server in debugging mode. With gems, use 'gem install ruby-debug'
|
137
|
+
Example:
|
138
|
+
|
139
|
+
class WeblogController < ActionController::Base
|
140
|
+
def index
|
141
|
+
@posts = Post.find(:all)
|
142
|
+
debugger
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
So the controller will accept the action, run the first line, then present you
|
147
|
+
with a IRB prompt in the server window. Here you can do things like:
|
148
|
+
|
149
|
+
>> @posts.inspect
|
150
|
+
=> "[#<Post:0x14a6be8 @attributes={\"title\"=>nil, \"body\"=>nil, \"id\"=>\"1\"}>,
|
151
|
+
#<Post:0x14a6620 @attributes={\"title\"=>\"Rails you know!\", \"body\"=>\"Only ten..\", \"id\"=>\"2\"}>]"
|
152
|
+
>> @posts.first.title = "hello from a debugger"
|
153
|
+
=> "hello from a debugger"
|
154
|
+
|
155
|
+
...and even better is that you can examine how your runtime objects actually work:
|
156
|
+
|
157
|
+
>> f = @posts.first
|
158
|
+
=> #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
|
159
|
+
>> f.
|
160
|
+
Display all 152 possibilities? (y or n)
|
161
|
+
|
162
|
+
Finally, when you're ready to resume execution, you enter "cont"
|
163
|
+
|
164
|
+
|
165
|
+
== Console
|
166
|
+
|
167
|
+
You can interact with the domain model by starting the console through <tt>script/console</tt>.
|
168
|
+
Here you'll have all parts of the application configured, just like it is when the
|
169
|
+
application is running. You can inspect domain models, change values, and save to the
|
170
|
+
database. Starting the script without arguments will launch it in the development environment.
|
171
|
+
Passing an argument will specify a different environment, like <tt>script/console production</tt>.
|
172
|
+
|
173
|
+
To reload your controllers and models after launching the console run <tt>reload!</tt>
|
174
|
+
|
175
|
+
== dbconsole
|
176
|
+
|
177
|
+
You can go to the command line of your database directly through <tt>script/dbconsole</tt>.
|
178
|
+
You would be connected to the database with the credentials defined in database.yml.
|
179
|
+
Starting the script without arguments will connect you to the development database. Passing an
|
180
|
+
argument will connect you to a different database, like <tt>script/dbconsole production</tt>.
|
181
|
+
Currently works for mysql, postgresql and sqlite.
|
182
|
+
|
183
|
+
== Description of Contents
|
184
|
+
|
185
|
+
app
|
186
|
+
Holds all the code that's specific to this particular application.
|
187
|
+
|
188
|
+
app/controllers
|
189
|
+
Holds controllers that should be named like weblogs_controller.rb for
|
190
|
+
automated URL mapping. All controllers should descend from ApplicationController
|
191
|
+
which itself descends from ActionController::Base.
|
192
|
+
|
193
|
+
app/models
|
194
|
+
Holds models that should be named like post.rb.
|
195
|
+
Most models will descend from ActiveRecord::Base.
|
196
|
+
|
197
|
+
app/views
|
198
|
+
Holds the template files for the view that should be named like
|
199
|
+
weblogs/index.html.erb for the WeblogsController#index action. All views use eRuby
|
200
|
+
syntax.
|
201
|
+
|
202
|
+
app/views/layouts
|
203
|
+
Holds the template files for layouts to be used with views. This models the common
|
204
|
+
header/footer method of wrapping views. In your views, define a layout using the
|
205
|
+
<tt>layout :default</tt> and create a file named default.html.erb. Inside default.html.erb,
|
206
|
+
call <% yield %> to render the view using this layout.
|
207
|
+
|
208
|
+
app/helpers
|
209
|
+
Holds view helpers that should be named like weblogs_helper.rb. These are generated
|
210
|
+
for you automatically when using script/generate for controllers. Helpers can be used to
|
211
|
+
wrap functionality for your views into methods.
|
212
|
+
|
213
|
+
config
|
214
|
+
Configuration files for the Rails environment, the routing map, the database, and other dependencies.
|
215
|
+
|
216
|
+
db
|
217
|
+
Contains the database schema in schema.rb. db/migrate contains all
|
218
|
+
the sequence of Migrations for your schema.
|
219
|
+
|
220
|
+
doc
|
221
|
+
This directory is where your application documentation will be stored when generated
|
222
|
+
using <tt>rake doc:app</tt>
|
223
|
+
|
224
|
+
lib
|
225
|
+
Application specific libraries. Basically, any kind of custom code that doesn't
|
226
|
+
belong under controllers, models, or helpers. This directory is in the load path.
|
227
|
+
|
228
|
+
public
|
229
|
+
The directory available for the web server. Contains subdirectories for images, stylesheets,
|
230
|
+
and javascripts. Also contains the dispatchers and the default HTML files. This should be
|
231
|
+
set as the DOCUMENT_ROOT of your web server.
|
232
|
+
|
233
|
+
script
|
234
|
+
Helper scripts for automation and generation.
|
235
|
+
|
236
|
+
test
|
237
|
+
Unit and functional tests along with fixtures. When using the script/generate scripts, template
|
238
|
+
test files will be generated for you and placed in this directory.
|
239
|
+
|
240
|
+
vendor
|
241
|
+
External libraries that the application depends on. Also includes the plugins subdirectory.
|
242
|
+
If the app has frozen rails, those gems also go here, under vendor/rails/.
|
243
|
+
This directory is in the load path.
|
@@ -0,0 +1,304 @@
|
|
1
|
+
module Rgviz
|
2
|
+
class Executor
|
3
|
+
attr_reader :model_class
|
4
|
+
|
5
|
+
def initialize(model_class, query)
|
6
|
+
@model_class = model_class
|
7
|
+
@query = query
|
8
|
+
@joins = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def execute
|
12
|
+
@table = Table.new
|
13
|
+
|
14
|
+
generate_columns
|
15
|
+
generate_conditions
|
16
|
+
generate_group
|
17
|
+
generate_order
|
18
|
+
generate_rows
|
19
|
+
@table
|
20
|
+
end
|
21
|
+
|
22
|
+
def add_joins(joins)
|
23
|
+
map = @joins
|
24
|
+
joins.each do |join|
|
25
|
+
key = join.name
|
26
|
+
val = map[key]
|
27
|
+
map[key] = {} unless val
|
28
|
+
map = map[key]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def generate_columns
|
33
|
+
@selects = []
|
34
|
+
|
35
|
+
if @query.select && @query.select.columns.present?
|
36
|
+
# Select the specified columns
|
37
|
+
i = 0
|
38
|
+
@query.select.columns.each do |col|
|
39
|
+
@table.cols << (Column.new :id => column_id(col, i), :type => column_type(col))
|
40
|
+
@selects << "(#{column_select(col)}) as c#{i}"
|
41
|
+
i += 1
|
42
|
+
end
|
43
|
+
else
|
44
|
+
# Select all columns
|
45
|
+
i = 0
|
46
|
+
@model_class.send(:columns).each do |col|
|
47
|
+
@table.cols << (Column.new :id => col.name, :type => (rails_column_type col))
|
48
|
+
@selects << "(#{col.name}) as c#{i}"
|
49
|
+
i += 1
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def generate_conditions
|
55
|
+
@conditions = to_string @query.where, WhereVisitor if @query.where
|
56
|
+
end
|
57
|
+
|
58
|
+
def generate_group
|
59
|
+
@group = to_string @query.group_by, ColumnVisitor if @query.group_by
|
60
|
+
end
|
61
|
+
|
62
|
+
def generate_order
|
63
|
+
@order = to_string @query.order_by, OrderVisitor if @query.order_by
|
64
|
+
end
|
65
|
+
|
66
|
+
def generate_rows
|
67
|
+
results = @model_class.send :all,
|
68
|
+
:select => @selects.join(','),
|
69
|
+
:conditions => @conditions,
|
70
|
+
:group => @group,
|
71
|
+
:order => @order,
|
72
|
+
:limit => @query.limit,
|
73
|
+
:offset => @query.offset,
|
74
|
+
:joins => @joins
|
75
|
+
|
76
|
+
results.each do |result|
|
77
|
+
row = Row.new
|
78
|
+
@table.rows << row
|
79
|
+
|
80
|
+
i = 0
|
81
|
+
@table.cols.each do |col|
|
82
|
+
row.c << (Cell.new :v => column_value(col, result.send("c#{i}")))
|
83
|
+
i += 1
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def column_id(col, i)
|
89
|
+
case col
|
90
|
+
when IdColumn
|
91
|
+
col.name
|
92
|
+
else
|
93
|
+
"c#{i}"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def column_type(col)
|
98
|
+
case col
|
99
|
+
when IdColumn
|
100
|
+
klass, rails_col, joins = Rgviz::find_rails_col @model_class, col.name
|
101
|
+
raise "Unknown column #{col}" unless rails_col
|
102
|
+
rails_column_type rails_col
|
103
|
+
when NumberColumn
|
104
|
+
:number
|
105
|
+
when StringColumn
|
106
|
+
:string
|
107
|
+
when BooleanColumn
|
108
|
+
:boolean
|
109
|
+
when DateColumn
|
110
|
+
:date
|
111
|
+
when DateTimeColumn
|
112
|
+
:datetime
|
113
|
+
when TimeOfDayColumn
|
114
|
+
:timeofday
|
115
|
+
when ScalarFunctionColumn
|
116
|
+
case col.function
|
117
|
+
when ScalarFunctionColumn::Now
|
118
|
+
:datetime
|
119
|
+
when ScalarFunctionColumn::ToDate
|
120
|
+
:date
|
121
|
+
when ScalarFunctionColumn::Upper, ScalarFunctionColumn::Lower
|
122
|
+
:string
|
123
|
+
else
|
124
|
+
:number
|
125
|
+
end
|
126
|
+
when AggregateColumn
|
127
|
+
:number
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def column_select(col)
|
132
|
+
to_string col, ColumnVisitor
|
133
|
+
end
|
134
|
+
|
135
|
+
def column_value(col, value)
|
136
|
+
case col.type
|
137
|
+
when :number
|
138
|
+
i = value.to_i
|
139
|
+
f = value.to_f
|
140
|
+
i == f ? i : f
|
141
|
+
when :boolean
|
142
|
+
value == '1' ? true : false
|
143
|
+
else
|
144
|
+
value.to_s
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def to_string(node, visitor_class)
|
149
|
+
visitor = visitor_class.new self
|
150
|
+
node.accept visitor
|
151
|
+
visitor.string
|
152
|
+
end
|
153
|
+
|
154
|
+
def rails_column_type(col)
|
155
|
+
case col.type
|
156
|
+
when :integer
|
157
|
+
:number
|
158
|
+
else
|
159
|
+
col.type
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
class ColumnVisitor < Rgviz::Visitor
|
165
|
+
attr_reader :string
|
166
|
+
|
167
|
+
def initialize(executor)
|
168
|
+
@string = ''
|
169
|
+
@executor = executor
|
170
|
+
end
|
171
|
+
|
172
|
+
def visitIdColumn(node)
|
173
|
+
klass, rails_col, joins = Rgviz::find_rails_col @executor.model_class, node.name
|
174
|
+
raise "Unknown column '#{node.name}'" unless rails_col
|
175
|
+
@string += ActiveRecord::Base.connection.quote_column_name(klass.table_name)
|
176
|
+
@string += '.'
|
177
|
+
@string += ActiveRecord::Base.connection.quote_column_name(rails_col.name)
|
178
|
+
|
179
|
+
@executor.add_joins joins
|
180
|
+
end
|
181
|
+
|
182
|
+
def visitNumberColumn(node)
|
183
|
+
@string += node.value.to_s
|
184
|
+
end
|
185
|
+
|
186
|
+
def visitStringColumn(node)
|
187
|
+
@string += escaped_string(node.value)
|
188
|
+
end
|
189
|
+
|
190
|
+
def visitBooleanColumn(node)
|
191
|
+
@string += node.value ? '1' : '0'
|
192
|
+
end
|
193
|
+
|
194
|
+
def visitDateColumn(node)
|
195
|
+
@string += escaped_string(node.value.to_s)
|
196
|
+
end
|
197
|
+
|
198
|
+
def visitDateTimeColumn(node)
|
199
|
+
@string += escaped_string(node.value.strftime "%Y-%m-%d %H:%M:%S")
|
200
|
+
end
|
201
|
+
|
202
|
+
def visitTimeOfDayColumn(node)
|
203
|
+
@string += escaped_string(node.value.strftime "%H:%M:%S")
|
204
|
+
end
|
205
|
+
|
206
|
+
def visitScalarFunctionColumn(node)
|
207
|
+
case node.function
|
208
|
+
when ScalarFunctionColumn::Sum, ScalarFunctionColumn::Difference,
|
209
|
+
ScalarFunctionColumn::Product, ScalarFunctionColumn::Quotient
|
210
|
+
node.arguments[0].accept self
|
211
|
+
@string += node.function.to_s
|
212
|
+
node.arguments[1].accept self
|
213
|
+
end
|
214
|
+
false
|
215
|
+
end
|
216
|
+
|
217
|
+
def visitAggregateColumn(node)
|
218
|
+
@string += node.function.to_s
|
219
|
+
@string += '('
|
220
|
+
node.argument.accept self
|
221
|
+
@string += ')'
|
222
|
+
false
|
223
|
+
end
|
224
|
+
|
225
|
+
def visitLabel(node)
|
226
|
+
false
|
227
|
+
end
|
228
|
+
|
229
|
+
def visitFormat(node)
|
230
|
+
false
|
231
|
+
end
|
232
|
+
|
233
|
+
def visitOption(node)
|
234
|
+
false
|
235
|
+
end
|
236
|
+
|
237
|
+
def escaped_string(str)
|
238
|
+
str = str.gsub("'", "''")
|
239
|
+
"'#{str}'"
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
class WhereVisitor < ColumnVisitor
|
244
|
+
def visitBinaryExpression(node)
|
245
|
+
node.left.accept self
|
246
|
+
@string += " #{node.operator} "
|
247
|
+
node.right.accept self
|
248
|
+
false
|
249
|
+
end
|
250
|
+
|
251
|
+
def visitUnaryExpression(node)
|
252
|
+
case node.operator
|
253
|
+
when UnaryExpression::Not
|
254
|
+
@string += 'not ('
|
255
|
+
node.operand.accept self
|
256
|
+
@string += ')'
|
257
|
+
when UnaryExpression::IsNull
|
258
|
+
node.operand.accept self
|
259
|
+
@string += 'is null'
|
260
|
+
when UnaryExpression::IsNotNull
|
261
|
+
node.operand.accept self
|
262
|
+
@string += 'is not null'
|
263
|
+
end
|
264
|
+
false
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
class OrderVisitor < ColumnVisitor
|
269
|
+
def visitOrderBy(node)
|
270
|
+
i = 0
|
271
|
+
node.sorts.each do |sort|
|
272
|
+
@string += ',' if i > 0
|
273
|
+
sort.accept self
|
274
|
+
end
|
275
|
+
false
|
276
|
+
end
|
277
|
+
|
278
|
+
def visitSort(node)
|
279
|
+
node.column.accept self
|
280
|
+
@string += node.order == Sort::Asc ? ' asc' : ' desc'
|
281
|
+
false
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
def self.find_rails_col(klass, name)
|
286
|
+
joins = []
|
287
|
+
|
288
|
+
while true
|
289
|
+
col = klass.send(:columns).select{|x| x.name == name}.first
|
290
|
+
return [klass, col, joins] if col
|
291
|
+
|
292
|
+
idx = name.index '_'
|
293
|
+
return nil if not idx
|
294
|
+
|
295
|
+
before = name[0 ... idx]
|
296
|
+
name = name[idx + 1 .. -1]
|
297
|
+
|
298
|
+
assoc = klass.send :reflect_on_association, before.to_sym
|
299
|
+
raise "Unknown association #{before}" unless assoc
|
300
|
+
klass = assoc.klass
|
301
|
+
joins << assoc
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
data/lib/rgviz_rails.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rgviz_rails/executor.rb'
|
data/spec/blueprints.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'machinist/active_record'
|
2
|
+
require 'sham'
|
3
|
+
require 'faker'
|
4
|
+
|
5
|
+
Sham.define do
|
6
|
+
name { Faker::Name.name }
|
7
|
+
number(:unique => false) { rand(100) + 1 }
|
8
|
+
date { Date.parse("#{rand(40) + 1970}-#{rand(12) + 1}-#{rand(28) + 1}") }
|
9
|
+
end
|
10
|
+
|
11
|
+
City.blueprint do
|
12
|
+
name
|
13
|
+
country
|
14
|
+
end
|
15
|
+
|
16
|
+
Country.blueprint do
|
17
|
+
name
|
18
|
+
end
|
19
|
+
|
20
|
+
Person.blueprint do
|
21
|
+
name
|
22
|
+
age { Sham::number }
|
23
|
+
birthday { Sham::date }
|
24
|
+
city
|
25
|
+
end
|
data/spec/models/city.rb
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
require 'rgviz'
|
3
|
+
|
4
|
+
include Rgviz
|
5
|
+
|
6
|
+
describe Executor do
|
7
|
+
before :each do
|
8
|
+
[Person, City, Country].each &:delete_all
|
9
|
+
end
|
10
|
+
|
11
|
+
def parse(string)
|
12
|
+
Parser.new(string).parse
|
13
|
+
end
|
14
|
+
|
15
|
+
def exec(query)
|
16
|
+
exec = Executor.new Person, (parse query)
|
17
|
+
exec.execute
|
18
|
+
end
|
19
|
+
|
20
|
+
def format_datetime(date)
|
21
|
+
date.strftime "%Y-%m-%d %H:%M:%S"
|
22
|
+
end
|
23
|
+
|
24
|
+
def format_date(date)
|
25
|
+
date.strftime "%Y-%m-%d"
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.it_processes_single_select_column(query, id, type, value)
|
29
|
+
it "processes select #{query}" do
|
30
|
+
if block_given?
|
31
|
+
yield
|
32
|
+
else
|
33
|
+
Person.make
|
34
|
+
end
|
35
|
+
|
36
|
+
table = exec "select #{query}"
|
37
|
+
table.cols.length.should == 1
|
38
|
+
|
39
|
+
table.cols[0].id.should == id
|
40
|
+
table.cols[0].type.should == type
|
41
|
+
|
42
|
+
table.rows.length.should == 1
|
43
|
+
table.rows[0].c.length.should == 1
|
44
|
+
|
45
|
+
table.rows[0].c[0].v.should == value
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
it "processes select *" do
|
50
|
+
p = Person.make
|
51
|
+
|
52
|
+
table = exec 'select *'
|
53
|
+
table.cols.length.should == 7
|
54
|
+
|
55
|
+
i = 0
|
56
|
+
[['id', :number], ['name', :string], ['age', :number], ['birthday', :date],
|
57
|
+
['created_at', :datetime], ['updated_at', :datetime],
|
58
|
+
['city_id', :number]].each do |id, type|
|
59
|
+
table.cols[i].id.should == id
|
60
|
+
table.cols[i].type.should == type
|
61
|
+
i += 1
|
62
|
+
end
|
63
|
+
|
64
|
+
table.rows.length.should == 1
|
65
|
+
table.rows[0].c.length.should == 7
|
66
|
+
|
67
|
+
i = 0
|
68
|
+
[p.id, p.name, p.age, format_date(p.birthday),
|
69
|
+
format_datetime(p.created_at), format_datetime(p.updated_at), p.city.id].each do |val|
|
70
|
+
table.rows[0].c[i].v.should == val
|
71
|
+
i += 1
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
it_processes_single_select_column 'name', 'name', :string, 'foo' do
|
76
|
+
Person.make :name => 'foo'
|
77
|
+
end
|
78
|
+
|
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
|
93
|
+
Person.make :age => 20
|
94
|
+
end
|
95
|
+
|
96
|
+
it_processes_single_select_column 'sum(age)', 'c0', :number, 6 do
|
97
|
+
[1, 2, 3].each{|i| Person.make :age => i}
|
98
|
+
end
|
99
|
+
|
100
|
+
it_processes_single_select_column 'avg(age)', 'c0', :number, 30 do
|
101
|
+
[10, 20, 60].each{|i| Person.make :age => i}
|
102
|
+
end
|
103
|
+
|
104
|
+
it_processes_single_select_column 'count(age)', 'c0', :number, 3 do
|
105
|
+
3.times{|i| Person.make}
|
106
|
+
end
|
107
|
+
|
108
|
+
it_processes_single_select_column 'max(age)', 'c0', :number, 3 do
|
109
|
+
[1, 2, 3].each{|i| Person.make :age => i}
|
110
|
+
end
|
111
|
+
|
112
|
+
it_processes_single_select_column 'min(age)', 'c0', :number, 1 do
|
113
|
+
[1, 2, 3].each{|i| Person.make :age => i}
|
114
|
+
end
|
115
|
+
|
116
|
+
it_processes_single_select_column 'age where age > 2', 'age', :number, 3 do
|
117
|
+
[1, 2, 3].each{|i| Person.make :age => i}
|
118
|
+
end
|
119
|
+
|
120
|
+
it_processes_single_select_column 'age where age > 2 and age <= 3', 'age', :number, 3 do
|
121
|
+
[1, 2, 3, 4, 5].each{|i| Person.make :age => i}
|
122
|
+
end
|
123
|
+
|
124
|
+
it_processes_single_select_column 'name where age is null', 'name', :string, 'b' do
|
125
|
+
Person.make :age => 1, :name => 'a'
|
126
|
+
Person.make :age => nil, :name => 'b'
|
127
|
+
end
|
128
|
+
|
129
|
+
it_processes_single_select_column 'name where age is not null', 'name', :string, 'a' do
|
130
|
+
Person.make :age => 1, :name => 'a'
|
131
|
+
Person.make :age => nil, :name => 'b'
|
132
|
+
end
|
133
|
+
|
134
|
+
it "processes group by" do
|
135
|
+
Person.make :name => 'one', :age => 1
|
136
|
+
Person.make :name => 'one', :age => 2
|
137
|
+
Person.make :name => 'two', :age => 3
|
138
|
+
Person.make :name => 'two', :age => 4
|
139
|
+
|
140
|
+
table = exec 'select max(age) group by name'
|
141
|
+
|
142
|
+
table.rows.length.should == 2
|
143
|
+
table.rows[0].c.length.should == 1
|
144
|
+
table.rows[0].c[0].v.should == 2
|
145
|
+
table.rows[1].c.length.should == 1
|
146
|
+
table.rows[1].c[0].v.should == 4
|
147
|
+
end
|
148
|
+
|
149
|
+
it "processes order by" do
|
150
|
+
Person.make :age => 1
|
151
|
+
Person.make :age => 3
|
152
|
+
Person.make :age => 2
|
153
|
+
|
154
|
+
table = exec 'select age order by age desc'
|
155
|
+
|
156
|
+
table.rows.length.should == 3
|
157
|
+
table.rows[0].c.length.should == 1
|
158
|
+
table.rows[0].c[0].v.should == 3
|
159
|
+
table.rows[1].c.length.should == 1
|
160
|
+
table.rows[1].c[0].v.should == 2
|
161
|
+
table.rows[2].c.length.should == 1
|
162
|
+
table.rows[2].c[0].v.should == 1
|
163
|
+
end
|
164
|
+
|
165
|
+
it_processes_single_select_column 'age where age > 3 order by age limit 1', 'age', :number, 4 do
|
166
|
+
[1, 2, 3, 4, 5].each{|i| Person.make :age => i}
|
167
|
+
end
|
168
|
+
|
169
|
+
it_processes_single_select_column 'age where age > 3 order by age limit 1 offset 1', 'age', :number, 5 do
|
170
|
+
[1, 2, 3, 4, 5].each{|i| Person.make :age => i}
|
171
|
+
end
|
172
|
+
|
173
|
+
it_processes_single_select_column 'city_name', 'city_name', :string, 'Buenos Aires' do
|
174
|
+
Person.make :city => City.make(:name => 'Buenos Aires')
|
175
|
+
end
|
176
|
+
|
177
|
+
it_processes_single_select_column 'city_country_name', 'city_country_name', :string, 'Argentina' do
|
178
|
+
Person.make :city => City.make(:country => Country.make(:name => 'Argentina'))
|
179
|
+
end
|
180
|
+
|
181
|
+
it_processes_single_select_column 'city_country_name group by city_country_name', 'city_country_name', :string, 'Argentina' do
|
182
|
+
Person.make :city => City.make(:country => Country.make(:name => 'Argentina'))
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'spec'
|
5
|
+
require 'logger'
|
6
|
+
|
7
|
+
require 'active_record'
|
8
|
+
|
9
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
10
|
+
|
11
|
+
ActiveRecord::Schema.define do
|
12
|
+
create_table "cities", :force => true do |t|
|
13
|
+
t.string "name"
|
14
|
+
t.datetime "created_at"
|
15
|
+
t.datetime "updated_at"
|
16
|
+
t.integer "country_id"
|
17
|
+
end
|
18
|
+
|
19
|
+
create_table "countries", :force => true do |t|
|
20
|
+
t.string "name"
|
21
|
+
t.datetime "created_at"
|
22
|
+
t.datetime "updated_at"
|
23
|
+
end
|
24
|
+
|
25
|
+
create_table "people", :force => true do |t|
|
26
|
+
t.string "name"
|
27
|
+
t.integer "age"
|
28
|
+
t.date "birthday"
|
29
|
+
t.datetime "created_at"
|
30
|
+
t.datetime "updated_at"
|
31
|
+
t.integer "city_id"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
require File.dirname(__FILE__) + '/models/person'
|
36
|
+
require File.dirname(__FILE__) + '/models/city'
|
37
|
+
require File.dirname(__FILE__) + '/models/country'
|
38
|
+
|
39
|
+
require File.dirname(__FILE__) + '/blueprints'
|
40
|
+
|
41
|
+
require 'rgviz'
|
42
|
+
require 'rgviz_rails'
|
43
|
+
|
44
|
+
RAILS_ENV = 'test'
|
45
|
+
|
46
|
+
# Add this directory so the ActiveSupport autoloading works
|
47
|
+
ActiveSupport::Dependencies.load_paths << File.dirname(__FILE__)
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rgviz-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Ary Borenszweig
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-16 00:00:00 +07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description:
|
22
|
+
email: aborenszweig@manas.com.ar
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README
|
29
|
+
files:
|
30
|
+
- lib/rgviz_rails.rb
|
31
|
+
- lib/rgviz_rails/executor.rb
|
32
|
+
- spec/blueprints.rb
|
33
|
+
- spec/spec.opts
|
34
|
+
- spec/spec_helper.rb
|
35
|
+
- spec/models/city.rb
|
36
|
+
- spec/models/country.rb
|
37
|
+
- spec/models/person.rb
|
38
|
+
- spec/rgviz/executor_spec.rb
|
39
|
+
- README
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://code.google.com/p/rgviz-rails
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
hash: 3
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.7
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: rgviz for rails
|
74
|
+
test_files: []
|
75
|
+
|