#349 Rails Modularity pro
Rails is a modular framework allowing you to include only what you need. Here I show how the smallest Rails app works, and then I take a look at how to whittle down a full Rails application stack.
- Download:
- source codeProject Files in Zip (133 KB)
- mp4Full Size H.264 Video (40.6 MB)
- m4vSmaller H.264 Video (18.1 MB)
- webmFull Size VP8 Video (18.1 MB)
- ogvFull Size Theora Video (39.6 MB)
Resources
- The Smallest Rails App
- Episode 317: Rack App From Scratch (pro)
- Episode 348: The Rails API Gem
- Episode 299: Rails Initialization Walkthrough
terminal
gem install coderay mate config.ru rackup -p 3000 mkdir -p app/controllers mkdir -p app/views/hello rails new blog cd blog rails g scaffold article name content:text rails g jquery:install
smallapp/config.ru
require "action_controller/railtie" require "coderay" class TheSmallestRailsApp < Rails::Application config.secret_token = "1780a66b68f2728158a3820af99b696f29285e82d23e9" initialize! routes.draw do root to: 'hello#world' end end run TheSmallestRailsApp class HelloController < ActionController::Base def world render inline: " <!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 # run HelloController.action(:world)
config/application.rb
require "active_model/railtie" require "action_controller/railtie" # require "action_mailer/railtie" # require "active_resource/railtie" # require "sprockets/railtie" # require "rails/test_unit/railtie" ... config.assets.enabled = false config.session_store :cookie_store, key: '_blog_session' config.secret_token = '6473461584e7c73f8884...'
views/layouts/application.html.erb
<%= javascript_include_tag :defaults %>
models/article.rb
class Article extend ActiveModel::Naming extend ActiveModel::Translation include ActiveModel::Validations include ActiveModel::Conversion attr_accessor :id, :name, :content def self.all @articles ||= [] end def self.find(id) all.detect { |a| a.id == id.to_i } end def initialize(attrs = {}) self.attributes = attrs end def persisted? @id.present? end def save if valid? unless persisted? self.id = self.class.all.size + 1 self.class.all << self end self else false end end def attributes=(attrs) attrs.each { |name, value| send("#{name}=", value) } end def update_attributes(attrs = {}) self.attributes = attrs save end def destroy self.class.all.delete(self) end end