Rails Environment - ruby-on-rails

Is it possible to create user defined Rails environment.... By default support development,staging,production,test... Other than that is it possible for user defined environment.... If yes how? ... please suggest me on this ... thanks in advance...

You can define whatever environments you like by adding a my_awesome_environment.rb file to config/environments. After that, if you want to run a rake task in that environment you could do rake awesome:task RAILS_ENV=my_awesome_environment.
And, unless it's changed recently, there is no staging environment by default. Only development, test, production. As an example of "custom" environments, Cucumber, has its own cucumber environment when you set it up for a project.
If you have a more specific question, we might be able to give a more specific answer. As it stands, this is pretty open ended.

Related

Different environments included in Ruby on Rails

Can someone explain to me what the Rails environments are and what they do? I have tried researching myself, but could not find anything. From what I gather, the environments are:
Development
Productions
Test
Each "environment" is really just a config. You can launch your app in various different modes, and the modes are called "environments" because they affect the app's behaviour in lots of different ways. Ultimately, though, they are just configs.
BTW you can't have looked very hard when you looked "everywhere", because i just googled "rails environment" and the top result was this
http://guides.rubyonrails.org/configuring.html
which is the official explanation of configuring the rails environment.
From what you have provided in your question, it seems that you are asking:
"What are the difference between each environment configuration in Rails?"
Rails comes packages with 3 types of environments. Each have its own server, database, and configuration. See Rails Guides: Configuration for more information on options available to you.
Setting up the environment
To set your Rails environment, you will want to enter in command line:
export RAILS_ENV=<env>
Where <env> can be test, development, or production. Setting this environment variable is crucial, as it will determine what gems are installed, or what env is touched when running rails console or rails server.
Included in configuration is the gemset used for the app. When you run rails new, you will find a Gemfile with groups test, development, and production. These groups correspond to the environment currently set. When the environment is set to one of those, running bundle install installs all gems related to that group (and gems not listed in a group).
Included environments
test is designed for running tests/specs. This database will likely be bare bones, except for seeds you may call before running the suite. After each test is complete, the database will rollback to its state before the test began. I do not recommend launching rails server, as running tests (via MiniTest or RSpec) will do this for you, and close the server once the suite is finished.
development allows you to "test" your app with a larger database, typically a clone of production. This allows you to test actual real-world data without breaking production (the version that customers or end-users will experience). To view the development environment in action, change the RAILS_ENV and launch rails server. This is good for deciding how you want your pages to look (CSS, HTML). It is also good practice to briefly "test" your app yourself, clicking around making sure everything "looks" good and the JavaScript works.
production is reserved for the customer and end-user. Configuration includes the actual domain of the app, which ports to use, and initializers or tasks to run. You do not want to play around with your database, as it may be customer-impacting. Ideally, the app should work as best as it can, since this is considered your "final product."
Here are some good reads about Rails Environments
http://teotti.com/use-of-rails-environments/
and
https://signalvnoise.com/posts/3535-beyond-the-default-rails-environments
good luck !!

Rails: Make request to development environment while using tests

I would like to know how I could make a request on my development environment from the tests environment.
I know it's not a good practice but I don't know how to achieve my goal otherwise:
On my project, I've created some modules (Newsletter, Search, GuestBook, ...) and I would like to have a table allowing me to enabled or disabled modules. I have created the scaffold for this.
The but of all of this is to not run tests if the setting is disabled in other environments.
My problem is when I make a request to check if the module is enabled or not, it targets the test database and not the development database.
Anyone know how I could solve my problem ?
Thanks for your help
You don't want to do it that way, you want to set the same DB in your test environment so that it will check that table in your test DB during the testing.
...or even better, stub out the model used to retrieve those enabled/disabled settings so you can just adjust them on the fly as you run various tests.

Best way to set environment-specific variables in rails

There are certain variables I would like to have in the development environment, and certain variables for the production environment. For example, in production mode, I might want to use a cache to speed up performance. I have couple of open-ended questions about this:
Question: What is the easiest and fastest way to set environment variables in a rails app? For example, ENV["USE_CACHE"] = true in production, and = false in development.
If you could point to a specific gem, and/or the specific files I'd need to touch, that would be most helpful. Thanks!
I learnt few things about ENV variables this discussion
You can set ENV varaibles by just appending
export USE_CACHE=true
to your .bashrc file.
...and in your application you can use
ENV["USE_CACHE"]
,
you can also check out figaro and dotenv which are application specific.
For your purpose just use
if Rails.env.eql?("development")
#do stuff
end
Its upto you choose easiest and fastest way!

Is there a way to test/validate production.rb (or any environment file)

I had a problem in a UAT environment because there was a configuration problem in config/environments/uat.rb
Is there a way to test/validate environment files like uat.rb or production.rb to catch errors before deploying?
Loading the files with require in rspec might be a problem because it might affect other tests, not sure about this.
Thanks,
Before deploying on production, I always run my code in production mode on my development machine using environment flag:
rails s --environment=production
You have to choose what tests you write based on the projected ROI (return on investment) of writing those tests. Tests aren't free - far from it. They take time to create and maintain, they junk up your project, etc. Only write tests that you judge to be useful in the future.
I've had plenty of issues with configuration in the past and the solution has always been KISS - Keep it simple stupid. Most apps should require very little configuration, thus are hard to mis-configure.

Is there a way to know that a rails app has been launched from a rake command?

I'm doing a bunch of caching tasks in my rails initializers.
They're useful for the website, but absolutely useless and time consuming when I launch rake tasks.
Is there a way in rails to know when the app has been launched from a rake command?
Thanks
I guess the other option is to ask whether you can tell whether the app has been launched as a website and only run the initializers then.
In that case you should be able to set environment variables in whichever web server you are running the application and you may then be able to trigger your initializer when the environment variable is set. Something like:
if ENV['INITIALIZE_BLAH']
# Do your website only initialization
end

Resources