One of the most eagerly anticipated aspects of the fast-approaching Rails 3 release is its inherent modularity, and how that modularity gives the application developer more choice. We'll start with a tour of some of the headlining differences between Rails 2 and 3, and then put Rails 3's internal architecture to the test by demonstrating how to plug in some non-standard standard components, including an example of how to wire in a Java library using JRuby.
44. Script
script/rails s
script/rails g
script/server script/rails c
script/generate
script/console rails server
rails generate
rails console
Sunday, March 21, 2010
55. Routing: R3
match 'products/:id' => 'catalog#view'
resources :products do
resources :comments
resources :sales do
get :recent, :on => :collection
end
end
Sunday, March 21, 2010
56. More
• Cleaner ActionMailer
• ActiveRecord lazy queries
• ...
Sunday, March 21, 2010
57. First Online
RailsConf
http://bit.ly/online-railsconf-slides
Sunday, March 21, 2010
68. Encapsulate
what varies
Sunday, March 21, 2010
69. Railties:
Configuration
encapsulated
Sunday, March 21, 2010
70. Railtie Elements
Initialization Generators
Rake Tasks Logging Events
Sunday, March 21, 2010
71. Example
require 'rails/railtie'
class MemCacheClusterRailtie < Rails::Railtie
initializer :setup_mem_cache_cluster do |app|
app.config.cache_store = [
:mem_cache_store, 'cache1:11211', 'cache2:11211']
end
end
Sunday, March 21, 2010
75. load_application_initializers
# the framework is now fully initialized
Init: Rails 2
after_initialize
# Setup database middleware after initializers have run
initialize_database_middleware
# Prepare dispatcher callbacks and run 'prepare' callbacks
prepare_dispatcher
# Routing must be initialized after plugins to allow the former to extend the routes
initialize_routing
# Observers are loaded after plugins in case Observers or observed models are modified by
plugins.
load_observers
# Load view path cache
load_view_paths
# Load application classes
load_application_classes
# Disable dependency loading during request cycle
disable_dependency_loading
# Flag initialized
Rails.initialized = true
end
Sunday, March 21, 2010
76. Init: Rails 3
class Rails::Application
def initialize!
run_initializers(self)
self
end
end
Sunday, March 21, 2010
81. codifies contract
between
controller
and model
Sunday, March 21, 2010
82. Contract
class Model
def to_model; ...; end
def to_key; ...; end
def to_param; ...; end
def valid?; ...; end
def persisted?; ...; end
def self.model_name; ...; end
def errors; ...; end
end
Sunday, March 21, 2010
83. class ModelLintTest < Test::Unit::TestCase
def setup
@model = Model.new
end
include ActiveModel::Lint::Tests
end
Sunday, March 21, 2010
84. Validations
class SimpleModel
include ActiveModel::Validations
# => Model#valid?, Model#errors
# plus validation framework
end
Sunday, March 21, 2010
85. class IceCream
include ActiveModel::Validations
attr_accessor :flavour
validates_presence_of :flavour
end
ic = IceCream.new
ic.valid? # => false
ic.errors.full_messages
# => ["Flavour can't be blank"]
ic.flavour = "vanilla"
ic.valid? # => true
Sunday, March 21, 2010
86. AMo Modules
ActiveModel::Callbacks
- before/after hooks
ActiveModel::Dirty
- dirty attribute tracking
ActiveModel::Errors
- #errors
ActiveModel::Serialization
- #to_xml, #to_json, etc.
ActiveModel::Translation
- I18n for model attributes
ActiveModel::Validations
- Full validations framework
See ActiveModel RDoc comments for more info
Sunday, March 21, 2010