Filter inheritance

can anyone tell me how before filter can call the private method audit which from the parent class ? because the bank controller has a private method called audit, this one should not be inheritance by VaultController, so how the before_filter use the private method audit ?

if the child class can use the parent class private method . does it break encapsulation ? does the private has any sense here .because you can use it from ur child class , why just make it public ?

below is the example from rail api.

Controller inheritance hierarchies share filters downwards, but subclasses can also add or skip filters without affecting the superclass. For example:

  class BankController < ActionController::Base     before_filter :audit

    private       def audit         # record the action and parameters in an audit log       end   end

  class VaultController < BankController     before_filter :verify_credentials

    private       def verify_credentials         # make sure the user is allowed into the vault       end   end

Now any actions performed on the BankController will have the audit method called before. On the VaultController, first the audit method is called, then the verify_credentials method. If the audit method renders or redirects, then verify_credentials and the intended action are never called.

* Mark Ma <rails-mailing-list@andreas-s.net> [2008-10-06 20:59:58 +0200]:

can anyone tell me how before filter can call the private method audit which from the parent class ? because the bank controller has a private method called audit, this one should not be inheritance by VaultController, so how the before_filter use the private method audit ?

if the child class can use the parent class private method . does it break encapsulation ? does the private has any sense here .because you can use it from ur child class , why just make it public ?

Ruby's private/public concept is different from those in other language like java. Make a method private means you can't invoke it unless the receiver is 'self'. So you can use parent's private methods in its children, but you can't use Foo's private methods in a class who is not child of Foo.

Jan

First off, private/protected probably does not mean what you think it does (ie it's not the same as in C++ or java). For example:

class A   private   def a_private_method     puts "hi"   end

  protected   def a_protected_method     puts "ho"   end end

class B< A   def foo     a_private_method   end

  def bar     A.new.a_private_method   end

  def baz     A.new.a_protected_method   end end

B.new.foo #=> "hi" B.new.bar #=> NoMethodError: private method `a_private_method' called for #<A:0x6452c> B.new.baz #=> "ho"

On top of that, filters eventually use the send method, which ignores protected/privateness

Fred