Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Collection Routing in Rails

Collection Routing in Rails

Claudio B.

April 09, 2015
Tweet

More Decks by Claudio B.

Other Decks in Programming

Transcript

  1. TL;DR DELETE "/posts/1" PostsController#destroy id: 1 DELETE "/posts/4" PostsController#destroy id:

    4 Rails 4 DELETE "/posts/1,4" PostsController#destroy_many ids: [1,4] Rails 5 Task: delete many posts in a blog ? 2/12
  2. Rails 5 TL;DR Task: ______ many posts in a blog

    PATCH "/posts/1..2" PostsController#update_many ids: [1,2] GET "/posts/1..3,5" PostsController#index ids: [1,2,3,5] DELETE "/posts/1,4" PostsController#destroy_many ids: [1,4] ? 3/12
  3. TL;DR Task: ______ many posts in a blog PATCH "/posts/1..2"

    PostsController#update_many ids: [1,2] GET "/posts/1..3,5" PostsController#index ids: [1,2,3,5] DELETE "/posts/1,4" PostsController#destroy_many ids: [1,4] Not in Rails 5 4/12
  4. May 2011 class PostsController < ActiveController::Base # PUT /posts/1,4,50,90 def

    update_many @posts = Post.find params[:ids] Post.transaction do @posts.each do |post| post.update! params[:posts][post.id] end end end end gist.github.com/dhh/981520 5/12
  5. February 2015 gist.github.com/claudiob/34f610b240a3fec364ae Let’s try to extract a single change

    from the PR GET "/posts/1,3,4" PostsController action: show id: "1,3,4" PostsController action: index ids: ["1", "3", "4"] Is this better than a filter? 8/12
  6. February 2015 gist.github.com/claudiob/34f610b240a3fec364ae Let’s try to extract a single change

    from the PR GET "/posts?ids[]=1&ids[]=3&ids[]=4" PostsController action: index ids: ["1", "3", "4"] When would the query cache expire? 9/12
  7. February 2015 gist.github.com/claudiob/34f610b240a3fec364ae Let’s try to extract a single change

    from the PR DELETE "/posts?ids[]=1&ids[]=3&ids[]=4" PostsController action: destroy id: "1,3,4" PostsController action: destroy_many ids: ["1", "3", "4"] Would this cause confusion? 10/12
  8. March 2015 Rails 5.0 Basecamp Perfect example of how something

    that seems “obvious” isn’t always so. I’ve yet to find any collection logic in my apps that would work well as an extraction. There always seems to be app-specific wriggles to contend with, and the code isn’t really complex in any case. I vote to not include this in core. 11/12