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.