Skip to content

Instantly share code, notes, and snippets.

@xdite
Created July 14, 2012 05:11
Show Gist options
  • Save xdite/3109315 to your computer and use it in GitHub Desktop.
Save xdite/3109315 to your computer and use it in GitHub Desktop.
Ten (42) Things You Didn't Know Rails Could Do

1. Get You a Hug, Every Friday

FridayHug.com http://fridayhug.com

2. Run From a Single File

The Smallest Rails App http://thesmallestrailsapp.com

%w(action_controller/railtie coderay).each &method(:require)

run TheSmallestRailsApp ||= Class.new(Rails::Application) {
  config.secret_token = routes.append { root to: 'hello#world' }.inspect
  initialize!
}

class HelloController < ActionController::Base
  def world
    render inline: %Q{
      <!DOCTYPE html>
      <title>The Smallest Rails App</title>
      <h3>I am the smallest rails app!</h3>
      <p>Here is my source code:</p>
      #{CodeRay.scan_file(__FILE__, :ruby).div(line_numbers: :table)}
      <a href="https://github.com/artemave/thesmallestrailsapp.com">Make me smaller</a>
    }
  end
end

3. Remind You of Things

class UsersController < ApplicationController
  # TODO: Make it possible to create new users.
end
class User < ActiveRecord::Base
  # FIXME: Should token really be accessible?
  attr_accessible :bio, :email, :name, token
end  
<% OPTIMIZE: Paginate this listing. %>
<%= render Article.all %>

rake notes

$rake notes
app/controllers/users_controller.rb
  * [ 2][TODO] Make it possible to create new users.
  
app/modles/user.rb:
  * [ 2][FIXME] Should token really be accessible?
  
app/views/articles/index.htmk.erb:
  * [ 2][OPTIMIZE] Paginate this listing. 

rake notes:todo

$rake notes:todo
app/controllers/users_controller.rb
  * [ 2] Make it possible to create new users.

rake notes:fixme

$rake notes:fixme
app/controllers/users_controller.rb
  * [ 2] Should token really be accessible?

rake notes:custom ANNOTATION=JEG2

class Article M ActiveRecord::Base
  belongs_to :user
  attr_accessible :body, :subject
  # JEG2: Add that code from your blog here.
end
$ rake notes:custom ANNOTATION=JEG2
app/modles/article.rb
  * [ 4] Add that code from your blog here.

TextMate Bundles

  • Bundles > TODO > Show TODO List

4. Sandbox Your Console

rails r

$ rails r 'p [Article, Comment, User].map(&:count)'
[0,0,0]

rails c --sanbox

rolls back changes

$ rails c --sandbox
Loading development environment in sandbox (Rails 3.2.3)
Any modifications you make will be rolled back on exit
>> jeg2 = User.create!(name: "Jame Edward Gray II")
=> #<User id: 1, name: "Jame Edward Gray II", …>
>> article = Article.new(subject: "First Post").tap { |a| a.user = jeg2; a.save! }
=> #<Article id: 1, user_id: 1, ubject: "First Post", …>
>> Comment.new(body: "I need to add this.").tap { |c| c.user, c.article = jeg2, article' c.save! }
=> #<Comment id: 1, user_id: 1, article_id: 1, body: "I need to add this.", …>
>> [Article, Comment, User].map(&:count)
=> [1, 1, 1]
>> exit
$ rails r 'p [Article, Comment, User].map(&:count)'
[0,0,0]

5. Run Helper Methods in the Console

$ rails c
Loading development environment ( Rails 3.2.3)
>> helper.number_to_currency(100)
>> "$100.00"
>> helper.time_ago_in_words(3.days.ago)
=> "3 days"

6. Use Non-WEBriock Servers in Development

source 'https://rubygems.org'
# …
group :developemnt do
  gem "thin"
end
$ rails s thin
=> Booting Thin
=> Rails 3.2.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
>> Thin web server (v1.3.1 codename Triple Espresso)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop

7. Allow You to Tap into its Configuration

  • require Railtie, with config.custom in Railtie
# lib/custom/railtie.rb
module Custom
  class Railtie < Rails::Railtie
    config.custom = ActiveSupport::OrderedOptions.new
  end
end
  • You can configure plugins the same way you configure Rails
# config/application.rb
# …
require_relavtive "../lib/custom/railtie"
module Blog
  class Application < Rails::Application
    # …
    config.custom.setting = 42
  end
end

8. Keep You Entertained

Ruby Dramas http://rubydramas.com

9. Understand Shorthand Migrations

$ rails g resources user name:string email:string token:string bio:text
  • Default is string, can add limits
$ rails g resources user name email token:string{6} bio:text
class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email
      t.string :token, :limit => 6
      t.text :bio
      
      t.timestamps
    end
  end
end 

10. Add Indexes to Migrations

$ rails g resource user name:index email:uniq token:string{6} bio:text
class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email
      t.string :token, :limit => 6
      t.text :bio
      
      t.timestamps
    end
    add_index :users, :name
    add_index :users, :email, :unique => true
  end
end 
@PWSdelta
Copy link

Fucking. EPIC!

@Sparkboxx
Copy link

Very cool. Thanks James!

@kruszczynski
Copy link

So useful! Much nice!

@Lackoftactics
Copy link

That's great!

@veekram
Copy link

veekram commented May 18, 2015

Golden notes !

@KingWillonius
Copy link

sweet!!

@vemarav
Copy link

vemarav commented Sep 3, 2018

Thanks @xdite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment