77
A BLINK INTO THE “RAILS MAGIC” The meta-programming in Rails / Rails as a code generator

A Blink Into The Rails Magic

Embed Size (px)

DESCRIPTION

My presentation at Athens Digital Week '09

Citation preview

Page 1: A Blink Into The Rails Magic

A BLINK INTO THE “RAILS MAGIC”

The meta-programming in Rails / Rails as a code generator

Page 2: A Blink Into The Rails Magic

Some rights reservedNikos Dimitrakopoulos, 2009

http://blog.nikosd.com

Page 3: A Blink Into The Rails Magic

The “classic” rails ingredients

MVCDRYCOC…

Page 4: A Blink Into The Rails Magic

Not for today…

MVCDRYCOC…

Page 5: A Blink Into The Rails Magic

Rails as a code generator

Page 6: A Blink Into The Rails Magic

PART I :

100% Static

Page 7: A Blink Into The Rails Magic

The directory structure

Page 8: A Blink Into The Rails Magic

Models + Tests

Page 9: A Blink Into The Rails Magic

Controllers / views + tests

Page 10: A Blink Into The Rails Magic

Resources (the whole set)

Page 11: A Blink Into The Rails Magic

Even with *implemented* views & controllers (a.k.a. “scaffolds”)

Page 12: A Blink Into The Rails Magic
Page 13: A Blink Into The Rails Magic
Page 14: A Blink Into The Rails Magic
Page 15: A Blink Into The Rails Magic
Page 16: A Blink Into The Rails Magic
Page 17: A Blink Into The Rails Magic

Static(*) API

Page 18: A Blink Into The Rails Magic
Page 19: A Blink Into The Rails Magic
Page 20: A Blink Into The Rails Magic

Nothing special so far…

Page 21: A Blink Into The Rails Magic

PART II :

100% On-the-fly

Page 22: A Blink Into The Rails Magic

This is where the actual fun begins!

Page 23: A Blink Into The Rails Magic

Some basic stuff: ORM

Page 24: A Blink Into The Rails Magic
Page 25: A Blink Into The Rails Magic
Page 26: A Blink Into The Rails Magic
Page 27: A Blink Into The Rails Magic

How does Rails map the attributes???

Page 28: A Blink Into The Rails Magic
Page 29: A Blink Into The Rails Magic
Page 30: A Blink Into The Rails Magic

The truth is in the source…

Page 31: A Blink Into The Rails Magic
Page 32: A Blink Into The Rails Magic
Page 33: A Blink Into The Rails Magic
Page 34: A Blink Into The Rails Magic
Page 35: A Blink Into The Rails Magic
Page 36: A Blink Into The Rails Magic

A blink into the “magic”

Page 37: A Blink Into The Rails Magic
Page 38: A Blink Into The Rails Magic

if we haven’t already generated the “dynamic” methods

Page 39: A Blink Into The Rails Magic

if we haven’t already generated the “dynamic” methods

• generate them

Page 40: A Blink Into The Rails Magic

if we haven’t already generated the “dynamic” methods

• generate them

•try to call the method with the given name again!

Page 41: A Blink Into The Rails Magic
Page 42: A Blink Into The Rails Magic
Page 43: A Blink Into The Rails Magic

for each column on the db table :

Page 44: A Blink Into The Rails Magic

for each column on the db table :

• define a “read” method

Page 45: A Blink Into The Rails Magic

for each column on the db table :

• define a “read” method (for example the Guest#name)

Page 46: A Blink Into The Rails Magic

for each column on the db table :

• define a “read” method(for example the Guest#name)

• define a “write” method

Page 47: A Blink Into The Rails Magic

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 = …)

Page 48: A Blink Into The Rails Magic

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

Page 49: A Blink Into The Rails Magic

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?)

Page 50: A Blink Into The Rails Magic
Page 51: A Blink Into The Rails Magic

PART III :

Magic happens Everywhere

Page 52: A Blink Into The Rails Magic

On the routes / Controllers

Page 53: A Blink Into The Rails Magic
Page 54: A Blink Into The Rails Magic
Page 55: A Blink Into The Rails Magic
Page 56: A Blink Into The Rails Magic
Page 57: A Blink Into The Rails Magic
Page 58: A Blink Into The Rails Magic
Page 59: A Blink Into The Rails Magic

+ 3 more by default

Page 60: A Blink Into The Rails Magic

More info on “resources” : http://api.rubyonrails.org/classes/ActionController/Resources.html

+ 3 more by default

Page 61: A Blink Into The Rails Magic

On the ORM / Models

Page 62: A Blink Into The Rails Magic

Dynamic finders

Page 63: A Blink Into The Rails Magic

Dynamic finders

Page 64: A Blink Into The Rails Magic

As said this is just a “blink”

Page 65: A Blink Into The Rails Magic

Look for more in :

• ActiveRecord associations• ActiveRecord named_scopes

• ActionController routes• ActionController rendering• ActionController format

• etc…

Page 66: A Blink Into The Rails Magic

What do we get from this?

Page 67: A Blink Into The Rails Magic

What do we get from this?

•Glue code

Page 68: A Blink Into The Rails Magic

What do we get from this?

•Glue code•Domain-aware code

Page 69: A Blink Into The Rails Magic

What do we get from this?

•Glue code•Domain-aware code

•Out of the box

Page 70: A Blink Into The Rails Magic

What do we get from this?

•Glue code•Domain-aware code

•Out of the box• Every time

Page 71: A Blink Into The Rails Magic

What do we get from this?

• Glue code• Domain-aware code

• Out of the box• Every time

• On every app

Page 72: A Blink Into The Rails Magic

Compare it with Java or .Net “enterprise”

solutions…

Page 73: A Blink Into The Rails Magic

Compare it with Java or .Net “enterprise”

solutions…

© Rubyrags.com

Page 74: A Blink Into The Rails Magic

Implement your own!!!

Page 75: A Blink Into The Rails Magic

Implement your own!!!

(Jim will give you a hint tomorrow at the Ruby

Meetup)

Page 76: A Blink Into The Rails Magic

Thanks for your time

Page 77: A Blink Into The Rails Magic

Thanks for your time

And welcome to the magic world of

Ruby and Ruby on Rails