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
- RAILS_GEM_VERSION = ‘2.3.2’ unless defined? RAILS_GEM_VERSION
- require File.join(File.dirname(FILE), ‘boot’)
- Rails::Initializer.run do |config|
- config.action_controller.session_store = :active_record_store
- config.gem ‘mislav-will_paginate’, :version => ‘~> 2.3.8’, :lib
=> ‘will_paginate’, - :source => ‘http://gems.github.com’
- end
-
Include your application configuration below
- require ‘acts_as_ferret’
- require ‘will_paginate’
- DB_STRING_MAX_LENGTH = 255
- DB_TEXT_MAX_LENGTH = 40000
- HTML_TEXT_FIELD_SIZE = 15
test_helper:
Code : - fold - unfold
- ENV[“RAILS_ENV”] = “test”
- require File.expand_path(File.dirname(FILE) +
“/…/config/environment”) - require ‘test_help’
- class ActiveSupport::TestCase
- …
- end
faq_test.rb:
Code : - fold - unfold
- require File.dirname(FILE) + ‘/…/test_helper’
- class FaqTest < ActiveSupport::TestCase
- fixtures :faqs
- def setup
-
@valid_faq = faqs(:valid_faq)
- end
- def test_max_lengths
-
Faq::QUESTIONS.each do |question|
-
assert_length :max, @valid_faq, question, DB_TEXT_MAX_LENGTH
-
end
- end
- end
faq.rb:
Code : - fold - unfold
- class Faq < ActiveRecord::Base
- belongs_to :user
- acts_as_ferret
- QUESTIONS = %w(bio skillz schools companies
-
music movies television books magazines)
-
A constant for everything except the bio
- FAVORITES = QUESTIONS - %w(bio)
- TEXT_ROWS = 10
- TEXT_COLS = 40
- validates_length_of QUESTIONS,
-
:maximum => DB_TEXT_MAX_LENGTH
- def initialize
-
super
-
QUESTIONS.each do |question|
-
self[question] = ""
-
end
- end
- 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
- ibookG4:rails_space Beo$ rake test:units
- (in /Users/Beo/Documents/Mesh/rails_space)
- /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”
/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
- ibookG4:rails_space Beo$ gem list
- *** LOCAL GEMS ***
- actionmailer (2.3.2)
- actionpack (2.3.2)
- activerecord (2.3.2)
- activeresource (2.3.2)
- activesupport (2.3.2)
- acts_as_ferret (0.4.4)
- ferret (0.11.6)
- mislav-will_paginate (2.3.11)
- mysql (2.7)
- rails (2.3.2)
- rake (0.8.7)
- 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.