josh
November 20, 2007, 6:59pm
1
Hi all
I have the following plugin code:
module IncenseCrud
def self.included(base_class)
base_class.extend(ClassMethods)
end
module ClassMethods
def performs_incense_crud
def index
list
return render(:action => ‘list’)
end
end
end
end
In init.rb I have the following:
require File.dirname(FILE ) + ‘/lib/incense_crud’
ActionController::Base.send(:include, IncenseCrud)
So far so good. In any controller I can put now
class AnyController < ApplicationController
performs_incense_crud
end
and the performs_incense_crud method is invoked. Cool - the class method
has been added to the controller through the plugin.
But strangely the instance method index does not seem to have been
added! It’s just disappeared when trying to reach
www.xxx.yyy/any_controller/index
What’s wrong here? Can’t I add these methods dynamically this way?
Thanks
Josh
josh
November 20, 2007, 8:15pm
2
define_method usually does the trick when trying to define methods
dynamically
Fred
josh
November 21, 2007, 2:04pm
3
Frederick C. wrote:
define_method usually does the trick when trying to define methods
dynamically
Fred
Thank you, this works well.
Now I’ve got another question…
In fact, my method performs_incense_crud accepts an option parameter:
def performs_incense_crud(options = {}) … end
Now I’d like to use this options parameter in another method called
list:
module IncenseCrud
def self.included(base_class)
base_class.extend(ClassMethods)
end
module ClassMethods
def performs_incense_crud(options = {})
define_method(:index) do
list
return render(:action => ‘list’)
end
define_method(:list) do
@pages, @objects = paginate :sessions, :per_page =>
(options[:items_per_page] || 10)
end
end
end
end
When using…
performs_incense_crud :items_per_page => 5
…it works great. But when omitting this option…
performs_incense_crud
…it returns the following error:
NameError in XxxController#index
undefined local variable or method ` 10’ for
#LinksController:0x32a896c
The same happens when using ? : …
:per_page => (options[:items_per_page] ? options[:items_per_page] : 10)
instead of ||.
Can anybody tell why this doesn’t work here? Thanks a lot,
Josh