4. Sendagaya.rb
Organized by @ppworks, @fukajun, and me
Concept: Looking forward to the weekly
meetup makes our everyday working fun
Held currently at Shinjuku(!?), formerly at
Sendagaya
#50 anniversary on June 3!
sendagayarb.github.io
12. GET /users users#index
POST /users users#create
GET /users/new users#new
GET /users/:id/edit users#edit
GET /users/:id users#show
PUT /users/:id users#update
DELETE /users/:id users#destroy
$ rake routes
13. GET POST PUT DELETE
/users index create - -
/users/:id show - update destroy
in addition, “new” and “edit” as supplementary resources
18. “resources” makes it
easy to design
For developer him/herself
“resources” decides on a resource form
All you have to do is to decide on a
resource name, such as “users”
/user/1 /users/1 /users/user/1 /users/user-1
19. “resources” makes it
easy to understand
For developer him/herself, co-developer,
and external developer (e.g. using API)
Suggest that there are corresponding
controller, model, view, etc, such as
UsersController, User model
20. “resources” makes it
easy to design & understand
Rails has demonstrated that
this simplicity goes well
“Constraints are liberating”
30. “resources” is the fundamental pattern
If there is a smaller and more concrete
pattern, we can design well accordingly
We want more (concrete)
patterns!
40. “Sessions” is a resource
(that doesn’t involve a model)
GET /users/sign_in devise/sessions#new
POST /users/sign_in devise/sessions#create
DELETE /users/sign_out devise/sessions#destroy
…
# config/routes.rb
devise_for :users
45. POST /session sessions#create
GET /session/new sessions#new
GET /session/edit sessions#edit
GET /session sessions#show
PUT /session sessions#update
DELETE /session sessions#destroy
$ rake routes
46. GET POST PUT DELETE
/session show create update destroy
in addition, “new” and “edit” as supplementary resources
47. GET POST PUT DELETE
/session show create update destroy
This is also a pattern
61. I wish I could use query parameters
like models (w/ validation)...
62. class PostsController < ApplicationController
private
def filter_params # using strong parameters
@_filter_params ||= begin
params.default( # you can set default value
per: '10'
).permit(
:date, :q, :page, :per
).validate( # you can validate like a model
date: { format: /Ad{4}-d{2}-d{2}Z/ },
q: { length: { maximum: 20 } }
)
end
end
helper_method :filter_params
end
github.com/tkawa/collection_filter
Query parameters like models
71. RESTful patterns including “resources”
are significant
Focusing on these patterns encourages
good resource design
RubyGems are also useful for this purpose
72. If you are at a loss on
resource modeling...
Focus on gems’ pattern
You will come up with the right resource
by referring to the design of a good gem
“resources” is fundamental
Diverging from the “resources” is the
last resort
73. If you are creating a gem...
You should consider designing around
resources, if possible
Let's stick to the fundamentals of
“resources”
And your gem will realize a pattern!
74. Thank you for your attention.
Let me know if you discover
more patterns!
rest-pattern.hatenablog.com