A BLINK INTO THE “RAILS MAGIC”
The meta-programming in Rails / Rails as a code generator
The “classic” rails ingredients
MVCDRYCOC…
Not for today…
MVCDRYCOC…
Rails as a code generator
PART I :
100% Static
The directory structure
Models + Tests
Controllers / views + tests
Resources (the whole set)
Even with *implemented* views & controllers (a.k.a. “scaffolds”)
Static(*) API
Nothing special so far…
PART II :
100% On-the-fly
This is where the actual fun begins!
Some basic stuff: ORM
How does Rails map the attributes???
The truth is in the source…
A blink into the “magic”
if we haven’t already generated the “dynamic” methods
if we haven’t already generated the “dynamic” methods
• generate them
if we haven’t already generated the “dynamic” methods
• generate them
•try to call the method with the given name again!
for each column on the db table :
for each column on the db table :
• define a “read” method
for each column on the db table :
• define a “read” method (for example the Guest#name)
for each column on the db table :
• define a “read” method(for example the Guest#name)
• define a “write” method
for each column on the db table :
• define a “read” method(for example the Guest#name)
• define a “write” method(for example the Guest#name = …)
for each column on the db table :
• define a “read” method(for example the Guest#name)
• define a “write” method(for example the Guest#name = …)
• define a “question” method
for each column on the db table :
• define a “read” method(for example the Guest#name)
• define a “write” method(for example the Guest#name = …)
• define a “question” method(for example the Guest#name?)
PART III :
Magic happens Everywhere
On the routes / Controllers
+ 3 more by default
More info on “resources” : http://api.rubyonrails.org/classes/ActionController/Resources.html
+ 3 more by default
On the ORM / Models
Dynamic finders
Dynamic finders
As said this is just a “blink”
Look for more in :
• ActiveRecord associations• ActiveRecord named_scopes
• ActionController routes• ActionController rendering• ActionController format
• etc…
What do we get from this?
What do we get from this?
•Glue code
What do we get from this?
•Glue code•Domain-aware code
What do we get from this?
•Glue code•Domain-aware code
•Out of the box
What do we get from this?
•Glue code•Domain-aware code
•Out of the box• Every time
What do we get from this?
• Glue code• Domain-aware code
• Out of the box• Every time
• On every app
Compare it with Java or .Net “enterprise”
solutions…
Compare it with Java or .Net “enterprise”
solutions…
© Rubyrags.com
Implement your own!!!
Implement your own!!!
(Jim will give you a hint tomorrow at the Ruby
Meetup)
Thanks for your time
Thanks for your time
And welcome to the magic world of
Ruby and Ruby on Rails