Getting Rid of the Smoke and Mirrors in Rails

cave man lawyer

Rails is crazy powerful. There is so much that can be done with so little effort due to the incredible library of methods and commands built in to this framework. That “magic” frightens and confuses me. I don’t like typing in a large command having it do it’s magic with out knowing at least part of what’s happening. Rails is not magic. It is fantastic. Here’s why:

Rails on Github

Wouldn’t it be great if magicians immediately explained how they did the trick? That’s what Rails gives us! What does “form_for” actually do when you call it? What does Rails actually do when I type in “rails generate model test”? It’s all there! It’s beautiful and understandable!

form_for
rails generate model

HOLY WOW! That’s some complex code, but it is readable. Take a look at this code:

model.rb

<% module_namespacing do -%>
class <%= class_name %> < <%= parent_class_name.classify %>
<% attributes.select(&:reference?).each do |attribute| -%>
  belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %><%= ', required: true' if attribute.required? %>
<% end -%>
<% attributes.select(&:token?).each do |attribute| -%>
  has_secure_token<% if attribute.name != "token" %> :<%= attribute.name %><% end %>
<% end -%>
<% if attributes.any?(&:password_digest?) -%>
  has_secure_password
<% end -%>
end
<% end -%>

When you call “rails generate model test name:string story:text age:integer” it uses that template to make this model:
test.rb

class Test < ActiveRecord::Base
end

To generate the migration, it uses this template:
create_table_migration.rb

class <%= migration_class_name %> < ActiveRecord::Migration
  def change
    create_table :<%= table_name %> do |t|
<% attributes.each do |attribute| -%>
<% if attribute.password_digest? -%>
      t.string :password_digest<%= attribute.inject_options %>
<% elsif attribute.token? -%>
      t.string :<%= attribute.name %><%= attribute.inject_options %>
<% else -%>
      t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
<% end -%>
<% end -%>
<% if options[:timestamps] %>
      t.timestamps
<% end -%>
    end
<% attributes.select(&:token?).each do |attribute| -%>
    add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>, unique: true
<% end -%>
<% attributes_with_index.each do |attribute| -%>
    add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
<% end -%>
  end
end

And creates this migration:
20150309205056_create_tests.rb

class CreateTests < ActiveRecord::Migration
  def change
    create_table :tests do |t|
      t.string :name
      t.text :story
      t.integer :age

      t.timestamps null: false
    end
  end
end

Isn’t that neat! The magic is slowly fading. Being a huge nerd, my favorite show is Star Trek: the Next Generation. In season 3 episode 4, Picard saves the day by explaining his advanced technology to someone from a bronze era civilization. I believe it’s particularly relevant here:

PICARD (cont’d)
Now suppose one of your cave-dwelling
ancestors could see you
as you are today. What would she
think?

NURIA
I don’t know.

PICARD
Put yourself in her place. You
have a power she lacks. You can
kill a hornbuck from a great
distance.

NURIA
Only because I have a bow.

PICARD
She has never seen a bow — it
doesn’t yet exist in her world.
To you, it’s a simple tool. To
her, it’s magic.

NURIA
(musing)
I suppose she might think so.

PICARD
And how would she react to you?

NURIA
I think… she would fear me.

picard approves
Thanks,
superandrew

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s