Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

What's new in rails 4

  • Login to see the comments

What's new in rails 4

  1. 1. Whats new in Rails 4Lucas Caton
  2. 2. www.lucascaton.com.br
  3. 3. 4
  4. 4. May 1, 2013Rails 4.0: Release Candidate 1 released!
  5. 5. Ruby 1.8.7Ruby 1.9.2Ruby 1.9.3Ruby 2.0.0
  6. 6. RubyGems 2.x
  7. 7. ThreadSafety
  8. 8. memcache-clientdalli=> 20% faster=> ThreadSafe=> Easier to integrate with NewRelic RPM
  9. 9. ActiveRecordActiveModel4
  10. 10. #wherePost.find_all_by_title(Rails 4)Post.where(title: Rails 4)
  11. 11. #find_or_*Post.find_or_initialize_by_title(Rails 4)Post.find_or_initialize_by(title:Rails4)
  12. 12. #update@post.update_attributes(post_params)@post.update(post_params)
  13. 13. #update_columns@post.update_attribute(post_params)@post.update_columns(post_params)Skip validations!
  14. 14. #all@posts = Post.scoped@posts = Post.allActiveRecord::Relation
  15. 15. Scopesscope :sold, where(state: sold)default_scope where(state: available)scope :sold, ->{ where(state: sold) }default_scope ->{ where(state: available) }
  16. 16. class User < ActiveRecord::Basedef visible_postscase rolewhen Country ManagerPost.where(country: country)when ReviewerPost.publishedwhen Bad User[]endendend
  17. 17. @posts = current_user.visible_postsif @posts.any?@posts.recentelse[]end
  18. 18. #noneclass User < ActiveRecord::Basedef visible_postscase rolewhen Country ManagerPost.where(country: country)when ReviewerPost.publishedwhen Bad UserPost.noneendendend
  19. 19. #notif authorPost.where(author != ?, author)elsePost.where(author IS NOT NULL)endPost.where.not(author: author)
  20. 20. #orderUser.order(created_at DESC)User.order(:name, created_at DESC)User.order(created_at: :desc)User.order(:name, created_at: :desc)
  21. 21. ActiveModel4
  22. 22. ActiveModelclass SupportTicketinclude ActiveModel::Conversioninclude ActiveModel::Validationsextend ActiveModel::Namingextend ActiveModel::Translationattr_accessor :title, :descriptionvalidates_presence_of :titlevalidates_presence_of :descriptionend
  23. 23. ActiveModelclass SupportTicketinclude ActiveModel::Modelattr_accessor :title, :descriptionvalidates_presence_of :titlevalidates_presence_of :descriptionend
  24. 24. StrongParameters4
  25. 25. class User < ActiveRecord::Baseattr_accessible :nameend# Controllerdef updateif @user.update_attributes(params[:user])redirect_to @user, notice: Updatedendend
  26. 26. Strong Parametersclass User < ActiveRecord::Baseend# Controllerdef updateuser_params = params.require(:user).permit(:name)if @user.update(params[:user])redirect_to @user, notice: Updatedendend
  27. 27. Routes4
  28. 28. match /items/:id/purchase, to: items#purchaseXSS Attack (Cross-site Scripting)post /items/:id/purchase, to: items#purchasematch /items/:id/purchase, to: items#purchase, via: :postmatch /items/:id/purchase, to: items#purchase, via: :all
  29. 29. PATCH Method for HTTPAbstractSeveral applications extending the Hypertext TransferProtocol (HTTP) require a feature to do partial resourcemodification. The existing HTTP PUT method only allowsa complete replacement of a document. This proposaladds a new HTTP method, PATCH, to modify an existingHTTP resource.http://tools.ietf.org/html/rfc5789
  30. 30. $ rake routesitems GET /items(.:format) items#indexPOST /items(.:format) items#createnew_item GET /items/new(.:format) items#newedit_item GET /items/:id/edit(.:format) items#edititem GET /items/:id(.:format) items#showPUT /items/:id(.:format) items#updatePATCH /items/:id(.:format) items#updateDELETE /items/:id(.:format) items#destroyPATCH HTTP verb
  31. 31. ActionController4
  32. 32. #before_actionbefore_filter :set_user, only: [:update]before_action :set_user, only: [:update]
  33. 33. Encrypted cookie storedin the browser
  34. 34. Flash messages<p id="notice"><%= flash[:notice] %></p><p id="notice"><%= notice %></p>
  35. 35. Streaming
  36. 36. Views4
  37. 37. collections helpers<% @owners.each do |owner| %><%= radio_button_tag :owner_id, owner.id %><%= owner.name %><% end %>collection_radio_buttons(:item, :owner_id, @owners, :id, :name)collection_check_boxes(:item, :owner_id, @owners, :id, :name)
  38. 38. date fields<%= f.date_field :return_date %><input id="item_return_date" name="item[return_date]" type="date">
  39. 39. date fields<%= f.date_field :return_date %>
  40. 40. TurboLinksaka. full page pjax
  41. 41. Other improvements=> Routing Concerns=> Test Folder Structure=> Caching improvements=> Custom Flash Types=> ActionController Live=> ETags=> jbuilder template handler
  42. 42. LinksRails Guides - Upgrading from Rails 3.2 to Rails 4.0:http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-3-2-to-rails-4-0Rails Casts - Upgrading to Rails 4http://railscasts.com/episodes/415-upgrading-to-rails-4Code School - Learn Rails 4 best practiceshttp://www.codeschool.com/courses/rails-4-zombie-outlaws
  43. 43. Thank you!

×