2

I have this class that the server is not picking up changes to unless I kill the server and reload it. All my other classes are automatically updated. How can I get the Rails server (WebBrick) to pick up changes to this class without having to kill the server?

I saw this questions but I'm not using a module: Rails 3.2.x: how to reload app/classes dir during development?

I saw this question but it had no answers: Rails Engine: How to auto reload class upon each request?

class UsersController < ApplicationController
  require 'PaymentGateway'
  def method
   result = PaymentGateway::capture

This is the class I want to automatically reload upon change. It is in the same directory as app/controllers/

class PaymentGateway < ApplicationController 
  def self.capture

Rails 4.0.0

Community
  • 1
  • 1
Chloe
  • 25,162
  • 40
  • 190
  • 357

3 Answers3

2

You code has some problems at first.

  1. require inside a class does nothing. To get mixins, use include or extend
  2. A class is not for include or extend. Module does.
  3. You don't need to require a file in '/app`

I don't know what's your real purpose, if you want to reuse the method in PaymentGateway, set it as a module and get it included in others.

module PaymentGateway
  extend ActiveSupport::Concern

  module ClassMethods
    def capture
      # ...
    end
  end
end

# Then in controller
class UsersController < ApplicationController
  include PaymentGateway
end

By this change, on each request to UsersController's actions, the include macro will be executed at runtime and you don't need to restart server.

Billy Chan
  • 24,625
  • 4
  • 52
  • 68
2
  1. Don't use require. That's only for 3rd party libraries.
  2. Change filename to snake_case.rb. Rails will pick up changes automatically.
Chloe
  • 25,162
  • 40
  • 190
  • 357
1

Couple of things I would suggest.

First, The PaymentGateway class should be part of lib/payment_gateway so that it can be used in any part of your application.

Second, use controller inheritance pattern if you want polymorphic controllers

class BaseController < ApplicationController
end

class UsersController < BaseController
end
swapab
  • 2,402
  • 1
  • 27
  • 44
  • I should put it in /lib/ even though it is not a 3rd party source? I don't want polymorphic controllers; I just tried to extend ApplicationController to get it to reload automatically. I thought maybe the server only looks at instances of ApplicationController. – Chloe Jan 20 '14 at 04:24