bcardarella / valid_attribute

Minimalist validation BDD for ActiveModel specs
178 stars 18 forks source link

Rails 2.3 compatibility #15

Closed rodrigoflores closed 12 years ago

rodrigoflores commented 12 years ago

Hi

I'm having some issues using valid_attribute on a Rails 2.3 project.

I'm almost sure that the problem is this, because RSpec 1 does defines Spec not RSpec:

if defined?(RSpec)
  require 'valid_attribute/rspec'
elsif defined?(MiniTest::Matchers)
  require 'valid_attribute/minitest'
else
  require 'valid_attribute/test_unit'
end

I'm working on sending a pull request to solve it but I don't know how to include ValidAttribute::Method on RSpec 1 matchers.

Do you have any idea how to do that ?

bcardarella commented 12 years ago

Can you try master? I just pushed: https://github.com/bcardarella/valid_attribute/commit/74d71cd3a1848d3e1bbf9e446eb74dbf9389c688

but it is just a guess from what I recall on how RSpec 1.x worked.

If it doesn't work please let me know and I reopen.

rodrigoflores commented 12 years ago

@bcardarella, thanks for the quick reply :)

Tried master and I got this error:

'undefined method `when' for #<Spec::Matchers::Has:0x110c98590>
./spec/models/blog_spec.rb:13:

But I guess I know what is causing this issue: RSpec is not including the matchers. If you add config.include ValidAttribute::Method to the Spec::Runner.configure do |config| block, it works. To solve this, is only a matter of writing a piece of code to RSpec 1 equivalent to Rspec 2:

module RSpec::Matchers
  include ValidAttribute::Method
end
bcardarella commented 12 years ago

Try pulling from the latest master. I just confirmed the fix.

rodrigoflores commented 12 years ago

Pulled from master and got the same problem :/

bcardarella commented 12 years ago

Can you uninstall the gem (all versions) and try running rake gems:install again? I did a force push and that may be causing an issue. I just tested with a Rails 2.3.14 app and RSpec 1.3.2 and it is working OK on my end.

et commented 12 years ago

@bcardarella - I am receiving the same problem as @rodrigoflores.

I am running rails 2.3.11 with the following gems.

➜  foo git:(master) ✗ bundle list
Gems included by the bundle:
  * activemodel (3.1.3)
  * activerecord (3.1.3)
  * activesupport (3.1.3)
  * arel (2.2.1)
  * awesome_print (1.0.1)
  * builder (3.0.0)
  * bundler (1.0.21)
  * factory_girl (1.3.3)
  * faker (1.0.1)
  * hpricot (0.8.5)
  * i18n (0.6.0)
  * multi_json (1.0.4)
  * mysql (2.8.1)
  * rack (1.1.2)
  * rake (0.9.2)
  * rspec (1.3.2)
  * rspec-rails (1.3.4)
  * scoped_search (2.3.6)
  * systemu (2.4.1)
  * timecop (0.3.5)
  * tzinfo (0.3.31)
  * valid_attribute (1.2.0 2ad0275)
  * wirble (0.1.3)
bcardarella commented 12 years ago

Why are you running Rails 2 with Rails 3 components?

et commented 12 years ago

Interesting, I didn't even notice that. For the project I'm on, we actually package rails in the vendor directory, so I'm not sure why Rails 3 stuff is getting included in there. Here is the Gemfile and Gemfile.lock generated -- https://gist.github.com/1443794

bcardarella commented 12 years ago

It looks like scoped_search is the culprit as it is installing activerecord >= 2.x

That being said, this shouldn't cause the issue you're having with valid_attribute

Can you try this: open up the rails console with the following:

script/server test

that should give you the test environment. Then see if either of the two constants are available:

RSpec & Spec

If RSpec is available this could be causing the issue.

bcardarella commented 12 years ago

btw, to fix the problem with the rails 3 components mixed in you can add the following to your Gemfile:

gem 'activerecord', '~> 2.3.0'

This will force the dependencies to lock to the 2.3 version and still be valid for scoped_search's dependency requirements.

et commented 12 years ago

I'm assuming you meant script/console test.

Indeed something is screwy:

➜  foo git:(master) ✗ ruby script/console test
Loading test environment (Rails 2.3.11)
loading test.rb
/home/et/code/foo/vendor/rails/activesupport/lib/active_support/dependencies.rb:469:in `load_missing_constant':NameError: uninitialized constant Test
>> RSpec
NameError: uninitialized constant RSpec
    from /home/et/code/foo/vendor/rails/activesupport/lib/active_support/dependencies.rb:469:in `load_missing_constant'
    from /home/et/code/foo/vendor/rails/activesupport/lib/active_support/dependencies.rb:106:in `rake_original_const_missing'
    from /home/et/.rvm/gems/ruby-1.8.7-p352@foo/gems/rake-0.9.2/lib/rake/ext/module.rb:36:in `const_missing'
    from /home/et/code/foo/vendor/rails/activesupport/lib/active_support/dependencies.rb:118:in `const_missing'
    from (irb):1
>> Spec
NameError: uninitialized constant Spec
    from /home/et/code/foo/vendor/rails/activesupport/lib/active_support/dependencies.rb:469:in `load_missing_constant'
    from /home/et/code/foo/vendor/rails/activesupport/lib/active_support/dependencies.rb:106:in `rake_original_const_missing'
    from /home/et/.rvm/gems/ruby-1.8.7-p352@foo/gems/rake-0.9.2/lib/rake/ext/module.rb:36:in `const_missing'
    from /home/et/code/foo/vendor/rails/activesupport/lib/active_support/dependencies.rb:118:in `const_missing'
    from (irb):2
bcardarella commented 12 years ago

hmmm... would you mind gisting your spec/spec_helper.rb?

et commented 12 years ago

https://gist.github.com/1443898

bcardarella commented 12 years ago

Perhaps I should flip the logic and check if Spec is defined first.

Give this a shot, run:

bundle open valid_attribute

That will let you edit the copy of valid_attribute your app will use. And in lib/valid_attribute.rb just flip the logic so it will see if Spec is defined before RSpec and require the proper file. Let me know if that solves the issue.

On Wed, Dec 7, 2011 at 1:10 PM, Eric Thomas reply@reply.github.com wrote:

https://gist.github.com/1443898


Reply to this email directly or view it on GitHub: https://github.com/bcardarella/valid_attribute/issues/15#issuecomment-3050956

et commented 12 years ago

Changed to this:

if defined?(Spec)
  require 'valid_attribute/spec'
elsif defind?(RSpec)
  require 'valid_attribute/rspec'
.
.
.
end

But from my last console output, it appears that Spec isn't defined at all.

I also just tried force requiring valid_attribute/spec with no luck

et commented 12 years ago

Getting closer, was able to get the Spec constant defined by changing

gem 'rspec', '~> 1.3.2'

to

gem 'rspec', '~> 1.3.2', require => 'spec'

➜  foo git:(master) ✗ ruby script/console test
Loading test environment (Rails 2.3.11)
loading test.rb
SDEPRECATION WARNING: require "activerecord" is deprecated and will be removed in Rails 3. Use require "active_record" instead. (called from /home/et/code/foo/vendor/rails/activerecord/lib/activerecord.rb:2)
pec>> Spec
=> Spec
>> RSpec
NameError: uninitialized constant RSpec
    from /home/et/code/foo/vendor/rails/activesupport/lib/active_support/dependencies.rb:469:in `load_missing_constant'
    from /home/et/code/foo/vendor/rails/activesupport/lib/active_support/dependencies.rb:106:in `rake_original_const_missing'
    from /home/et/.rvm/gems/ruby-1.8.7-p352@foo/gems/rake-0.9.2/lib/rake/ext/module.rb:36:in `const_missing'
    from /home/et/code/foo/vendor/rails/activesupport/lib/active_support/dependencies.rb:118:in `const_missing'
    from (irb):2
et commented 12 years ago

Ah-ha!

Forcefully adding config.include ValidAttribute::Method to my spec_helper.rb fixes this:

require 'valid_attribute'
.
.
.
Spec::Runner.configure do |config|
  config.include ValidAttribute::Method
.
.
.
end

I can only assume that the configure block can be run once.

@bcardarella - would this be the ideal solution to handling old-skool rspec? If so can you update the README to save my future googling self a headache. Thanks for your help with this.