How to load environment variables in the Rails console? - ruby-on-rails-3

I think this is a little, easy question!
I'm using .env file to keep all my environment variables, and i'm using foreman.
Unfortunately, these environment variables are not being loaded when running rails console rails c so, i'm now loading them manually after running the console, which is not the best way.
I'd like to know if there any better way for that.

About a year ago, the "run" command was added to foreman
ref: https://github.com/ddollar/foreman/pull/121
You can use it as follow:
foreman run rails console
or
foreman run rake db:migrate

rails does not know about the environmental variables specified in .env file as it is specific to foreman. You need to set the environment explicitly before invoking rails console. Have a look at this question.

I personnaly use dotenv in development and testing environements. With this approach, you don't have to prefix your commands, just call the initializer in your config/application.rb :
Bundler.require(*Rails.groups)
Dotenv::Railtie.load
HOSTNAME = ENV['HOSTNAME']

Related

How can i run "rails routes --expanded" exclusively in Rails6 Test Environment?

Below details are in reference of Rails 6.
I have created category controller using scaffold in the test environment. I want to crosscheck all the routes and its URI using rails routes --expanded for test environment.
How can i do that?
As to run that command directly in terminal will give routes of development environment.
I have already checked rails console -e test but its exclusively for irb of test env.
You can use RAILS_ENV=environment before the command like this:
RAILS_ENV=test rails routes --expanded
This will output all routes in your test environment.

Running Capybara tests at a different url when on Travis CI

I have some integration tests written in Capybara which I'm running on Travis. In the tests I hit a hardcoded url (given by Pow and symlinks) with the visit method. This of course does not work well on Travis. What I need to do is to somehow distinguish environments. So when the tests run on Travis they are hitting a different url like localhost:5000 for example. I put that in my .Travis.yml file that it will start a rails server in the background which works fine. The question is how do I make the tests use that url instead?
My config looks something like this:
language: ruby
rvm:
- 1.9.3
before_script:
- RAILS_ENV=test bundle exec rake db:create
- "bundle exec rails server -p 5000 &"
- "sleep 5" # Wait for the server to start up"
script: bundle exec rspec
I'm using PhantomJS through poltegeist gem. I'm thinking if I could somehow use the Travis env var. Anyone got any suggestions on this?
Thanks!
I'm not very familiar with Travis CI but I don't see any reason you need to hardcode the server name into url.
Instead of
visit 'http://localhost:5000/about'
You can use
visit '/about'
Or better
visit about_path
Less dependency is always better. I suggest your tweaking the tests.
Travis CI sets several environment variables for you that you can use. I think TRAVIS=true may be of interest:
Capybara.app_host = if ENV['TRAVIS']
'localhost:5000'
else
'http://www.example.com'
end

How to display RoR app code version on heroku?

For my Rails apps I normally deploy to production from a tagged version, and then display the tag in the user interface assigning the output of git describe --always to a variable in config/application.rb.
Now I'm moving an app over to Heroku, and deployment to heroku only happens using the master branch, so this trick won't work any more.
Are there any other ways to assign a version number to my code and display it on the UI when I've deployed to heroku?
Thanks,
Stewart
You can add a variable to the Heroku configuration by running this command locally whenever you push new changes to Heroku:
heroku config:add GIT_TAG=`git describe --always`
Then you can access this in your app's configuration:
version = ENV['GIT_TAG'] || `git describe --always`
When the app is running on Heroku, it will pick up the config variable (ENV['GIT_TAG']) and when it's running locally in development it will fall back to running git describe --always.
You will need to update the Heroku config variable each time you deploy, but I generally add this kind of thing to a deploy script or rake task (along with useful things like creating a new tag marking the deploy and running any new database migrations on Heroku).
Doesn't git tag fit your needs?
And why wouldn't the old trick work anymore?
If you want to display it on the UI then a git SHA output probably isn't particularly useful - you have two options, set a Heroku config variable with a user friendly version number in or a set a version number in your code that you increment when you deploy from master. You could probably wrap the deploy up in a rake task that incremented the version number either a file (and then readded it to git and commits it) or simply increments a value in a config variable.
Also, don't forget Heroku release management http://blog.heroku.com/archives/2010/11/17/releases/ which you may also be able to employ here to get the version number from that perhaps.

How do I ensure my jruby command line options are used when running "rails", "rake", "rspec" etc?

I currently run my Rails app using:
jruby --1.9 -J-XX:+CMSClassUnloadingEnabled -J-XX:+UseConcMarkSweepGC -J-XX:MaxPermSize=256m -S rails server
This is getting pretty old now. How can I set my Rails project up so that just running
rails server
has the same effect?
(Note: bash aliases and the like are not what I'm looking for here. I want to make the project work right, not fix my local settings)
When using RVM and a project .rvmrc, the canonical way is to set PROJECT_JRUBY_OPTS in the project .rvmrc. A bug prevented this from working for me, so use rvm head.
If not using rvm then use JRUBY_OPTS, which is the built-in way of doing it that JRuby checks (in fact, the PROJECT_JRUBY_OPTS thing ends up being converted to JRUBY_OPTS by rvm).

Register a script to use with the rails command, like `rails mycommand`

The rails command-line command provides a couple of commands, like rails generate, rails console etc. Now I'd like write a gem which registers my own command for use with rails mycommand.
Is this possible?
If so, any guides on how to do that?
NB: This is for rails 3+
regards, apeiros
Haven't done it, but here are some leads. In your Rails app, the script directory holds a file called 'rails' that has this line
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
It then requires this Rails file: https://github.com/rails/rails/blob/master/railties/lib/rails/commands.rb