(The title quotes Joshua Grahamтs comment here) I agree completely with Josh. In a casual conversation with Tim Cochran of Thoughtworks, we both felt that using RESTful controllers does not mean exposing all of your model objects, or database tables. Thoughts still need to go into designing the right interfaces.
In Rails, examples of RESTful controllers are usually CRUD controllers of ActiveRecord objects. (and the ActiveRecord objects are just straight mapping to database columns). My given example in the previous post is exactly like that, and thatтs because script/generate and scaffolding just make it so easy.
However, thatтs not an excuse not to think about what to expose, and what to hide. In our current project we have a rails app (say an HR app) that needs to obtain/update the Account information on another rails app (say an sales app). On the sales app side, letтs say Account is an ActiveRecord that has the following fields:
id: integer
number: integer
name:string
description: string
balance:integer
sales_person_number: integer
and there is already an AccountsController, with associated HTML forms, for CRUD-ing all the fields on an Account.
On the HR app, we are interested only in the sales_person_number of the account. So we instead implemented a brand new SalesPersonAssignmentsController on the sales app, to assign a sales person, so that
POST http://sales.app/sales_person_assignments params => {:sales_person_number => 666} # Give sales_person an account to look after
returns an empty HTTP response, with status code 201 CREATED, and the тLocationт header : "/sales_person_assignments/123"
# The sales app had given account #123 for the salesperson to look after
We havenтt implemented any security measures yet, to prevent the HR app to hit other actions on the sales appтs AccountsController. But at least the intention is that the HR app will only hit the SalesPersonAssignmentsController only, and nothing else, on the sales app. That hopefully limits the integration points, and make the change on one app have less impact on another app.
Conclusion: I think encapsulation, loosely-coupled systems, are good ideas. They donтt call me Captain Obvious for nothing :)
No comments yet.
You must be logged in to add your own comment.