我有这个完整的rails引擎Foo,它的功能是X。
我想做另一个引擎,engine Bar,它几乎相同,但用y覆盖功能性x。(它基本上做同样的事情,但一些控制器动作和视图的实现方式不同)。
(稍后我可能会将其分成几个可安装的引擎,但现在,这将是设置: project Baz,使用engine Bar,它使用engine Foo)
我想知道有没有什么陷阱。它看起来不像是经常使用的模式?还有其他人在使用这个“某种引擎继承”吗?
发布于 2012-11-19 06:57:21
Ruby OpenClassing就是你想要做的。例如,
# in Engine Foo
# this code creates functionality x
Foo::SomeRubyClass
# functionality x
def some_method
0.10
end
end
# in Engine Bar
# this code opens and reevaluates the functionality x in Foo Engine
Foo::SomeRubyClass.class_eval do
# functionlity x method
def some_method
0.05
end
end关于Rails模型/视图/控制器的更全面的解释。http://edgeguides.rubyonrails.org/engines.html#overriding-models-and-controllers
https://stackoverflow.com/questions/13384832
复制相似问题