Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I restrict Rails controller methods to development environment?

I have a Rails application that generates a weekly report and emails it out. I don't want the production app to have any sort of web interface, for security and convenience reasons (don't want to maintain a web interface).

However, I do have a rudimentary web interface to the database that I'd like to keep available in my development environment for debugging, etc.

Is there an easy way to make the controller methods invalid unless I'm in the development rails environment?

like image 961
Summit Guy Avatar asked Dec 06 '25 17:12

Summit Guy


2 Answers

A good approach would be the following:

class MyController < ActionController::Base
  before_filter :restrict_to_development, :only => [:user_report]

  def index
    ...
  end

  def user_report
    ...
  end

  protected
    # this method should be placed in ApplicationController
    def restrict_to_development
      head(:bad_request) unless Rails.env.development?
    end
end
like image 147
Aaron Rustad Avatar answered Dec 08 '25 08:12

Aaron Rustad


Because ruby's classes are evaluated as they are read, you can do this:

class MyController < ActionController::Base

  if RAILS_ENV == "development"
    def index
      #...
    end
  end
end

The index method should only be available when running in development mode.

like image 24
erik Avatar answered Dec 08 '25 08:12

erik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!