Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8197633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:46:56+00:00 2026-06-07T05:46:56+00:00

I am looking for a clean way to have different behavior depending on whether

  • 0

I am looking for a clean way to have different behavior depending on whether we are in the test, development, staging, or production environment.

My question is general but the best specific example I have is sending notifications. Let’s say I have a function in my User model object called notify

def notify(message)
  #send a notification to the user (differs depending on the environment)
end

In the test environment I would like to use a mock object that just saves the notifications to a file. In the staging and production environments I would like to use a PushNotification object which actually sends API requests to a third party service. I need to use a different api key for each environment.

I was thinking I would create a folder at lib/notifiers and create a file_notifier.rb and a push_notifier.rb. I don’t know where (what files) and how I would initialize the right notifier. Any ideas?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:46:57+00:00Added an answer on June 7, 2026 at 5:46 am

    For starters, I’m going to be a bit of a whiny bitch — you don’t have a function, but a method. A method, you see, is invoked on a receiver object. Why does this matter? Because if you think hard about it, it reveals the culprit of the difficulty you’re having:

    A user object has no business toggling the output location of a “notification” object.

    So the first step to solving your problem is to create an object that handles notifications and can be independently configured to behave appropriately based on the environment. Note that this object should not itself know about or care about the environment, but should be configurable such that you can easily mock it in development, which could happen either in your development.rb file or in an initializer (perhaps called configure_notifier.rb that will configure it for whichever environment).

    As far as using different API keys, well, that’s a separate issue entirely — you really don’t want to store these in your repository, and the generally-accepted way of doing this is to configure the environment you’re running in to maintain them. For Heroku, as a popular example, you could run heroku config:add SOMESERVICE_API_KEY=abcdefg12345 and then access this in your configuration via ENV['SOMESERVICE_API_KEY'].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a clean way to check if a registry key exists.
I'm looking for a clean way to import one git repository from another, where
I am looking for a neat/clean way to store a list of strings into
I was looking for a reliable, clean way to do advanced runtime reports for
I'm looking for a way to automatically do some clean up tasks when the
I'm looking for a fast, clean, pythonic way to divide a list into exactly
I am looking for a way to redirect users to different routes/templates that is
I am looking for way to clean up a template in django. A simple
I am looking for a way to edit data and have values dynamically calculated
I'm looking for a clean C++ idiom for the following situation: class SomeLibraryClass {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.