Bundler.require(:default, Rails.env) if defined?(Bundler) module Blog class Application < Rails::Application config.encoding = "utf-8" config.filter_parameters += [:password] end end Sunday, November 6, 11
Bundler.require(:default, Rails.env) if defined?(Bundler) module Blog class Application < Rails::Application config.encoding = "utf-8" config.filter_parameters += [:password] end end uma Rack App! Sunday, November 6, 11
Qualquer objeto que responde ao método call, retornando um array com três parâmetros: o response status, os headers e o body, que por sua vez deve responder ao método each. Rack App! lambda { |env| [ 200, {“Content-Type” => “plain/text”}, ["Hello!"] ] } Sunday, November 6, 11
Bundler.require(:default, Rails.env) if defined?(Bundler) module Blog class Application < Rails::Application config.encoding = "utf-8" config.filter_parameters += [:password] end end uma Rack App! Sunday, November 6, 11
Bundler.require(:default, Rails.env) if defined?(Bundler) module Blog class Application < Rails::Application config.encoding = "utf-8" config.filter_parameters += [:password] end end Sunday, November 6, 11
gems listed in the Gemfile. gemfile = File.expand_path('../../Gemfile', __FILE__) begin ENV['BUNDLE_GEMFILE'] = gemfile require 'bundler' Bundler.setup rescue Bundler::GemNotFound => e STDERR.puts e.message STDERR.puts "Try running `bundle install`." exit! end if File.exist?(gemfile) Sunday, November 6, 11
gems listed in the Gemfile. gemfile = File.expand_path('../../Gemfile', __FILE__) begin ENV['BUNDLE_GEMFILE'] = gemfile require 'bundler' Bundler.setup rescue Bundler::GemNotFound => e STDERR.puts e.message STDERR.puts "Try running `bundle install`." exit! end if File.exist?(gemfile) Sunday, November 6, 11
2.3 Rails 3 map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get } resources :products do member do get :short post :toggle end get :sold, :on => :collection end Sunday, November 6, 11
=> "[email protected]" def signup_notification(recipient) @account = recipient attachments['image.jpg'] = File.read("image.jpg") mail(:to => recipient.email) do |format| format.html format.text end end end ActionMailer nova API Sunday, November 6, 11
=> "[email protected]" def signup_notification(recipient) @account = recipient attachments['image.jpg'] = File.read("image.jpg") mail(:to => recipient.email) do |format| format.html format.text end end end ActionMailer nova API Defaults Sunday, November 6, 11
=> "[email protected]" def signup_notification(recipient) @account = recipient attachments['image.jpg'] = File.read("image.jpg") mail(:to => recipient.email) do |format| format.html format.text end end end Variáveis de instância ActionMailer nova API Defaults Sunday, November 6, 11
=> "[email protected]" def signup_notification(recipient) @account = recipient attachments['image.jpg'] = File.read("image.jpg") mail(:to => recipient.email) do |format| format.html format.text end end end Variáveis de instância Attachments tipo cookies ActionMailer nova API Defaults Sunday, November 6, 11
=> "[email protected]" def signup_notification(recipient) @account = recipient attachments['image.jpg'] = File.read("image.jpg") mail(:to => recipient.email) do |format| format.html format.text end end end Variáveis de instância Attachments tipo cookies mail tipo respond_to do |format| ActionMailer nova API Defaults Sunday, November 6, 11
• Extrair a lógica comum entre ActiveRecord e ActiveResource • Hoje • Desempenha papel no agnosticismo de ORM • Permite a criação de models à la ActiveRecord ActiveModel Sunday, November 6, 11
class Base extend ActiveModel::Naming include CustomMethods, Observing, Validations include ActiveModel::Conversion include ActiveModel::Serializers::JSON include ActiveModel::Serializers::Xml end end Sunday, November 6, 11
extend ActiveModel::Naming ... include ActiveModel::Conversion include Validations ... include ActiveModel::MassAssignmentSecurity include Callbacks, ActiveModel::Observing, Timestamp end end Sunday, November 6, 11
def test_to_key; end def test_to_param; end def test_valid?; end def test_persisted?; end def test_model_naming; end def test_errors_aref; end def test_errors_full_messages; end end end end Sunday, November 6, 11
=> true}) Job.where(:published => true) Faz um query no DB imediatamente e retorna um array de Jobs Não faz query no DB, retorna um ActiveRecord::Relation Rails 2.3 Rails 3 Sunday, November 6, 11
< ApplicationController def index @jobs = Jobs.where(:published => true).order("created_at DESC") end end # app/views/jobs/index.html.erb <% cache do %> <% @jobs.each do |job| %> ... <% end %> <% end %> Sunday, November 6, 11
< ApplicationController def index @jobs = Jobs.where(:published => true).order("created_at DESC") end end # app/views/jobs/index.html.erb <% cache do %> <% @jobs.each do |job| %> ... <% end %> <% end %> Não realiza query no DB Sunday, November 6, 11
< ApplicationController def index @jobs = Jobs.where(:published => true).order("created_at DESC") end end # app/views/jobs/index.html.erb <% cache do %> <% @jobs.each do |job| %> ... <% end %> <% end %> Só aqui que será feito a query no DB Não realiza query no DB Sunday, November 6, 11
< ApplicationController def index @jobs = Jobs.where(:published => true).order("created_at DESC") end end # app/views/jobs/index.html.erb <% cache do %> <% @jobs.each do |job| %> ... <% end %> <% end %> Só aqui que será feito a query no DB Não realiza query no DB Se estiver cacheado, a query não é disparada! Sunday, November 6, 11
ActiveRecord::Base has_many :comments scope :published, where('posts.published_at is not null') scope :recent, published.order("posts.published_at desc").limit(10) end class Comment < ActiveRecord::Base belongs_to :post scope :from_published_posts, joins(:post) & Post.published end Sunday, November 6, 11
ActiveRecord::Base has_many :comments scope :published, where('posts.published_at is not null') scope :recent, published.order("posts.published_at desc").limit(10) end class Comment < ActiveRecord::Base belongs_to :post scope :from_published_posts, joins(:post) & Post.published end Sunday, November 6, 11
ActiveRecord::Base has_many :comments scope :published, where('posts.published_at is not null') scope :recent, published.order("posts.published_at desc").limit(10) end class Comment < ActiveRecord::Base belongs_to :post scope :from_published_posts, joins(:post) & Post.published end Sunday, November 6, 11
ActiveRecord::Base has_many :comments scope :published, where('posts.published_at is not null') scope :recent, published.order("posts.published_at desc").limit(10) end class Comment < ActiveRecord::Base belongs_to :post scope :from_published_posts, joins(:post) & Post.published end Sunday, November 6, 11
no controller, na action e no status do recurso HttpCacheResponder: adiciona o cabeçalho HTTP Last-Modified para requests de API CollectionResponder: altera o redirecionamento para a action :index ao criar/atualizar um recurso Sunday, November 6, 11
post, :confirm => 'Are you sure?', :method => :delete <a href="/posts/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a> Sunday, November 6, 11
post, :confirm => 'Are you sure?', :method => :delete <a href="/posts/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a> Sunday, November 6, 11
Prototype: default • jQuery: http://github.com/rails/jquery-ujs • MooTools: http://mootools.net/forge/p/rails_3_driver • Você pode fazer o seu! Sunday, November 6, 11
def strong(content) "<strong>#{h content}</strong>".html_safe end end Dicas: 1. Certificar-se de que todo input está sendo escapado Sunday, November 6, 11
def strong(content) "<strong>#{h content}</strong>".html_safe end end Dicas: 1. Certificar-se de que todo input está sendo escapado 2. Chamar html_safe no output Sunday, November 6, 11