Question

Confession: I only use private and public visibility for my methods!

I have a feeling this is a bad thing. But in Rails it just doesn't seem to come up as an issue.

Does anyone have an example in Rails where it would be a big mistake not to use protected visibility?

Was it helpful?

Solution

Update -- Please see the comment below that links to a true explanation of protected/private in Ruby. That was a deep seated prejudice left over from my Java days, indeed. The only important part left to my answer is that controller methods that are not actions should not be public (or at least your routes should protect them).

Single Table Inheritance is a perfect example of when protected is helpful in the model tier, as it's one of the most common uses of inheritance there.

In the controller tier, helper methods defined on ApplicationController should be marked as protected -- if they were private the other controllers would not be able to access them, but if they are public Rails will treat them as actions.

Personally, I find that I use class inheritance more than many of my friends and coworkers, even in Rails applications. Because I use it often (and coming out of my Java days), I favor protected for all helper methods to give freedom to anyone (usually myself) who wants to extend the class -- unless I'm really really embarrassed about one, then I mark it private. :)

OTHER TIPS

I have SingleTableInheritance

class Person < AR::base class Teacher < Person calss Student < Person

And I use the protected methods to implement a private method that is common for Student and Teacher:

class Person < AR::base
  def self.find(*args)
    reject_leaves(super(*args))
  end
protected
  def self.reject_leaves(target) #like a private in Teacher and Student
    case target
      when Array target.select{|t| reject_leaves(t)}
      when Person (target.leave_date < Date.today ? target : nil)
      else target
    end
  end
end

Disclaimer: There are plugins like act-as-paranoid and other to implement the feature I use here to show you the case but I have a more complex landscape, that I have simplified here to get to your point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top