Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call a block passed rake_tasks method in the self context #3771

Merged
merged 1 commit into from
Nov 27, 2011

Conversation

kennyj
Copy link
Contributor

@kennyj kennyj commented Nov 27, 2011

When I run rake_test.rb, I had some warnings. The reproduce step is

$ cd railties
$ bundle exec ruby -Itest:lib:../activesupport/lib:../actionpack/lib test/application/rake_test.rb --name=test_initializers_are_executed_in_rake_tasks
...
WARNING: Global access to Rake DSL methods is deprecated.  Please include
    ...  Rake::DSL into classes and modules which use the Rake DSL methods.
WARNING: DSL method Class#task called at /home/kennyj/rails/railties/tmp/app/config/application.rb:58:in `block in <class:Application>'
...

We call the global scope task method (deprecated method) in rake_test.rb
Briefly we must call extend Rake::DSL only,
but I think that the cause of problem is located another.

In load_tasks method, we call extend Rake::DSL. But It seems that we have another context when executing rake_tasks.

182     def load_tasks(app=self)
183       extend Rake::DSL if defined? Rake::DSL
184       self.class.rake_tasks.each { |block| block.call(app) }

This is related to #1174.

If this is a specification, I'll fix a tastcase.

@josevalim
Copy link
Contributor

What if we did:

self.class.send :include, Rake::DSL if defined? Rake::DSL

Instead of:

extend Rake::DSL if defined? Rake::DSL

This should add Rake::DSL to the whole inheritance chain so we could skip instance_exec.

@kennyj
Copy link
Contributor Author

kennyj commented Nov 27, 2011

I think it is better than extend and self.class.send :include too.

Do you say below code?

    def load_tasks(app=self)
      self.class.send :include, Rake::DSL if defined? Rake::DSL
      self.class.rake_tasks.each { |block| block.call(app) }
      ...

but, I saw the warnings still.

    def load_tasks(app=self)
      self.class.send :include, Rake::DSL if defined? Rake::DSL
      self.class.rake_tasks.each { |block| self.instance_exec(app, &block) }
      ...

The above code looks fine.

(I'm not good at English, sorry if wrong.)

@josevalim
Copy link
Contributor

if we still need to use instance_exec, it is not much help. please let me think and check if i can find other alternatives. thanks for your contributions!!

josevalim added a commit that referenced this pull request Nov 27, 2011
Call a block passed rake_tasks method in the self context
@josevalim josevalim merged commit 0b7cd7b into rails:master Nov 27, 2011
@josevalim
Copy link
Contributor

@kennyj thanks, i have merged the patch with instance_exec. We already use instance_exec in the following lines, so i have decided to go with the same pattern. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants