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

Problem with helpers #186

Closed
mimay opened this issue Jun 16, 2012 · 4 comments
Closed

Problem with helpers #186

mimay opened this issue Jun 16, 2012 · 4 comments

Comments

@mimay
Copy link

mimay commented Jun 16, 2012

I'm having some problems with helper methods on a server-script. To make it easier, I took this example from grape's documentation.
When I try to call current_user helper-method, I get "undefined local variable or method `current_user' for #Grape::Endpoint:0x1006a0af8"

Anyone else having the same problem?

  • grape 0.2.0
  • rack 1.4.1

I get the error with both Ruby 1.8.7 and 1.9.2-p290.

Test script:

require 'grape'

module MyHelpers
  def say_hello(user)
    "hey there #{user}"
  end
end

class API < Grape::API
  rescue_from :all, :backtrace => true

  # define helpers with a block
  helpers do
    def current_user
      "Superman"
    end
  end

  # or mix in a module
  helpers MyHelpers

  get '/hello' do
    # helpers available in your endpoint and filters
    say_hello(current_user)
  end
end

application = Rack::Builder.new do
  map '/' do
    run API
  end
end

Rack::Handler::Thin.run application, :Port => 3000

Stacktrace:

undefined local variable or method `current_user' for #
 test.rb:25:in `block in '
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/grape-0.2.0/lib/grape/endpoint.rb:248:in `instance_eval'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/grape-0.2.0/lib/grape/endpoint.rb:248:in `run'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/grape-0.2.0/lib/grape/endpoint.rb:110:in `block in call!'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/grape-0.2.0/lib/grape/middleware/base.rb:25:in `call'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/grape-0.2.0/lib/grape/middleware/base.rb:25:in `call!'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/grape-0.2.0/lib/grape/middleware/base.rb:19:in `call'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/grape-0.2.0/lib/grape/middleware/error.rb:44:in `block in call!'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/grape-0.2.0/lib/grape/middleware/error.rb:43:in `catch'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/grape-0.2.0/lib/grape/middleware/error.rb:43:in `call!'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/grape-0.2.0/lib/grape/middleware/base.rb:19:in `call'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:134:in `call'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/grape-0.2.0/lib/grape/endpoint.rb:111:in `call!'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/grape-0.2.0/lib/grape/endpoint.rb:101:in `call'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-mount-0.8.3/lib/rack/mount/route_set.rb:152:in `block in call'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:96:in `block in recognize'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:68:in `optimized_each'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:95:in `recognize'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-mount-0.8.3/lib/rack/mount/route_set.rb:141:in `call'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/grape-0.2.0/lib/grape/api.rb:399:in `call'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/grape-0.2.0/lib/grape/api.rb:51:in `call!'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/grape-0.2.0/lib/grape/api.rb:47:in `call'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:134:in `call'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/urlmap.rb:64:in `block in call'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/urlmap.rb:49:in `each'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/urlmap.rb:49:in `call'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:134:in `call'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/thin-1.3.1/lib/thin/connection.rb:80:in `block in pre_process'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/thin-1.3.1/lib/thin/connection.rb:78:in `catch'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/thin-1.3.1/lib/thin/connection.rb:78:in `pre_process'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/thin-1.3.1/lib/thin/connection.rb:53:in `process'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/thin-1.3.1/lib/thin/connection.rb:38:in `receive_data'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/thin-1.3.1/lib/thin/backends/base.rb:61:in `start'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/thin-1.3.1/lib/thin/server.rb:159:in `start'
 /Users/test/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/handler/thin.rb:13:in `run
@mimay
Copy link
Author

mimay commented Jun 17, 2012

With the script below, everything works fine, except for current_user=(message) method. When calling this method I don't see any output. Neither I get the value back from warden method (though I see the output "warden method called" from this method)

I tested this with ruby 1.9.2-p290 and 1.9.3-rc1. Branches master, 0-1-stable and frontier.

module MyApp
  class API < Grape::API
    prefix 'api'

    helpers do
      def warden
        puts "warden method called"
        @message
      end

      def current_user
        warden
      end

      def current_user=(message)
        puts "message: #{message}"
        @message = message
      end

      def authenticate!
        current_user
      end
    end

    get 'my_email' do
      current_message = "something"
      authenticate!
    end
  end
end

@ppadron
Copy link
Contributor

ppadron commented Jun 28, 2012

Regarding your first script sample, I ran into a similar issue. It seems that when you add helpers from a module and from a block, you have to add the module first, and then the block. Try this:

require 'grape'

module MyHelpers
  def say_hello(user)
    "hey there #{user}"
  end
end

class API < Grape::API
  rescue_from :all, :backtrace => true

  # first you add your module
  helpers MyHelpers

  # and then you define your helpers with a block
  helpers do
    def current_user
      "Superman"
    end
  end

  get '/hello' do
    # helpers available in your endpoint and filters
    say_hello(current_user)
  end
end

application = Rack::Builder.new do
  map '/' do
    run API
  end
end

Rack::Handler::Thin.run application, :Port => 3000

I suppose it's a problem in Grape::API#helpers method. I'll try to make a patch, but I can't promise since this is my first Ruby project (still learning metaprogramming). =)

@ppadron
Copy link
Contributor

ppadron commented Jun 28, 2012

Can anyone review this patch? I changed the behavior of Grape::API#helpers and added a test for it (which fails without the change).

@dblock dblock closed this as completed in 9018102 Jun 29, 2012
stephencelis pushed a commit to stephencelis/grape that referenced this issue Jun 29, 2012
…helpers

fixes ruby-grape#186: helpers should allow multiple calls with modules and blocks
@adamgotterer
Copy link
Contributor

If someone stumbles on this issue because they are running into helpers overriding each other. You'll be happy to know this pull request fixes that issue. Use the latest master, the current gem doesn't have this fix.

Example of overriding bug:

module Helper1
  def help1
    puts 'Helper 1 works'
  end
end

module Helper2
  def help2
     puts 'Helper 2 works'
  end
end

class MyApi < Grape::API
  helpers Helper1
  helpers Helper2

  resource :myapi do
    get '/' do
      help1
      help2
    end
  end
end

Throws a method doesn't exist error for help1.

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

No branches or pull requests

4 participants