Ruby convention for chaining calls over multiple lines
Solution 1:
There is actually a section on that in the Ruby style guide:
Adopt a consistent multi-line method chaining style. There are two popular styles in the Ruby community, both of which are considered good - leading
.
(Option A) and trailing.
(Option B).
(Option A) When continuing a chained method invocation on another line keep the
.
on the second line.# bad - need to consult first line to understand second line one.two.three. four # good - it's immediately clear what's going on the second line one.two.three .four
(Option B) When continuing a chained method invocation on another line, include the
.
on the first line to indicate that the expression continues.# bad - need to read ahead to the second line to know that the chain continues one.two.three .four # good - it's immediately clear that the expression continues beyond the first line one.two.three. four
A discussion on the merits of both alternative styles can be found here.
Solution 2:
In Ruby 1.9+ it's possible to write like this:
query = reservations_scope
.for_company(current_company)
.joins{property.development}
.group{property.development.id}
.group{property.development.name}
.group{property.number}
.group{created_at}
.group{price}
.group{reservation_path}
.group{company_id}
.group{user_id}
Much more readable, I think.