Fatima
Fatima

Reputation: 355

How to reuse a method from a helper inside of a lib module in Rails?

Let's say I have a method in my ApplicationHelper:

module ApplicationHelper
  def a
    do something
  end
end

and I have a module in my lib folder:

module MyModule
  def do_another_thing
   # How can I use the method a here?
   # I know that I can include the `ApplicationHelper` here, but I don't want to pollute my module with unrelated functions
  end
end

Upvotes: 0

Views: 131

Answers (1)

Vasiliy Ermolovich
Vasiliy Ermolovich

Reputation: 24617

You can mark that a method as module_function like so:

module ApplicationHelper
  def a
    do something
  end
  module_function :a
end

and then use it like ApplicationHelper.a

Upvotes: 2

Related Questions