How to Use Helpers in Rake

How do I use helpers in rake?

Yes, you can. You simply need to require the helper file and then include that helper inside your rake file (which actually a helper is a mixin that we can include).

For example, here I have an application_helper file inside app/helpers directory that contains this:

module ApplicationHelper
def hi
"hi"
end
end

so here is my rake file's content:

require "#{Rails.root}/app/helpers/application_helper"
include ApplicationHelper

namespace :help do
task :hi do
puts hi
end
end

and here is the result on my Terminal:

god:helper-in-rake arie$ rake help:hi 
hi

Where to put helper functions for rake tasks and test files in Ruby on Rails?

You could create a static class, with static functions. That would look something like this:

class HelperFunctions

def self.random_address
[Faker::Address.street_address, Faker::Address.city].join("\n")
end

def self.otherFunction
end
end

Then, all you would need to do is:

  1. include your helper class in the file you want to use
  2. execute it like:

    HelperFunctions::random_address(anyParametersYouMightHave)

When doing this, make sure you include any dependencies in your HelperFunctions class.

How to use devise helpers in a rake task?

Devise in its default configuration does not allow you to track which users are online and offline. The helpers current_user and user_signed_in? pertain to the current session and are only valid during a user's request. Since there is no request in a rake task, they aren't useful.

You could potentially add a column to your database to track whether users are signed in or signed out, but this functionality isn't present in devise.

Using Routing helpers in a Rake task

Are you using an ActionMailer to deliver the messages? It seems like it. If so, you can just pass off the token and have the Mailer itself format the token using the register_url. So you would do:

invitation = Invitation.all(:conditions => { :sent_at => nil, :sender_id => nil }, :limit => limit).each do |i|
Mailer.deliver_invitation(i)
c.increment!
end

and then in your invite template you would just use

<%= register_url(i.token) %>

Using helpers in model: how do I include helper dependencies?

Just change the first line as follows :

include ActionView::Helpers

that will make it works.

UPDATE: For Rails 3 use:

ActionController::Base.helpers.sanitize(str)

Credit goes to lornc's answer

Accessing Named Routes in Rakefile

You can either use this example code in your rake task:

include Rails.application.routes.url_helpers
puts birthdays_url(:host => 'example.com')

or you can use this example code in your rake task:

puts Rails.application.routes.url_helpers.birthdays_url(:host => 'example.com')

If you only want the path part of the URL, you can use (:only_path => true) instead of (:host => 'example.com'). So, that would give you just /birthdays instead of http://example.com/birthdays.

You need either the (:host => 'example.com') or (:only_path => true) piece, because the rake task doesn't know that bit of information and will give this error without it:

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

Is it possible to include a module in rake tasks without polluting the global scope?

Here's what worked for me:

module MyRakeHelpers
def helper
puts 'foo'
end
end

module MyRakeTasks
extend Rake::DSL
extend MyRakeHelpers

task :some_task do
helper
end
end

In short, you can use the Rake DSL in a different scope by including (or extending) Rake::DSL. From the source:

DSL is a module that provides #task, #desc, #namespace, etc. Use this when you'd like to use rake outside the top level scope. For a Rakefile you run from the command line this module is automatically included.

task uses Rake::Task#define_task under the hood, which you could also use to write your own DSL.

Thanks to How To Build Custom Rake Tasks; The Right Way for the tip about define_task.



Related Topics



Leave a reply



Submit