Ruby on Rails Thursday, January 15, 2015


On Thursday, January 15, 2015 at 11:51:39 AM UTC, Andrey Nering wrote:
I have a custom form builder for my application:
I am requiring it in an initializer:

require "#{Rails.root}/lib/ruby/app_form_builder.rb"
ActionView::Base.default_form_builder = ActionView::Helpers::AppFormBuilder


This works, but I have to restart the server on every change. I was reading the Constant Guide, but didn't find the right way to made this file to be automaticaly reloaded. I tried to put this in /app/helpers forlder and making it default in application.rb, but I got "unitialized constant" error.


require is never a good idea if you want something to be reloaded. You've also got a clash between the class name and it's location on disk - having the class just be AppFormBuilder in lib/app_form_builder.rb should work.

Lastly if you look into the bowels of action view, the default_form_builder method looks like this

 def default_form_builder
     builder = ActionView::Base.default_form_builder
     builder.respond_to?(:constantize) ? builder.constantize : builder
 end

So if you set default_form_builder to be a "AppFormBuilder" then that should allow it to be reloaded.
 
Also, I would like to know if Rails can reload code that modify native Ruby classes:

class String
 
def do_something
   
'something'
 
end
end



This can't be done. code reloading is done by removing the class/module (using remove_const) and then letting it be loaded again, which wouldn't work for a class like String

Fred

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f7d461d5-dda7-480f-b26a-6f13830910c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment