Skip to content
This repository
Browse code

Add #helpers into Sinatra::Base [#134 state:resolved]

Signed-off-by: Ryan Tomayko <rtomayko@gmail.com>
  • Loading branch information...
commit c094757b080e945939a0112572cb19761ec894b6 1 parent 25b0456
Nicolás Sanguinetti foca authored rtomayko committed
7 lib/sinatra/base.rb
@@ -658,6 +658,11 @@ def compile(path)
658 658 end
659 659
660 660 public
  661 + def helpers(*modules, &block)
  662 + include *modules unless modules.empty?
  663 + class_eval(&block) if block
  664 + end
  665 +
661 666 def development? ; environment == :development ; end
662 667 def test? ; environment == :test ; end
663 668 def production? ; environment == :production ; end
@@ -900,7 +905,7 @@ def #{method_name}(*args, &b)
900 905 delegate :get, :put, :post, :delete, :head, :template, :layout, :before,
901 906 :error, :not_found, :configures, :configure, :set, :set_option,
902 907 :set_options, :enable, :disable, :use, :development?, :test?,
903   - :production?, :use_in_file_templates!
  908 + :production?, :use_in_file_templates!, :helpers
904 909 end
905 910
906 911 def self.new(base=Base, options={}, &block)
7 lib/sinatra/main.rb
@@ -32,13 +32,6 @@ class Default < Base
32 32
33 33 include Sinatra::Delegator
34 34
35   -def helpers(*modules, &block)
36   - Sinatra::Application.class_eval do
37   - modules.each {|m| include m }
38   - end
39   - Sinatra::Application.class_eval(&block)
40   -end
41   -
42 35 def mime(ext, type)
43 36 ext = ".#{ext}" unless ext.to_s[0] == ?.
44 37 Rack::Mime::MIME_TYPES[ext.to_s] = type
42 test/helpers_test.rb
@@ -379,3 +379,45 @@ def send_file_app(opts={})
379 379 assert_equal 'W/"FOO"', response['ETag']
380 380 end
381 381 end
  382 +
  383 +describe 'Adding new helpers' do
  384 + module HelperOne; def one; '1'; end; end
  385 + module HelperTwo; def two; '2'; end; end
  386 +
  387 + it 'should allow passing a list of modules' do
  388 + mock_app {
  389 + helpers HelperOne, HelperTwo
  390 +
  391 + get '/one' do
  392 + one
  393 + end
  394 +
  395 + get '/two' do
  396 + two
  397 + end
  398 + }
  399 +
  400 + get '/one'
  401 + assert_equal '1', body
  402 +
  403 + get '/two'
  404 + assert_equal '2', body
  405 + end
  406 +
  407 + it 'should take a block and mix it into the app' do
  408 + mock_app {
  409 + helpers do
  410 + def foo
  411 + 'foo'
  412 + end
  413 + end
  414 +
  415 + get '/' do
  416 + foo
  417 + end
  418 + }
  419 +
  420 + get '/'
  421 + assert_equal 'foo', body
  422 + end
  423 +end

1 comment on commit c094757

TJ Holowaychuk

Damn! I was going to do this last night but it would not let me fork Sinatra haha :(

Please sign in to comment.
Something went wrong with that request. Please try again.