Rails makes many of our day to day tasks much easier, but sometimes this ease
comes at a cost. Consistently across applications we see a lack of
encapsulation and the shared global state of controllers and views to be one
of the biggest sources of complexity and bugs. In this episode Chris and Josh dive into
the causes, and solutions to this complexity in hopes of building easier to
maintain Rails apps.
Hi @acandael, yup, I think that is a nice improvement. Purposefully went pretty strong on the âdonât access instance variables in partials topicâ in the video since it is both a source of trouble, and easy to avoid.
Do you have any opinions on the global state when using rails as the API layer in your architecture ? I really try to avoid global state there as well and some of these tips apply but you might have some other advice
@novarac23 In APIs specifically, things like flash, session, and cookies donât make sense, so thereâs momentum towards reducing global state already. Many of the same principles, like avoiding instance variables in controllers and favoring methods returning correctly scoped information, still apply.
I think youâre missing an attr_reader :users in this class.
class SubscriberCounter
def initialize(users:)
@users = users
end
def count
users.where.not(subscription: nil)
end
end
But on another note, I like the idea of passing in just the users that are needed, but if youâre passing in the list of all users and then whittling it down, wouldnât that be an extreme waste of cpu time? Probably doesnât matter for users, but for whatever your business actually does it could be very bad.
Unless youâre relying on passing a lazy collection reference, but that seems like itâs violating some rule about passing native things instead of complex objects/hashes/references (but I donât know the name for this rule or if itâs even a rule).
What am I missing?
Btw, I really like this article even though I had complaints. Iâm definitely going to change how I do use my before filters and I love the idea of passing locals. This is really nice!
Any thoughts about how to control other types of global state like Rails.application.config? Thatâs one that Iâve been trying to figure out how to test recently.
The users passed in is an ActiveRecord_Relation. Thereâs no performance cost because the query isnât executed until count is called, but perhaps it would be better named users_relation to make this clearer.