如何解决Rails模型命名空间冲突
到目前为止的故事:
我有一个名为“Term”的模型的rails应用程序。 一切都很好,直到尝试安装黄瓜。 跑步的时候
rake cucumber
我明白了
Term is not a class (TypeError)
这是因为Cucumber包含另一个gem,’term-ansicolor’(在控制台中执行漂亮的彩色文本输出),term-ansicolor定义了一个名为“Term”的模块。 在包含Rails模型之前,Cucumber包括term-ansicolor,因此在加载“Term”模型时,“Term”已被定义为模块。 顶级模块和类在Ruby中不能具有相同的名称,因此发生冲突。
我不想重命名模型,而是开始修补term-ansicolor gem。 事实certificate这比我想象的更难。 我将Term模块名称更改为“ANSITerm”,但我无法弄清楚如何让Cucumber加载我修改过的gem,我已将其放入RAILS_ROOT / vendor / gems / term-ansicolor。
有任何想法吗? 我吠叫错了树吗?
两种解决方案
1)将您应用的期限模型更改为其他内容。
2)Patch term-ansicolor具有命名空间Term并改为使用该gem。
这是我做的:
sudo gem uninstall term-ansicolor
sudo gem uninstall cucumber
从github下载term-ansicolor和cucumber的来源
搜索"module Term"
term-ansicolor源并替换为"module ANSITerm"
搜索黄瓜源"include Term"
并替换为"include ANSITerm"
搜索黄瓜源"::Term"
并替换为"::ANSITerm"
sudo gem install term-ansicolor
从我的本地存储库sudo gem install term-ansicolor
sudo gem install cucumber
从我的本地存储库sudo gem install cucumber
现在我有两个gem要维护,但这似乎比改变我的应用程序中的所有模型引用更容易。
欢迎提出意见/建议。
我确实在我的规范中得到了与我的应用程序模型Node和Capybara :: Node的命名空间冲突。 我可以在规范中明确声明顶级命名空间来解决问题:
it "should find the specified node scoped under the current user" do ::Node.should have_received(:find_by_id_for_user).with(node.id, user) end