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
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
Actually, the path was a typo. I am using the
path: /vendor/plugins/broom_stick/.
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 S.
http://somethinglearned.com
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 S.
http://somethinglearned.com
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
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 S.
http://somethinglearned.com
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
Thanks, I’ll give that I try!
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!
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 S.
http://somethinglearned.com
On 12-Apr-06, at 10:51 AM, Charlie B. 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