I am trying to understand why my class method was not recognized. Below is my code:
wiki_patch.rb
require_dependency 'wiki_content'
module WikiRecipientPatch
def self.included(base)
base.send(:include, InstanceMethods)
base.class_eval do
alias_method_chain :recipients, :send_wiki_mail
end
end
end
module InstanceMethods
def self.set_mail_checker(mail)
@mail_checker = mail
end
end
Rails.configuration.to_prepare do
WikiContent.send(:include, WikiRecipientPatch)
end
controller.rb
WikiContent.set_mail_checker(params[:mail_checker_wiki])
I am getting this error:
NoMethodError (undefined method `set_mail_checker' for #<Class:0x4876560>):
Any idea why it happens and what the solution for it is?
You got the idiom slightly wrong.
ClassMethods
/ InstanceMethods
modules are supposed to be nested in the "main" module ( WikiRecipientPatch
in this case). You are including instance methods, but expecting class methods to somehow arise from this? Surely you meant extend ClassMethods
, didn't you?
module WikiRecipientPatch def self.included(base) base.extend ClassMethods end module ClassMethods def set_mail_checker(mail) 'mail checker' end end end class WikiContent; end WikiContent.send(:include, WikiRecipientPatch) WikiContent.set_mail_checker('whatever') # => "mail checker"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.