Asana / ruby-asana

Official Ruby client library for the Asana API v1
MIT License
77 stars 53 forks source link

ruby2_0_0_compatibility.rb#required Breaks FactoryGirl on models that have a `required` attribute. #45

Open mful opened 8 years ago

mful commented 8 years ago

Hi there,

Trying to integrate the asana API client into a project I am working on. Simply adding the gem to my Gemfile and bundling breaks my test suite, because I have factories that create models with a required attribute, and the the #required method in ruby-asana/lib/asana/ruby2_0_0_compatibility.rb interferes.

Stack Trace (note line 4):

$ rspec
/Users/me/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.5.0/lib/factory_girl/definition_proxy.rb:42:in `add_attribute': wrong number of arguments (given 3, expected 1..2) (ArgumentError)
from /Users/me/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.5.0/lib/factory_girl/definition_proxy.rb:102:in `method_missing'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/asana-0.5.0/lib/asana/ruby2_0_0_compatibility.rb:2:in `required'
from /Users/me/projects/project/spec/factories/content_block_factories.rb:5:in `block (2 levels) in <top (required)>'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:18:in `instance_eval'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:18:in `factory'
from /Users/me/projects/project/spec/factories/content_block_factories.rb:3:in `block in <top (required)>'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:49:in `instance_eval'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:49:in `run'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:7:in `define'
from /Users/me/projects/project/spec/factories/content_block_factories.rb:1:in `<top (required)>'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.5.0/lib/factory_girl/find_definitions.rb:20:in `block (2 levels) in find_definitions'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.5.0/lib/factory_girl/find_definitions.rb:19:in `each'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.5.0/lib/factory_girl/find_definitions.rb:19:in `block in find_definitions'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.5.0/lib/factory_girl/find_definitions.rb:15:in `each'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.5.0/lib/factory_girl/find_definitions.rb:15:in `find_definitions'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/factory_girl_rails-4.5.0/lib/factory_girl_rails/railtie.rb:21:in `block in <class:Railtie>'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:44:in `each'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/railties-4.2.6/lib/rails/application/finisher.rb:62:in `block in <module:Finisher>'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `run'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/railties-4.2.6/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/me/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
from /Users/me/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
from /Users/me/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
from /Users/me/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
from /Users/me/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each'
from /Users/me/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `call'
from /Users/me/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
from /Users/me/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
from /Users/me/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/railties-4.2.6/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/railties-4.2.6/lib/rails/application.rb:352:in `initialize!'
from /Users/me/projects/project/config/environment.rb:5:in `<top (required)>'
from /Users/me/projects/project/spec/rails_helper.rb:3:in `require'
from /Users/me/projects/project/spec/rails_helper.rb:3:in `<top (required)>'
from /Users/me/projects/project/spec/controllers/api/comments_controller_spec.rb:1:in `require'
from /Users/me/projects/project/spec/controllers/api/comments_controller_spec.rb:1:in `<top (required)>'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
from /Users/me/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
from /Users/me/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `load'
from /Users/me/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `<main>'
from /Users/me/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
from /Users/me/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'

To quickly reproduce, create a model with a boolean required attribute in a rails project that uses FactoryGirl, and create a simple Factory for it, then try to run rspec:


# spec/factories/my_model_factories.rb
FactoryGirl.define do

factory :my_model do
 required true
 end

end

Is there a way I can work around this?

Thanks!

┆Issue is synchronized with this Asana task

mful commented 8 years ago

Found a workaround, though the underlying issue is still there. In my Factories, I replaced all instances of required true and required false with add_attribute :required, <value>.

So an updated factory definition looks as follows:

# spec/factories/my_model_factories.rb

FactoryGirl.define do

  factory :my_model do
    # was 'required true'
    add_attribute :required, true
  end

end
agnoster commented 8 years ago

I'm not really up in the ruby code at the moment and haven't had a chance to look at this yet - do you have any suggestions/proposals to fix this? I'm sure we'll get to this eventually but obviously suggestions (or, heh, best case pull requests I suppose) would help accelerate that timeline ;-)

czeise commented 1 year ago

I've got a PR that should fix this without modifying any generated code.

As noted in the PR, the scope of this bug isn't just compatibility with FactoryBot. The required method is introduced globally and will conflict with any codebase that has a method named required.