Forum: Ruby on Rails I can't get rails to see my plugin. How can I this?

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.
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2006-04-12 01:36
I'm trying to produce a plugin to help me with my page caching woes.  I
can't seem to get rails to see my method within the plugin.  This is my
first attempt at a plugin so I'm sure I'm missing something.  I've
created a directory /vendor/lib/plugins/broom_stick/.  I then created an
init.rb file with:
require 'broom_stick'

I then created /vendor/lib/plugins/broom_stick/lib/broom_stick.rb
Here are the contents of broom_stick.rb.  It doesn't add any new
functionalit...yet.  I wanted to make sure I could get the plugin seen
by rails before I did any real coding.  Thanks in advance for any
assistance.

module ActionController::Caching::Pages::ClassMethods
  # Expires the page that was cached with the +path+ as a key. Example:
  #   expire_page "/lists/show"
  def expire_each_page(path)
    return unless perform_caching
    benchmaddrk "EXPIRED ALL PAGES: #{page_cache_file(path)}" do
    File.delete(page_cache_path(path)) if
File.exists?(page_cache_path(path))
    Dir.foreach(page_cache_path(path)) {|x| benchmark ("Got " + x) }
    end
  end
end
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-04-12 06:13
(Received via mailing list)
charlie bowman wrote:

> I'm trying to produce a plugin to help me with my page caching woes.  I
> can't seem to get rails to see my method within the plugin.  This is my
> first attempt at a plugin so I'm sure I'm missing something.  I've
> created a directory /vendor/lib/plugins/broom_stick/.  I then created an

The simplest possible thing, you do mean:

#{RAILS_ROOT}/vendor/lib/plugins/broom_stick/

right?

--

Ray
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-04-12 06:34
(Received via mailing list)
On 11-Apr-06, at 4:36 PM, charlie bowman wrote:

> I'm trying to produce a plugin to help me with my page caching
> woes.  I
> can't seem to get rails to see my method within the plugin.  This
> is my
> first attempt at a plugin so I'm sure I'm missing something.  I've
> created a directory /vendor/lib/plugins/broom_stick/.  I then
> created an
> init.rb file with:
> require 'broom_stick'
>

Hi Charlie,

I think your folder hierarchy is wrong:

vendor/plugins/broom_stick/init.rb
vendor/plugins/broom_stick/lib/broom_stick.rb

If you've got any doubt, the simplest thing to do is use the plugin
generator to see how to properly layout a plugin:

(in your rails application root)

./script/generate plugin test_this

will show you where the files ought to go (you can remove vendor/
plugins/test_this afterwards).

Regards,
Trevor
--
Trevor Squires
http://somethinglearned.com
89d967359903c639d31e4cad4569f537?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-04-12 14:44
(Received via mailing list)
Actually, the path was a typo.  I am using the
path: /vendor/plugins/broom_stick/.
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-04-12 17:21
(Received via mailing list)
Hi again Charlie,

you've got me wondering - you *do* realize that you've only added
your method to the ClassMethods module (so it's not a controller
instance method) right?

So you have to use it like:

MyController.expire_each_page(somepath)

The code you posted really ought to work so trying to call a class
method as though it was an instance method is about the only place I
can think you're going wrong.

HTH,
Trevor

--
Trevor Squires
http://somethinglearned.com
89d967359903c639d31e4cad4569f537?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-04-12 17:45
(Received via mailing list)
I didn't.  I'm still trying hard to grasp the inner working of rails and
ruby inheritance in general.  I have modified the plugin but it is still
breaking because it can't find the page_cache_path and page_cache_file
methods.  Here's what I've got so far.  Thank you for your help so far!

module ActionController::Caching::Pages
  def expire_each_page(path)
    return unless perform_caching
    logger.error("EXPIRED ALL PAGES: #{path}")
    File.delete(page_cache_path(path)) if
File.exists?(page_cache_path(path))
    Dir.foreach(page_cache_path(path)) {|x| benchmark ("Got " + x) }
    rescue
  end
end
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-04-12 18:01
(Received via mailing list)
Hey again,

you can't find page_cache_path and page_cache_file because they are
*class* methods.

With the changes you sent your expire_each_page method is now an
*instance* method (it's no longer in the ClassMethods module).

So, change the calls to page_cache_path and page_cache_file to be
self.class.page_cache_path and self.class.page_cache_file and see how
you get on.

Regards,
Trevor

--
Trevor Squires
http://somethinglearned.com
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-04-12 19:34
(Received via mailing list)
Hi Charlie,

it would seem on my last email I steered you a bit wrong:  namely I
forgot that page_cache_file and page_cache_path are private class
methods so you can't call them from an instance method.

Try this instead:

module ActionController::Caching::Pages
   module ClassMethods
     def expire_each_page(path)
       return unless perform_caching
       logger.error("EXPIRED ALL PAGES: #{path}")
       File.delete(page_cache_path(path)) if File.exists?
(page_cache_path(path))
       Dir.foreach(page_cache_path(path)) {|x| benchmark ("Got " + x) }
       rescue
     end
   end

   def expire_each_page(path)
     self.class.expire_each_page(path)
   end
end

Regards,
Trevor

--
Trevor Squires
http://somethinglearned.com
89d967359903c639d31e4cad4569f537?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-04-12 19:34
(Received via mailing list)
Thanks again for so much help! I think I'm getting much closer to
understanding this stuff.  It still isn't quite there.  The app runs and
rails doesn't complain about not finding the methods, but I still can't
get the correct result out of the method.  Only the first logger.error
is printing to the log.  I added a two more logger statements and the
second statement won't print.  I"m I missing something else as well?

module ActionController::Caching::Pages
  # Expires the page that was cached with the +path+ as a key. Example:
expire_page "/lists/show"
  def expire_each_page(path)
    return unless perform_caching
    #test = self.class.page_cache_path(path)
    logger.error("EXPIRED ALL PAGES: #{path}")
    test = self.class.page_cache_path(path)
    logger.error("FULL PATH OF PAGES: #{test}")
    File.delete(self.class.page_cache_path(path)) if
File.exists?(self.class.page_cache_path(path))
    Dir.foreach(self.class.page_cache_path(path)) {|x| logger.error("Got
#{x}") }
    rescue
  end
end
89d967359903c639d31e4cad4569f537?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-04-12 19:37
(Received via mailing list)
Thanks, I'll give that I try!
89d967359903c639d31e4cad4569f537?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-04-12 19:52
(Received via mailing list)
Thanks, your last tip really helped out!  My method ran correctly.  So
just to make sure I have this correct in my head, I'm adding a method to
the ClassMethods module.  Then I'm creating a method of the same name in
ActionController::Caching::Pages.  This second method calls the method
in the module ClassMethods?  Wow this stuff can be confusing!
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-04-13 00:15
(Received via mailing list)
On 12-Apr-06, at 10:51 AM, Charlie Bowman wrote:

> Thanks, your last tip really helped out!  My method ran correctly.
> So just to make sure I have this correct in my head, I'm adding a
> method to the ClassMethods module.  Then I'm creating a method of
> the same name in ActionController::Caching::Pages.  This second
> method calls the method in the module ClassMethods?  Wow this stuff
> can be confusing!
>

That's about the size of it.

Regards,
Trevor
This topic is locked and can not be replied to.