FridayHug.com http://fridayhug.com
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
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
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
app/controllers/users_controller.rb
* [ 2] Make it possible to create new users.
$rake notes:fixme
app/controllers/users_controller.rb
* [ 2] Should token really be accessible?
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.
- Bundles > TODO > Show TODO List
$ rails r 'p [Article, Comment, User].map(&:count)'
[0,0,0]
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]
$ 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"
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
- 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
Ruby Dramas http://rubydramas.com
$ 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
$ 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
Fucking. EPIC!