My code must parse Vcard 2.1 format.
I am using vpim(there are no other libs)
When I run Vpim::Vcard.decode(data) I get error:
undefined method `each' for #<String:0x0000000928e778>
StackTrace:
NoMethodError (undefined method `each' for #<String:0x0000000928e778>):
vpim (0.695) lib/vpim/rfc2425.rb:82:in `unfold'
vpim (0.695) lib/vpim/rfc2425.rb:308:in `decode'
vpim (0.695) lib/vpim/vcard.rb:692:in `decode'
app/models/event.rb:71:in `block (2 levels) in parse_data'
I tryed to run gem install vcard and to require 'vcard' directry(config.gem does not include this file after vpim gem) after RailsApp initialization.(config.gem 'vpim' is included in environment.rb)
By this way I get another error:
;=D0=9D=D0=B0=D0=B4=D1=80=D0=B0=20=D0=B1=D0=B0=D0=BD=D0=BA=20=D0=BE=D0=BB=
(Exception class is Vpim::InvalidEncodingError)
Vcard code I try parse:
BEGIN:VCARD
VERSION:2.1
REV:20090710T151929Z
TEL;CELL:80954130722
X-CLASS:private
END:VCARD
It is interesting that second error is when I am decoding inside a Rails model.
When I try to decode directly from script/console (after installinv vcard gem and direct including 'vcard') I succesfuly get Vcard object.
StackTrace:
Vpim::InvalidEncodingError (;=D0=9D=D0=B0=D0=B4=D1=80=D0=B0=20=D0=B1=D0=B0=D0=BD=D0=BA=20=D0=BE=D0=BB=):
vcard (0.1.1) lib/vcard/field.rb:106:in `decode0'
vcard (0.1.1) lib/vcard/field.rb:172:in `initialize'
vcard (0.1.1) lib/vcard/field.rb:183:in `new'
vcard (0.1.1) lib/vcard/field.rb:183:in `decode'
vcard (0.1.1) lib/vcard/rfc2425.rb:308:in `block in decode'
vcard (0.1.1) lib/vcard/rfc2425.rb:308:in `collect'
vcard (0.1.1) lib/vcard/rfc2425.rb:308:in `decode'
vcard (0.1.1) lib/vcard/vcard.rb:686:in `decode'
app/models/event.rb:71:in `block (2 levels) in parse_data'
app/models/event.rb:71 :
vcard = Vpim::Vcard.decode(contact.text)
Here is axample from irb:
95-25-164-74:~ smix$ irb
ruby-1.9.2-rc2 > str = <<EOS
ruby-1.9.2-rc2"> BEGIN:VCARD
ruby-1.9.2-rc2"> VERSION:2.1
ruby-1.9.2-rc2"> REV:20090710T151929Z
ruby-1.9.2-rc2"> TEL;CELL:80954130722
ruby-1.9.2-rc2"> X-CLASS:private
ruby-1.9.2-rc2"> END:VCARD
ruby-1.9.2-rc2"> EOS
=> "BEGIN:VCARD\nVERSION:2.1\nREV:20090710T151929Z\nTEL;CELL:80954130722\nX-CLASS:private\nEND:VCARD\n"
ruby-1.9.2-rc2 > require 'vpim'
=> true
ruby-1.9.2-rc2 > Vpim::Vcard.decode str
NoMethodError: undefined method `each' for #<String:0x000001010e0428>
from /Users/smix/.rvm/gems/ruby-1.9.2-rc2/gems/vpim-0.695/lib/vpim/rfc2425.rb:82:in `unfold'
from /Users/smix/.rvm/gems/ruby-1.9.2-rc2/gems/vpim-0.695/lib/vpim/rfc2425.rb:308:in `decode'
from /Users/smix/.rvm/gems/ruby-1.9.2-rc2/gems/vpim-0.695/lib/vpim/vcard.rb:692:in `decode'
from (irb):10
from /Users/smix/.rvm/rubies/ruby-1.9.2-rc2/bin/irb:17:in `<main>'
ruby-1.9.2-rc2 >
How is it possible to parse Vcard 2.1 in rails?
The problem is that String#each has been removed in Ruby 1.9.1. (Its functionality endures in String#each_line). To fix/work around this bug, do any of the following:
Run your rails app on Ruby 1.8.7.
You can edit the Vpim code to use String#each_line appropriately (and send the vpim authors a patch)
You can monkey-patch the String class by defining the each method
class String
alias_method :each, :each_line
end
Switch to the vcard gem, which is taken from Vpim and which has Ruby 1.9.1 support.
I recommend the 4th option.
Related
Can't seem to read a serialized column after upgrading to Rails 5.0 stable.
class PaymentNotification < ActiveRecord::Base
serialize :raw_IPN
...
end
a raw_IPN in the database looks like:
--- !ruby/hash:ActionController::Parameters
address_status: confirmed
subscr_date: 12:42:39 Dec 20, 2015 PST
payer_id: 123
address_street: 1 Main St
mc_amount1: '6.00'
mc_amount3: '12.00'
charset: windows-1252
address_zip: '123'
first_name: Test
reattempt: '1'
address_country_code: US
address_name: Test Buyer
notify_version: '3.8'
subscr_id: I-123123123
custom: '2'
payer_status: verified
business: payments-facilitator#test.com
address_country: United States
address_city: San Jose
verify_sign: 123-12312312321312.2JohI99Xp56cvu9zc
payer_email: payments-buyer#test.com
btn_id: '123'
last_name: Buyer
address_state: CA
receiver_email: test#email.com
recurring: '1'
txn_type: subscr_signup
item_name: asd
mc_currency: EUR
item_number: asd
residence_country: US
test_ipn: '1'
period1: 12 M
period3: 12 M
ipn_track_id: 111
controller: payment_notifications
action: create
The error: (testing in the console)
p = PaymentNotification.last
PaymentNotification Load (0.1ms) SELECT `payment_notifications`.* FROM `payment_notifications` ORDER BY
`payment_notifications`.`id` DESC LIMIT 1
NoMethodError: undefined method `[]=' for nil:NilClass
Stacktrace:
actionpack (5.0.0) lib/action_controller/metal/strong_parameters.rb:414:in `[]='
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:362:in `block in revive_hash'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:336:in `revive_hash'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:281:in `visit_Psych_Nodes_Mapping'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/visitor.rb:16:in `visit'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/visitor.rb:6:in `accept'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:32:in `accept'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:311:in `visit_Psych_Nodes_Document'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/visitor.rb:16:in `visit'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/visitor.rb:6:in `accept'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:32:in `accept'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych/nodes/node.rb:38:in `to_ruby'
/home/frexuz/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych.rb:253:in `load'
activerecord (5.0.0) lib/active_record/coders/yaml_column.rb:24:in `load'
activerecord (5.0.0) lib/active_record/type/serialized.rb:18:in `deserialize'
activerecord (5.0.0) lib/active_record/attribute.rb:140:in `type_cast'
activerecord (5.0.0) lib/active_record/attribute.rb:38:in `value'
activerecord (5.0.0) lib/active_record/attribute_set.rb:42:in `fetch_value'
activerecord (5.0.0) lib/active_record/attribute_methods/read.rb:66:in `_read_attribute'
activerecord (5.0.0) lib/active_record/attribute_methods/read.rb:36:in `__temp__271677f59405e4'
app/admin/payment_notifications.rb:17:in `block (3 levels) in <top (required)>'
This issue is fixed in the 5-0-stable branch.
Solution:
gem 'rails', github: "rails/rails", branch: '5-0-stable'
and bundle update rails until they release a new gem version.
I had a test case of the following form:
require 'test/unit'
class SomeTests < Test::Unit::TestCase
def test_should_do_action
assert true
end
end
and have re-written it using must as suggested in the book A Test::Unit Trick to Know About:
require 'test/unit'
class SomeTests < Test::Unit::TestCase
must "do action" do
assert true
end
end
And when I run it, I get an undefined method 'must' error shown as follows:
SomeTests.rb:3:in `<class:SomeTests>': undefined method `must' for SomeTests:Class (NoMethodError) from
SomeTests.rb:2:in `<top (required)>' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:9:in `each' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:9:in `block in <main>' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:4:in `select' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:4:in `<main>' rake aborted! Command failed with status (1): [ruby -w -I"lib" -I"/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0" "/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb" "test/**/*Tests.rb" ]
Tasks: TOP => default => test (See full trace by running task with --trace)
I thought that must might be a part of minitest, so I required 'minitest/unit' instead, but I still get an error. I also assume that must keyword isn't part of rspec, which I'm not using yet.
How do I get this to work properly?
It looks like that method is not provided out of the box, but was developed by a third party. You need to add code described here.
Upgrade steps were performed as mentioned in http://guides.rubyonrails.org/3_2_release_notes.html
Right now I'm on Ruby 1.8.7 and Rails 3.1.x (using system ruby). I installed RVM with ruby 1.8.7 and added to rails 3.2 and then I get the following error:
[app]$ rails console
Faraday: you may want to install system_timer for reliable timeouts
$HOME/src/qbol/tapp/config/environment.rb:16:in `add': undefined method `>' for nil:NilClass (NoMethodError)
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/tagged_logging.rb:55:in `add'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/tagged_logging.rb:61:in `info'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activerecord-3.2.17/lib/active_record/railtie.rb:86
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:26:in `on_load'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:25:in `each'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:25:in `on_load'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activerecord-3.2.17/lib/active_record/railtie.rb:80
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:30:in `instance_exec'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:30:in `run'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:55:in `run_initializers'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:54:in `each'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:54:in `run_initializers'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/application.rb:136:in `initialize!'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/railtie/configurable.rb:30:in `send'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/railtie/configurable.rb:30:in `method_missing'
from $HOME/src/app/config/environment.rb:48
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:251:in `require'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:251:in `require'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:236:in `load_dependency'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:251:in `require'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/application.rb:103:in `require_environment!'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/commands.rb:40
from script/rails:6:in `require'
from script/rails:6
This is on my development box. Any idea why this is happening?
EDIT: Below is the environment.rb. The error is happening on the last line App::Application.initialize!
# Load the rails application
require File.expand_path('../application', __FILE__)
module ActiveSupport
class BufferedLogger
def self.current_user
Thread.current[:user]
end
def self.current_user=(user)
Thread.current[:user] = user
end
def add(severity, message = nil, progname = nil, &block)
return if #level > severity
message = (message || (block && block.call) || progname).to_s
level = {
0 => "DEBUG",
1 => "INFO ",
2 => "WARN ",
3 => "ERROR",
4 => "FATAL"
}[severity] || "UNKNOWN"
user=BufferedLogger.current_user
if(!user.nil?)
idstr = "uid:#{user.id}"
if !user.current_app_user.nil?
idstr.concat(", acid: #{user.current_app_user.account_id}")
end
else
idstr=""
end
message = "[%s: %s #{idstr}] %s" %
["#{level} pid: #{$$}", Time.now.strftime("%y-%m-%d %H:%M:%S"), message]
message = "#{message}\n" unless message[-1] == ?\n
buffer << message
auto_flush
message
end
end
end
# Initialize the rails application
App::Application.initialize!
The error is happening inside ActiveSupport::BufferedLogger#add method call and you appear to be monkey-patching this class. The internals of ActiveSupport::BufferedLogger have likely changing between the versions of Rails you're using.
Try removing all of the ActiveSupport::BufferedLogger code from your environment.rb to work past this error and get your app running. Then, if you still need the monkeypatch (don't know why), you'd have to rewrite it on top of the newer version of the class.
When I try this with roo gem:
irb(main):001:0> require 'roo'
=> true
irb(main):002:0> oo = Excel.new("C:/Users/Abash/Desktop/test1.xls")
**NameError: uninitialized constant Excel** from (irb):2
from C:/Ruby193/bin/irb:12:in `<main>'
When I try this with spreadsheet gem:
irb(main):001:0> require 'spreadsheet'
=> true
irb(main):002:0>Spreadsheet.client_encoding = 'UTF-8'
=> "UTF-8"
irb(main):003:0> book = Spreadsheet.open 'C:/Users/Abash/Desktop/test1.xls'
**Errno::EACCES: Permission denied** - C:/Users/Abash/Desktop/test1.xls
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.8.2/lib/spreadsheet.rb:69:in `initialize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.8.2/lib/spreadsheet.rb:69:in `open'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.8.2/lib/spreadsheet.rb:69:in `open'
from (irb):3
from C:/Ruby193/bin/irb:12:in `<main>'
Can somebody show me a work around for these errors?
I believe you are using ruby 1.9 or above version. In such a case,you need to specify the gem while creating a new instance.
require 'roo'
s =Roo::Excel.new("myspreadsheet.xls")
s =Roo::Excelx.new("myspreadsheet.xlsx")
I can not figure out how to get spork not to load all of my app models. Testing changes to my models is greatly slowed down as I am unable to use spork to help. This is what I get when I debug what spork is loading:
- Spork Diagnosis -
-- Summary --
app/models/account.rb
app/models/admin.rb
app/models/affiliate.rb
app/models/app.rb
app/models/application_server.rb
app/models/domain_record.rb
app/models/domain_zone.rb
app/models/event.rb
app/models/oid.rb
app/models/user.rb
config/application.rb
config/boot.rb
config/environment.rb
config/environments/test.rb
config/initializers/api_conversions.rb
config/initializers/backtrace_silencers.rb
config/initializers/compass.rb
config/initializers/devise.rb
config/initializers/hoptoad.rb
config/initializers/inflections.rb
config/initializers/mime_types.rb
config/initializers/rspec_generator.rb
config/initializers/secret_token.rb
config/initializers/session_store.rb
config/routes.rb
lib/application_server_api.rb
lib/oid_helper.rb
lib/soa_record.rb
spec/spec_helper.rb
-- Detail --
--- app/models/account.rb ---
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `block in require'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:227:in `load_dependency'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:346:in `require_or_load'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:300:in `depend_on'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:216:in `require_dependency'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:54:in `load_model'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:17:in `block (2 levels) in load_models'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:16:in `each'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:16:in `block in load_models'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/paths.rb:102:in `block in each'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/paths.rb:102:in `each'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/paths.rb:102:in `each'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:15:in `load_models'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/mongoid/railtie.rb:88:in `block (2 levels) in '
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:419:in `_run_prepare_callbacks'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/callbacks.rb:40:in `initialize'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:33:in `new'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:33:in `build'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `block in build'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `each'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `inject'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `build'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:162:in `app'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application/finisher.rb:35:in `block in '
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `instance_exec'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `run'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:50:in `block in run_initializers'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `each'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `run_initializers'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:134:in `initialize!'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
config/environment.rb:8:in `'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `block in require'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `block in load_dependency'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
spec/spec_helper.rb:9:in `block in '
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/spork-0.9.0.rc3/lib/spork.rb:23:in `prefork'
spec/spec_helper.rb:4:in `'
update: my spec_helper.rb
require 'rubygems'
require 'spork'
ENV["RAILS_ENV"] = "test"
Spork.prefork do
require File.expand_path(File.dirname(__FILE__) + '/../config/environment')
require 'rspec/rails'
end
Spork.each_run do
# Hub::Application.reload_routes!
end
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
If I move require File.expand_path(File.dirname(__FILE__) + '/../config/environment') out of the prefork, my models are not preloaded, but as you might expect it takes much longer to run my tests.
Any ideas on how to get spork to not load models on a Rails 3 app? I have tried using spork 0.8.4, master branch, and currently 0.9.0.rc3. Does anyone have any ideas how I can avoid having the models preloaded?
After much time spinning my wheels I finally have something that is acceptable:
Spork.prefork do
require "rails/mongoid"
Spork.trap_class_method(Rails::Mongoid, :load_models)
# The following does not work correctly with Devise's routes that load the User model.
# Results in NameError unintitialized *
# :reload_routes! triggers :devise_for which loads and caches the User class.
# https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu
# require "rails/application"
# Spork.trap_method(Rails::Application, :reload_routes!)
require 'factory_girl_rails'
Spork.trap_class_method(Factory, :find_definitions)
require File.expand_path(File.dirname(__FILE__) + '/../config/environment')
end
The problem was mostly due to Rails::Mongoid#load_models. After inspecting the debug output of spork and the backtrace of how things are loaded, some clues are provided as to what is loading the models. This page goes into a bit more detail https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu, but I do not find the :reload_routes! helping with Devise forcing the User class to be preloaded.
My specs are running much faster now. If only I could get my User model to not be cached I would be in bliss, but until then, I shall be mostly satisfied.
I have the following setup in both RSpec and Cucumber on Rails 3.1 and models are refreshing:
Set factory_girls_rails require => false in your Gemfile
gem 'factory_girl_rails', :require => false
Replace the Spork.prefork and Spork.each_run blocks with the following code.
if Spork.using_spork?
ActiveSupport::Dependencies.clear
ActiveRecord::Base.instantiate_observers
Spork.prefork do
end
Spork.each_run do
require 'factory_girl_rails'
# reload all the models
Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
load model
end
# reload routes
Rails.application.reload_routes!
end
end
The following solution suggested here works for me. It also reloads the User model.
# env.rb
Spork.each_run do
require 'factory_girl_rails'
# reload all the models
Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
load model
end
end
This solution works for me (from http://my.rails-royce.org/2011/03/17/rspec-2-and-fixtures/)
Spork.prefork do
require 'rails/application'
Spork.trap_method(Rails::Application, :reload_routes!)
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
Spork.trap_method(Rails::Application, :eager_load!)
require File.expand_path("../../config/environment", __FILE__)
Rails.application.railties.all { |r| r.eager_load! }
require 'rspec/rails'
RSpec.configure do |config|
...
end
end
Note that doing a "load model" in each_run is only a partial solution, this will re-evaluate the model code, but for instance after removing a model method it will still exist in memory until you restart Spork.