17

I have lot of helpers in my main Sinatra project_name.rb and I want to remove them to the external file, what is the best practice to do that ?

from ./preject_name.rb

   helpers do
     ...#bunch of helpers
   end

to for exapmple ./helpers/something.rb

thank you

equivalent8
  • 13,754
  • 8
  • 81
  • 109

5 Answers5

30

The simple and recommended way:

module ApplicationHelper

# methods

end

class Main < Sinatra::Base

  helpers ApplicationHelper

end
kgpdeveloper
  • 2,099
  • 4
  • 25
  • 34
15

Alas, if, like me, you are building a modular Sinatra app, it's a little more complex than simply moving the helpers out into another file.

The only way I got this to work is as follows.

first up in your app (I'll call this my_modular_app.rb)

require 'sinatra/base'
require 'sinatra/some_helpers'

class MyModularApp < Sinatra::Base
  helpers Sinatra::SomeHelpers

  ...

end

and then create the folder structure ./lib/sinatra/ and create some_helpers.rb as follows:

require 'sinatra/base'

module Sinatra
  module SomeHelpers

    def help_me_world
      logger.debug "hello from a helper"
    end

  end

  helpers SomeHelpers

end

doing this you can simply split all your helpers up into multiple files, affording more clarity in larger projects.

Dave Sag
  • 13,266
  • 14
  • 86
  • 134
9

Just as you said it yourself:

Move the helpers block into another file and require it where you need.

#helpers.rb
helpers do
...
end

#project_name.rb
require 'path/to/helpers.rb'
scable
  • 4,064
  • 1
  • 27
  • 41
  • lol, simple :) why didn't I tryied in firstplace :) thank you, I used is like `require "#{File.dirname(__FILE__)}/helpers/helpers.rb"` – equivalent8 Aug 03 '11 at 13:56
  • 1
    When you use Ruby 1.9.2 you might aswell use `require_relative 'helpers/helpers'` instead of that `File`-construct – scable Aug 03 '11 at 14:10
  • what's the advantage ? ... and I kinda want this project to run on all machines, will be on github for share :) – equivalent8 Aug 04 '11 at 09:34
  • Not sure if it has any impact besides it just looks better/cleaner imho. – scable Aug 04 '11 at 10:38
  • See my answer. I think we should use modules especially when we are working on modular Sinatra applications. – kgpdeveloper Mar 31 '13 at 14:03
2

It seems the answer @DaveSag offered miss something. Should add a line at the beginning of my_modular_app.rb:

$:.unshift File.expand_path('../lib', __FILE__)  # add ./lib to $LOAD_PATH

require 'sinatra/base'
require 'sinatra/some_helpers' # this line breaks unless line 1 is added.

# more code below...

In addition, if someone prefers a "classical style" like me, the following is for you :)

In app.rb

$:.unshift File.expand_path('../lib', __FILE__)

require 'sinatra'
require 'sinatra/some_helpers'

get '/' do
  hello_world
end

In lib/sinatra/some_helpers.rb

module Sinatra
  module SomeHelper
    def hello_world
      "Hello World from Helper!!"
    end
  end

  helpers SomeHelper
end
Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
0

I just added require_relative './lib/sinatra/helpers' to my config.ru and that's all.

So it looks like this:

require_relative './config/environment'
require_relative './lib/sinatra/helpers'
use ProjectsController
run ApplicationController

and my ./lib/sinatra/helpers.rb file is not even a module and I don't use any requires or includes in it. I can just define methods straight in this file and use them all over the app.

The answer of @kgpdeveloper didn't work for me.

kirqe
  • 2,431
  • 4
  • 37
  • 63