Search code examples
ruby-on-railsruby-on-rails-5.2

Rails 5 include helper in class


We are migrating from Rails 4.2 to 5.2. The following code works fine in 4.2 but not in 5

require 'action_view'

module OurModule
  class CheckReport
    include ActionView::Helpers::DateHelper

    def self.our_method
      start_time = Time.current
      LOGGER.info "OurModule::CheckReport.our_method finished in #{distance_of_time_in_words(start_time, Time.current)}"
    end
  end
end

But in Rails 5 we are getting:

NoMethodError: undefined method `distance_of_time_in_words' for OurModule::CheckReport:Class

This seems to be because these are class methods and not instance methods.

So, why did it work in Rails 4 (same ruby version - 2.4.9) and what can we do to fix it (apart from making all these cases instance methods?)


Solution

  • Seeing as some people are coming here to find the answer, here is what I did:

    require 'action_view'
    
    module OurModule
      class CheckReport
    
        def self.our_method
          start_time = Time.current
          LOGGER.info "OurModule::CheckReport.our_method finished in #{ActionController::Base.helpers.distance_of_time_in_words(start_time, Time.current)}"
        end
      end
    end