Rails:通过插件包含模块时遇到问题
Rails: trouble including modules via a plugin
我正在尝试创建一个 rails 插件,我面临的问题是在迁移插件时该应用程序不会包含我的模块。
这是我目前的情况:
1. 一个文件lib/patch/settings_helper_patch.rb
,扩展码
2. init.rb
文件 require_dependency 'patch/settings_helper_patch'
3. settings_helper_patch.rb
中的部分代码如下:
module ValidateIssuePatch
module Patch
module SettingsHelperPatch
def self.included(base)
base.send(:include, InstanceMethods)
end
module InstanceMethods
def issue_options
#some code here
end
end
end
end
end
unless SettingsHelper.included_modules.include?(ValidateIssuePatch::Patch::SettingsHelperPatch)
SettingsHelper.send(:include, ValidateIssuePatch::Patch::SettingsHelperPatch)
end
迁移插件后,我想使用issue_options
方法,但出现undefined local variable or method
错误。
如果我从控制台 运行 SettingsHelper.included_modules.include?(ValidateIssuePatch::Patch::SettingsHelperPatch)
,我会得到 uninitialized constant Patch::SettingsHelperPatch
。
但是,如果我从控制台调用 ValidateIssuePatch,我会得到 => ValidateIssuePatch
作为响应。
谁能告诉我这里缺少的魔法是什么?
首先,如果你的模块只有实例方法,我建议使用以下简单易懂的语法:
module ValidateIssuePatch
module Patch
module SettingsHelperPatch
def issue_options
# code
end
end
end
end
SettingsHelper.include(ValidateIssuePatch::Patch::SettingsHelperPatch)
其次,ValidateIssuePatch
可能被定义的原因是其他一些文件有它,这是正确需要的。该文件未以任何方式执行。我会在某处引发错误,当引发错误时,将验证代码是否正在执行/未执行。类似于以下内容:
module ValidateIssuePatch
module Patch
module SettingsHelperPatch
raise "All good" # remove this afterwards
def issue_options
# code
end
end
end
end
SettingsHelper.include(ValidateIssuePatch::Patch::SettingsHelperPatch)
很可能不会出现错误,它会确认不需要您的文件 - 根本不需要或顺序不正确。
要进一步验证这一点,只需打开控制台并使用现有代码执行以下操作:
ValidateIssuePatch::Patch::SettingsHelperPatch #=> error
require path_of_file
ValidateIssuePatch::Patch::SettingsHelperPatch #=> no more error
最后,为什么要检查 SettingsHelper
中已经包含的模块? (参考 unless
条件)您的代码应该只包含模块一次,而不是 "maybe only once".
我正在尝试创建一个 rails 插件,我面临的问题是在迁移插件时该应用程序不会包含我的模块。
这是我目前的情况:
1. 一个文件lib/patch/settings_helper_patch.rb
,扩展码
2. init.rb
文件 require_dependency 'patch/settings_helper_patch'
3. settings_helper_patch.rb
中的部分代码如下:
module ValidateIssuePatch
module Patch
module SettingsHelperPatch
def self.included(base)
base.send(:include, InstanceMethods)
end
module InstanceMethods
def issue_options
#some code here
end
end
end
end
end
unless SettingsHelper.included_modules.include?(ValidateIssuePatch::Patch::SettingsHelperPatch)
SettingsHelper.send(:include, ValidateIssuePatch::Patch::SettingsHelperPatch)
end
迁移插件后,我想使用issue_options
方法,但出现undefined local variable or method
错误。
如果我从控制台 运行 SettingsHelper.included_modules.include?(ValidateIssuePatch::Patch::SettingsHelperPatch)
,我会得到 uninitialized constant Patch::SettingsHelperPatch
。
但是,如果我从控制台调用 ValidateIssuePatch,我会得到 => ValidateIssuePatch
作为响应。
谁能告诉我这里缺少的魔法是什么?
首先,如果你的模块只有实例方法,我建议使用以下简单易懂的语法:
module ValidateIssuePatch
module Patch
module SettingsHelperPatch
def issue_options
# code
end
end
end
end
SettingsHelper.include(ValidateIssuePatch::Patch::SettingsHelperPatch)
其次,ValidateIssuePatch
可能被定义的原因是其他一些文件有它,这是正确需要的。该文件未以任何方式执行。我会在某处引发错误,当引发错误时,将验证代码是否正在执行/未执行。类似于以下内容:
module ValidateIssuePatch
module Patch
module SettingsHelperPatch
raise "All good" # remove this afterwards
def issue_options
# code
end
end
end
end
SettingsHelper.include(ValidateIssuePatch::Patch::SettingsHelperPatch)
很可能不会出现错误,它会确认不需要您的文件 - 根本不需要或顺序不正确。
要进一步验证这一点,只需打开控制台并使用现有代码执行以下操作:
ValidateIssuePatch::Patch::SettingsHelperPatch #=> error
require path_of_file
ValidateIssuePatch::Patch::SettingsHelperPatch #=> no more error
最后,为什么要检查 SettingsHelper
中已经包含的模块? (参考 unless
条件)您的代码应该只包含模块一次,而不是 "maybe only once".