I’ve written a simple module to extend ActiveRecord::Base, which works
fine in the rails console but fails miserably in rspec tests. The
intended usage looks like this:
class Premise < ActiveRecord::Base
has_my_extensions
end
I’ve traced it down to the fact that in the console, it loads
lib/my_extensions.rb before it loads premise.rb. In rspec, it’s
attempting to load premise.rb without loading my_extensions.rb, which
(naturally) results in a method_missing error.
(A) has anyone else seen this?
(B) what’s the proper remedy?
Details below the sig. Thanks…
- ff
Some particulars: Ruby 1.9.2. Rails 3.0.3. RSpec 2.4.0.
=== file: $ROOT/config/environment.rb
require File.expand_path(’…/application’, FILE)
Demo::Application.initialize!
require ‘my_extensions’
=== EOF
=== file: $ROOT/app/models/premise.rb
$stderr.puts(Rails.logger.debug("==== loading #{FILE}"))
class Premise < ActiveRecord::Base
has_my_extensions
def test_one
extension_one
end
end
=== EOF
=== file: $ROOT/lib/my_extensions.rb hews to a familiar pattern:
$stderr.puts(Rails.logger.debug("==== loading #{FILE}"))
module MyExtensions
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def has_my_extensions
send(:include, InstanceMethods)
end
end
module InstanceMethods
def extension_one
$stderr.puts(Rails.logger.debug("==== extension_one"))
end
end
end
ActiveRecord::Base.send(:include, MyExtensions)
=== EOF