自定义/覆盖 Rails SimpleForm Gem
我正在使用 Rails gem SimpleForm,但我认为我的问题可能适用于任何 gem。
https://github.com/plataformatec/simple_form
它有很多很棒的功能和自定义功能,但我我希望能走得更远一点。例如,我真的希望生成的标记没有插入默认类,但我仍然希望能够手动插入自己的类。我发现我可以通过注释 gem 文件中的行来删除一些类。然而,这超出了我的项目范围——我想要一个 DRY 解决方案,当我部署到生产环境时,该解决方案将保留在我的项目中,最好不需要打包所有的 gem。
我想这是一种常见的情况,可以适用于任何gem,并且我应该能够通过在我的项目中添加覆盖gem的自定义文件来完全或部分地覆盖任何gem...但我不确定如何。
任何帮助将不胜感激!谢谢。
I'm using the Rails gem SimpleForm, but I think my question may be applicable to any gem.
https://github.com/plataformatec/simple_form
It has a lot of great features and customization, but I'm looking to go a bit further. For example, I really wish the markup generated had no default classes inserted into it, but I'd still like the ability to insert my own manually. I found that I could remove some of the classes by commenting out lines in the gem files. However this is outside of my project-- I would want a DRY solution that will stay with my project when I deploy to production, preferably without having to pack all of my gems.
I imagine this is a common situation that could apply to any gem, and I should be able to override any gem wholly or partially probably by adding customs files in my project that override the gem... but I'm not sure how.
Any help would be appreciated! Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是在谈论猴子补丁吗?假设你的 gem 在文件中有一个类
如果你想更改 #some_method 的输出,那么你可以创建一个初始化文件并执行
你的猴子补丁只会影响 A#some_method,而不影响 A 中的其他方法。只需确保输出你的猴子补丁不会破坏宝石中的其他东西。
Are you talking about monkey patching? Say your gem has a class in a file
If you want to change the output of #some_method then you can create an initializer file and do
Your monkey patch will only affect A#some_method, and not other methods in A. Just make sure the output of your monkey patch won't break something else in the gem.