如何解决 Rails 模型命名空间冲突

发布于 2024-08-11 01:35:45 字数 580 浏览 3 评论 0原文

到目前为止的故事:

我有一个 Rails 应用程序,其模型名为“Term”。一切都很顺利,直到尝试安装 Cucumber。运行时

rake cucumber

我得到

Term is not a class (TypeError)

这种情况,因为 Cucumber 包含另一个 gem,“term-ansicolor”(在控制台中进行漂亮的彩色文本输出),并且 term-ansicolor 定义了一个名为“Term”的模块。 Cucumber 在包含 Rails 模型之前包含 term-ansicolor,因此在加载“Term”模型时“Term”已经被定义为模块。顶级模块和类在 Ruby 中不能具有相同的名称,因此会发生冲突。

由于不想重命名该模型,我开始修补术语 ansicolor gem。事实证明这比我想象的要难。我将 Term 模块名称更改为“ANSITerm”,但我不知道如何让 Cucumber 加载修改后的 gem,我已将其放入 RAILS_ROOT/vendor/gems/term-ansicolor 中。

有什么想法吗?我是不是找错了树?

The story so far:

I have a rails app with a model named "Term". All is well until trying to install Cucumber. Upon running

rake cucumber

I get

Term is not a class (TypeError)

This happens because Cucumber includes another gem, 'term-ansicolor' (to do the nifty colored text output in the console), and term-ansicolor defines a module named "Term". Cucumber includes term-ansicolor before including the Rails models, thus "Term" is already defined as a module when loading the "Term" model. Top-level modules and classes cannot have the same names in Ruby, thus the collision.

Preferring not to rename the model, I set about patching the term-ansicolor gem. This proved harder than I thought. I changed the Term module name to "ANSITerm", but I can't figure out how to get Cucumber to load my modified gem, which I've put into RAILS_ROOT/vendor/gems/term-ansicolor.

Any ideas? Am I barking up the wrong tree?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫卆 2024-08-18 01:35:45

这就是我所做的:

sudo gem uninstall term-ansicolor
sudo gem uninstall cucumber

从 github 下载 term-ansicolor 和 cucumber 的源代码
在 term-ansicolor 源中搜索 “module Term” 并替换为 “module ANSITerm”
在 Cucumber 源中搜索 "include Term" 并替换为 "include ANSITerm"
在黄瓜源中搜索 "::Term" 并替换为 "::ANSITerm"

sudo gem install term-ansicolor 从我的本地存储库
来自我的本地存储库的 sudo gem install cucumber

现在我有两个 gem 需要维护,但这似乎比更改我的应用程序中的所有模型引用更容易。

欢迎提出意见/建议。

Here's what I did:

sudo gem uninstall term-ansicolor
sudo gem uninstall cucumber

Download sources for term-ansicolor and cucumber from github
Search term-ansicolor source for "module Term" and replace with "module ANSITerm"
Search cucumber source for "include Term" and replace with "include ANSITerm"
Search cucumber source for "::Term" and replace with "::ANSITerm"

sudo gem install term-ansicolor from my local repository
sudo gem install cucumber from my local repository

Now I have two gems to maintain, but that seems easier than changing all the model references in my app.

Comments/suggestions welcome.

最笨的告白 2024-08-18 01:35:45

两种解决方案:

1) 将应用程序的术语模型更改为其他模型。

2) 修补 term-ansicolor 以拥有命名空间 Term 并使用该 gem 代替。

Two solutions:

1) Change your app's Term model to be something else.

2) Patch term-ansicolor to have a namespaced Term and use that gem instead.

·深蓝 2024-08-18 01:35:45

我确实在我的规范中遇到了与我的应用程序模型 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

I did get a namespace collision with my application model Node and Capybara::Node in my specs. It was sufficient for me to explicitly state toplevel namespace in specs to fix the problem:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文