Rails auto-reloading plug in development mode - ruby-on-rails

I am trying to have a plugin I am developing auto-reload every time I change my code, emulating the same auto-reloading that happens normally in Rail's development mode. My plugin is primarily an ActiveRecord mixin module. I have tried all suggestions I have been able to find in related Google searches. Nothing has worked yet.
In my plugin's init.rb:
require 'activesupport' unless defined? ActiveSupport
require 'activerecord' unless defined? ActiveRecord
if RAILS_ENV == 'development'
ActiveSupport::Dependencies.load_once_paths.delete lib_path
ActiveSupport::Dependencies.load_once_paths.delete File.join(lib_path, 'crowd_compass', 'publisher.rb')
ActiveSupport::Dependencies.load_paths << lib_path
ActiveSupport::Dependencies.load_paths << File.join(lib_path, 'crowd_compass', 'publisher.rb')
end
ActiveRecord::Base.send(:include, CrowdCompass::Publisher)
Looking in the rails changelog, I did notice the feature to auto reload all plugins.
config.reload_plugins = true if RAILS_ENV == 'development'
This did not work as I expected it to when I added it to my conf/environment.rb
My plugin is structured so all files are auto-loaded by namespace => directory. I did this so I could avoid using "require", as I thought require was inhibiting my plugin from being auto-reloaded.
I have been doing all of my work in development mode through the rails console and I do not know if this behaves any different than running through mongrel (or like web server).
The plugin works as expected, but I have to reload every time I make any change to the code. Does anyone know a way to get plugins to reload?

The console definitely doesn't work like a mongrel. All of the techniques you're using are made to reload on every request, which is akin to every time you start up the console.
There isn't a way to reload code in the console without calling reload!.

Related

How can I add Rack Middleware to the test environment in Rails?

Situation
I am currently testing a Rails app using Capybara. In addition, I was using Guard with its extension guard-livereload to automatically reload my browser as soon as relevant source files changed.
As the save_and_open_page method from Capybara did not display stylesheets correctly, I applied this solution to the problem, in which a temporary view-dump capybara.html gets placed in the /public/ folder to ensure the accessibility of assets.
Now, as LiveReload has worked like a charm in development, I would like to use it during feature-tests to automatically reload /public/capybara.html instead of opening it over and over myself.
Problem
For some reason I can only insert the Rack Middleware, which is responsible for reloading the page, into the middleware-stack within the developent-environment, but not within the test-environment. I use the following code for insertion:
/config/environments/development.rb
Rails.application.configure do
config.middleware.insert_after ActionDispatch::Static, Rack::LiveReload
end
When using the same method in /config/environments/test.rb, the following error occurs
myApp/config/environments/test.rb:44:in `block in <top (required)>': uninitialized constant Rack::LiveReload (NameError)
As I am still rather new to Rails, I don't really know where to start here. As far as I know, trying to require the file manually wouldn't really be The Rails WayTM .
So, how can I resolve this problem?
Thanks in advance.
In your Gemfile you are probably only loading the rack-livereload gem in the development group -- for this to work you would need to be loading it in the development and test groups. That being said, you really want your test environment to mimic production as closely as possible, so running rack-livereload in the test environment would usually be a bad idea.
I believe you should include the Rack::LiveReload in your test environment inside Gemfile:
group :development, :test do
gem "rack-livereload"
end

Loading Rails environment and classes from an independent gem

I have an app environment that we validate using an automated script. It populates some setup data in the Rails app and then sends a number of requests to validate some external workflows. Afterwards it removes the setup data from the database and cleans up other associated files.
The problem is I can’t have the scripts (currently rake tasks) in the application code. The script code needs to be independent from the application codebase for regulatory reasons.
Can anyone provide a way to load the Rails environment and classes in the rails application from a gem?
Right now I check the gem is run from the application root folder and then require the Rails environment.
require Dir.pwd + '/config/boot’
The issue is I can’t figure out how to reference the Rails app classes.
I think this is the proper way to load a Rails app (found in config.ru): require ::File.expand_path('../config/environment', __FILE__). In fact you can copy and paste that into a regular irb session to turn it into a 'rails console' session.
So the key seems to be to require your config/environment and that should allow you to use your app models normally.

Load Capistrano's environment in rails console

What's the best way to load the Capistrano environment within the rails console?
I'd like to access the variables and methods capistrano is using in deploys such as latest_release, source etc.
Ruby debug isn't efficient when trying to hack out complicated capistrano tasks.
Within the Rails console is probably not practical as the two environments would likely come into conflict. In an interactive console isn't hard, though. You could add a Capistrano task that simply spins up one:
task :console do
require 'irb'
$config = self
ARGV.clear
IRB.start
end
This exposes the current configuration object as the global $config. Although using a global variable is a bit ugly, I couldn't find a way to switch the default context of IRB from the main object.

Rails 3 Locale switches when using different server

I've got a Rails 3.2.3 app with the default_locale set to :nl. When I start the app using Thin or Unicorn, the app's locale is set to :en. When I use Webrick, the locale is correctly set to :nl.
This change is triggered by a commit that updates several third-party gems, although I have not been able to single out any one gem upgrade in particular -- I can reverse each of them individually and get the same result. However, when I checkout the offending commit's parent, all is well too.
When I run the app on a remote server in production mode, it all works fine, so it seems to be local to my machine.
I have removed every single installed gem and re-installed them all, which made no difference.
Does anyone have any idea what might trigger this behaviour? And especially why using webrick or unicorn would make a difference?
Edit: I have pinpointed the bug to be triggered by upgrading Draper from 0.11 to 0.12 (issue at Github). Not sure if it is also the cause.
http://labs.revelationglobal.com/2009/11/13/unicorn_and_i18n.html
This problem has occured to me before wich was triggered by the "active_admin" gem you might want to use an earlier version to prevent this, I do not really know wich one so you can play around with it a little.
another option would be to set the active_admin locale in a before_filter,
config.before_filter :set_admin_locale
And set_admin_locale is in the application_controller:
def set_admin_locale
I18n.locale = :nl
end
hope it helped
I managed to track this problem down to a bad practice in my own Rails app that caused a bug by upgrading the Draper gem. There's a full explanation in the Draper documentation.

Ruby and/or Rails caching require()'ed scripts?

I'm testing out a basic Rails app and I seem to be getting some undesirable caching behavior on a library script that's being require()'ed into my controller script.
Suppose FooController.rb contains the following:
require 'utils' # a library script
class FooController
def some_action
#some_member = do_something() # global method defined in utils.rb
end
end
It appears that changes to utils.rb do not take effect until I restart the Rails server. I don't believe this is due to a misconfiguration of Rails' class caching, since a) I am running in a "development" environment, and b) I can make changes directly to the controller code (e.g., to the some_action method above) that are reflected upon the next execution of the script. I've been testing this with some calls to puts that spam messages into the console.
Is there some behavior in either Ruby or Rails that would cause require()-ed scripts to remain cached? If so, is there a way to configure that behavior?
Rails class reloader is relatively naive. I believe it's only intended to reload things like controllers and models, leaving anything you might be require'ing into your project alone. So, if you have some custom code in the lib directory or elsewhere that you have changed it is expected behavior for you to have to restart the Rails server.
If you want to require each time the code is encountered, you really want load.
http://www.ruby-doc.org/core/Kernel.html#method-i-load

Resources