I’m just getting started with rails gems, and wondering how best to add functionality to existing rails modules. For example, if I wanted to add a new form helper, I’d typically do something like this:
class ActionView::Helpers::FormBuilder
# My form defs in here
end
But I’m wondering if that’s the most elegant way of doing things – especially if, for example, I’m going to wrap the new functionality up in a gem.
For example, suppose I’m creating the gem “MyGem”, and I only want its functionality to be present if the gem is called in the controller. So in the controller I add ‘include MyGem’, and in the lib/my_gem.rb I’d typically do something like:
# lib/my_gem.rb
module MyGem
# My form defs in here
end
The question is: what is the standard way for overwriting defs in the ActionView::Helpers::FormBuilder module from within the MyGem module?
Cheers…
If you create a Class with inheritance of
ActionView::Helpers::FormBuilderyou can override all method from FormBuilder you want.You can add other method too.
After you just need use this FormBuilder when you create your form with option
:builderOr you can do an helper method likesimple_form_forto call theform_formethod with your builder.If you want do in a module you need create your class in module
In your builder you use
:builder => MyGem::FormBuilder