Inside or outside of Rails::Initializer block

Please help me figure out whether to put the application configuration
below inside or outside of the Rails::Initializer in environment.rb.
The application runs regardless of the position of the application
configuration. However, a unit test fails if the application
configuration is outside the block but will pass if the configuration is
inside the block. Below are the code.

environment.rb:
Code : - fold - unfold

   1. RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
   2. require File.join(File.dirname(__FILE__), 'boot')
   3.
   4. Rails::Initializer.run do |config|
   5.
   6. config.action_controller.session_store = :active_record_store
   7.
   8. config.gem 'mislav-will_paginate', :version => '~> 2.3.8', :lib
=> 'will_paginate',
   9. :source => 'http://gems.github.com'
  10.
  11. end
  12.
  13. # Include your application configuration below
  14. require 'acts_as_ferret'
  15. require 'will_paginate'
  16.
  17. DB_STRING_MAX_LENGTH = 255
  18. DB_TEXT_MAX_LENGTH = 40000
  19. HTML_TEXT_FIELD_SIZE = 15

test_helper:
Code : - fold - unfold

   1. ENV["RAILS_ENV"] = "test"
   2. require File.expand_path(File.dirname(__FILE__) +
"/../config/environment")
   3. require 'test_help'
   4.
   5. class ActiveSupport::TestCase
   6. ...
   7. end

faq_test.rb:
Code : - fold - unfold

   1. require File.dirname(__FILE__) + '/../test_helper'
   2.
   3. class FaqTest < ActiveSupport::TestCase
   4. fixtures :faqs
   5.
   6. def setup
   7. @valid_faq = faqs(:valid_faq)
   8. end
   9.
  10. def test_max_lengths
  11. Faq::QUESTIONS.each do |question|
  12. assert_length :max, @valid_faq, question, DB_TEXT_MAX_LENGTH
  13. end
  14. end
  15. end

faq.rb:
Code : - fold - unfold

   1. class Faq < ActiveRecord::Base
   2. belongs_to :user
   3. acts_as_ferret
   4.
   5. QUESTIONS = %w(bio skillz schools companies
   6. music movies television books magazines)
   7. # A constant for everything except the bio
   8. FAVORITES = QUESTIONS - %w(bio)
   9. TEXT_ROWS = 10
  10. TEXT_COLS = 40
  11.
  12. validates_length_of QUESTIONS,
  13. :maximum => DB_TEXT_MAX_LENGTH
  14.
  15. def initialize
  16. super
  17. QUESTIONS.each do |question|
  18. self[question] = ""
  19. end
  20. end
  21. end

When I run the unit test, with the application configuration outside the
initializer block as above, I get the error that none of the variable
outside the block is defined. If I move the "require" lines inside the
block, the error will say that the constants are not defined, etc. (The
line numbers in the error messages below may not be the same as the code
line numbers above):

Code : - fold - unfold

   1. ibookG4:rails_space Beo$ rake test:units
   2. (in /Users/Beo/Documents/Mesh/rails_space)
   3. /opt/local/bin/ruby -I"lib:test"
"/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
"test/unit/faq_test.rb" "test/unit/helpers/community_helper_test.rb"
"test/unit/helpers/faq_helper_test.rb"
"test/unit/helpers/profile_helper_test.rb"
"test/unit/helpers/site_helper_test.rb"
"test/unit/helpers/spec_helper_test.rb"
"test/unit/helpers/user_helper_test.rb" "test/unit/spec_test.rb"
"test/unit/user_test.rb"
   4.
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1964:in
`method_missing_without_paginate': undefined local variable or method
`acts_as_ferret' for #<Class:0x1a52430> (NameError)
   5. from
/opt/local/lib/ruby/gems/1.8/gems/mislav-will_paginate-2.3.11/lib/will_paginate/finder.rb:170:in
`method_missing'
   6. from /Users/Beo/Documents/Mesh/rails_space/app/models/faq.rb:3
   7. from
/opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
   8. from
/opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
   9. from
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:158:in
`require'
  10. from
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:265:in
`require_or_load'
  11. from
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:224:in
`depend_on'
  12. from
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:136:in
`require_dependency'
  13. from
/opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:393:in
`load_application_classes'
  14. from
/opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:392:in
`each'
  15. from
/opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:392:in
`load_application_classes'
  16. from
/opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:390:in
`each'
  17. from
/opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:390:in
`load_application_classes'
  18. from
/opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:195:in
`process'
  19. from
/opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:113:in
`send'
  20. from
/opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:113:in
`run'
  21. from
/Users/Beo/Documents/Mesh/rails_space/config/environment.rb:9
  22. from ./test/unit/../test_helper.rb:2:in `require'
  23. from ./test/unit/../test_helper.rb:2
  24. from ./test/unit/faq_test.rb:1:in `require'
  25. from ./test/unit/faq_test.rb:1
  26. from
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in
`load'
  27. from
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5
  28. from
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in
`each'
  29. from
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5
  30. rake aborted!
  31. Command failed with status (1): [/opt/local/bin/ruby -I"lib:test"
"/opt/loc...]
  32.
  33. (See full trace by running task with --trace)

I am running the following gems. My plugins folder is empty:

Code : - fold - unfold

   1. ibookG4:rails_space Beo$ gem list
   2.
   3. *** LOCAL GEMS ***
   4.
   5. actionmailer (2.3.2)
   6. actionpack (2.3.2)
   7. activerecord (2.3.2)
   8. activeresource (2.3.2)
   9. activesupport (2.3.2)
  10. acts_as_ferret (0.4.4)
  11. ferret (0.11.6)
  12. mislav-will_paginate (2.3.11)
  13. mysql (2.7)
  14. rails (2.3.2)
  15. rake (0.8.7)
  16. rubygems-update (1.3.3)

You may say that I should just move them all inside the block and be
done with it. After all, the application runs fine with those inside
the block. However, according to the book "RailsSpace" that I am
following, those app config should be outside the block. I've read also
that the require lines for acts_as_ferret and will_paginate should be
outside the block. I am afraid that if I leave them inside the block, I
may have some problems down the road. Please help. Thanks.

The book is out of date. Seriously out of date, in fact. (built on
Rails 1.2.3) So much has changed since then that I'd recommend getting
almost *anything* more modern.

You should replace the call to "require 'acts_as_ferret'" to a
config.gem statement, much like the one for mislav-will_paginate that
you've already got. You can also remove the "require 'will_paginate'"
part, as the config.gem statement does it for you.

Finally, the current best practice for setting constants is to put
them in a file in config/initializers.

Once again, I can't over-stress the need to get a more modern
reference for learning Rails. While the online source code has been
somewhat updated, here's a short list (took less than 5 minutes) of
issues:

- views as .rhtml rather than .html.erb
- old-style tests in many places (derived from Test::Unit::TestCase,
vs ActiveSupport::TestCase and friends); these will fail now
- spends a lot of time rolling its own authentication system; your
time would be better spent picking a good plugin
- also rolls its own file upload
- very few named routes

--Matt Jones

Matt,

Thank you very much for your valuable advices. I will definitely need
to check out the latest good book on Rails. Which one would you
recommend?

I also have discovered the wonder of plugins. Which plugins would you
recommend for user authentication, file uploads and other common tasks
for a social networking site?

I am trying to build a social networking site. That's why the
step-by-step instructions in RailsSpace are so convenient albeit sorely
out-of-date. I have been spending quite a bit of time debugging and
updating the code to Rails 2.3.2. Any suggestions would be greatly
appreciated.

Thank you.

For documentation, a good place to start is on the main Rails site -
http://rubyonrails.org/documentation . The Rails Guides can be a
helpful source, and they are typically one of the most up-to-date
places for info.

Book-wise, the discussion of "which book is the best" could fill a
whole thread of its own. I got started with the first edition of Agile
Web Development with Rails, so I'd lean towards recommending the
current 3rd edition of that. Whatever you pick, try to avoid anything
published before 2008, as Rails has been evolving rapidly lately.

As far as plugins, there's simply too many different use cases to
recommend any particular one over all the others. Github's search
function can sometimes help here. Whatever you find, pay attention to
the commit history; nothing worse than getting stuck with a dead
plugin.

--Matt Jones