Forum: Ruby on Rails Filter inheritance

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mark M. (Guest)
on 2008-10-06 22:59
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.
Flower B. (Guest)
on 2008-10-06 23:16
(Received via mailing list)
* Mark Ma <removed_email_address@domain.invalid> [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
Frederick C. (Guest)
on 2008-10-06 23:20
(Received via mailing list)
On Oct 6, 7:59 pm, Mark Ma <removed_email_address@domain.invalid> wrote:
> 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 ?
>
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
This topic is locked and can not be replied to.