Where is the best place load a YAML application config file? - ruby-on-rails-3

I have setup my application configuration for my rails 3.1 application as described here by Ryan Bates.
Problem is I want to use the config in my environment file for username/password for ActionMailer, but Ryan suggests loading the config from an initializer, and they seem to get included after environment.rb. Where is the best place to load the configuration file so it can be accessed by the whole rails application?
Thanks

In that case, this code should go in as a pre-initializer. As of Rails 3, all pre-initialization code needs to go near the top of application.rb, just before the line require 'rails/all'
application.rb:
require File.expand_path('../boot', __FILE__)
# load app_config.yml
require 'yaml'
APP_CONFIG = YAML.load(File.read(File.expand_path('../app_config.yml', __FILE__)))
require 'rails/all'

Related

Unexpected require in my config/application.rb

I have an issue with the following line in the config/application.rb file of my generated spec/dummy rails application:
# railtie requires ...
Bundler.require(*Rails.groups)
require "tm_app_core" # This line should not exists according to rails application.rb template
module Dummy
class Application < Rails::Application
# ...
I am using Rails::Generators::AppGenerator to generate my dummy app. But according to Rails application.rb template it should not have the following line:
require "tm_app_core"
Where does this line comes from?
I'm using Rails 3.2.13. tm_app_core is the name of my root directory (will be different on my CI server for example).

cucumber/capybara undefined method `visit'

I have a bundler application to perform cucumber tests for two of my applications named as "validation" and "recruiters". The directory structure of my test application is as follows:
root-folder
|_features
|_recruiters
|_recruitment_navigation.feature
|_step_definitions
|_recruitment_navigation_steps.rb
|_validation
|_FEATURE FILES
|_step_definitions
|_DEFINITION STEPS
|_support
|_env.rb
For the above directory structure. undefined method visit for #<Object:0x870c080> (NoMethodError) for every test which requires page visit.
But for the following directory structure, the tests are working fine:
root-folder
|_features
|_FEATURE FILES
|_step_definitions
|_DEFINITION STEPS
|_support
|_env.rb
Here is the env.rb file:
require 'capybara'
require 'capybara/dsl'
require 'capybara/cucumber'
require 'rspec/expectations'
require "ruby-debug"
Capybara.app_host = ENV["host"]
Capybara.run_server = false
Capybara.default_driver = :selenium
World(Capybara)`enter code here`
Please help me with this. Is there any different kind of setup for Capybara that is required for first type of directory structure?
You will need to tell cucumber to correctly require the features directory when running features in a subfolder.
e.g.
cucumber host=<host-url> -r features features/recruiters/<feature file to be tested>
Should work I think.

rails activeadmin errors rspec

my database is MongoDB and I use ActiveAdmin.
before i add gem 'activeadmin-mongoid', :git => 'git://github.com/simplybusiness/activeadmin-mongoid.git' rspec pass every test with mongoDB, but after that rspec get an errors
ActiveRecord::ConnectionNotEstablished
file config/application.rb I repalce
require all with
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
I removed any activerecord config in folder config . I follow that link
sorry about my bad english.
Any help would be appreciated. Thanks!

rails3 with compass-rails

I'm trying to set up a rails 3.2 app with the new compass-rails gem
https://github.com/Compass/compass-rails
I want to compile the files in the assets folder with the compass watch command—as I want to deploy to heroku, but so far i'm unsuccessful.
The compass-rails gem page states:
Developing with the Compass watcher
When using the Compass watcher to update your stylesheets, your stylesheets are recompiled as soon as you save your Sass files. In this mode, compiled stylesheets will be written to your project's public folder and therefore will be served directly by your project's web server -- superceding the normal rails compilation.
In this mode, rails 3.0 or earlier users will experience a slight speed up by disabling the Sass::Plugin like so:
config.after_initialize do
Sass::Plugin.options[:never_update] = true
end
But i don't get where to put this config options
Any idea?
==EDIT==
I tried to add the config.after_initialize block in my application.rb
require File.expand_path('../boot', __FILE__)
# require 'rails/all'
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
# require "sprockets/railtie"
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module Salsacaribecouk
class Application < Rails::Application
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
config.after_initialize do
Sass::Plugin.options[:never_update] = true
end
end
end
but when i run the rails server I get an error message:
block in <class:Application>': uninitialized constant Sass::Plugin (NameError)
The config.after_initialize should be put in config\application.rb if you want it to be executed in all environments, or in config\environments\[development|test|production].rb to executed in a specific environment.

Rails 3: how to load files in /lib?

I'm new to rails and making some sort of noob mistake: I frequently need to count the number of lines in a file, so I'm trying to monkey patch class File like this:
class File
def self.line_count( filename )
%x{wc -l #{filename}}.split.first.to_i
end
end
I saved this to /lib/file_util.rb. I thought that this was supposed to be auto-required, so that I could just use it, but that doesn't work:
$ rails console
>> File.line_count('Gemfile')
NoMethodError: undefined method `line_count' for File:Class
...
So I try to require it manually, no joy:
>> require '<myproj>/lib/file_util.rb' # same result with require 'file_util.rb'
=>nil
But it works if I require it within IRB:
$ irb
>> require '<myproj>/lib/file_util.rb'
=> true
>> File.line_count('Gemfile')
=> 22
I also tried to to add the require to config/application.rb:
...
Bundler.require(:default, Rails.env) if defined?(Bundler)
require 'file_util.rb'
module <myproj>
...
and I get:
$ rails console
<myproj>/config/application.rb:9:in `require': no such file to load -- file_util.rb (LoadError)
What am I doing wrong?
Ok, I seem to have mostly figured it out. Rails doesn't automatically require everything under /lib. It only auto loads when you try to use a new class name that matches a file name in lib. So if I define line_count in class FileUtil instead of File it automatically finds and loads 'file_util.rb'. But patching File and naming the patch file 'file.rb' doesn't work, since the File class is already defined, so Rails doesn't go looking for a definition.
My other problem was that I was trying to do the require too soon in the startup sequence, before Rails had a chance to enhance require to look in its directories. When I added "require 'file_util'" to config/environments/development.rb it works fine.
But this doesn't explain why I can't manually require the file from within rails console.
Monkeypatching classes can be done more easily by adding a file in config/initializers. All of those files are automatically loaded by Rails at startup.
You can call your initializer file anything you want. Try config/initializers/file.rb.