Consider we have typical Rails project under Git repository. I want to do my personal tweaks for configuration or code base (e.g. to simplify debugging). I don't want to do any extra work when switching, merging or pulling git branches. I don't want to affect other developers either. What approaches do you use (or can suggest) for that?
My approach is following:
create a file (let say personal_initializer.rb) in config/intializers/ directory
in ~/.gitconfig define
[core]
excludesfile = ~/.excludesfile
in ~/.excludesfile add config/intializers/personal_initializer.rb
do all hacks and tweaks in personal_initializer
I would add development.rb file to .gitignore and create a development.example.rb
development.rb would contain all my personal configuration, and wont be pushed to other devs local sources
development.example.rb would contain default configuration, so when a new dev pull the sources he must copy development.example.rb to development.rb and tweak it as he wish
Related
Configuration values (e.g. 3rd party API key/secret, SMTP config, some paths) can be stored in environment (e.g. .bash_profile file) or config/environment folder (e.g. config/environments/development.rb and etc.).
Are there any reasons why one or the other solution is preferred? It seems that using config/environments is a little easier because everything will be tracked in CVS. I'm wondering if there are any reasons why environment variables should be used instead of specifying values directly in config/environments.
You should not put this kind of config values in the CVS (what if you hire some developers, do you really want to share credentials with everybody?).
Many solutions are available in the Rails ecosystem, check here.
Its often a matter of having those settings either in ENV or in a yml file.
I've been using gitignore for password/key protection on my Ruby on Rails apps, so that my config files aren't committed to and publicly available for view on Github.
Going through the Github Developer's API tutorial suggests using environment variables to store these. If I'm not deploying is there any reason I shouldn't use gitignore to store my keys?
There's no strongly compelling reason to prefer environment variables over files, but I'm not sure I'd feel comfortable relying on .gitignore and keeping the files in the source tree. If you opt for files, you might want to consider moving them outside the source tree to avoid any mistakes.
If you've committed the files at any point in the past (particularly to a public site like GitHub), then adding them to .gitignore later isn't going to remove the copies of previous versions that were committed; they'll still be in the repository, and someone could browse back to a version where they weren't ignored.
Additionally, depending on how you run your app, having them in the same directory as the app may make you more vulnerable to web-based security holes. If you keep them outside the app tree, you may gain some limited protection from exploits that can read arbitrary data from the app tree. I don't know that this is all that much of a concern (an exploit that can read within the app tree can probably read outside it too), but it wouldn't hurt to keep them in another directory.
I tend to prefer environment variables because it makes deploying to Heroku easier, so I use them in development too. I'd probably use a file for anything more than simple login/password pairs (RSA keys, etc), and store it in my home directory or somewhere under /var/lib or /etc or /usr/local or something.
There is a gem designed for solving such problems specifically
Figaro: https://github.com/laserlemon/figaro
In a nutshel, Figaro provides a clean and simple way to configure your app and keep the private stuff ... private.
All of your passwords, API keys and anything confidential can be saved in a local yml and won't be pushed to remote git repo.
I'm trying to create a rails app and have it on github, but I'm running into some trouble separating out personal settings from what I check in to git. Just like how you typically check in a database.yml.example and let people make their own database.yml, I wanted to do that with a bunch of other files (all rb files though), like secret_token and production.rb, but I didn't feel like making the setup process involve copying 15 sample files to files that actually get used.
What I ended up doing was creating a settings.yml.example file in my config dir, and putting all of those settings from the other files in there. then the setup process was just 2 copies (database.yml and settings.yml). Then I added this to the beginning of environment.rb
#allow files to read their private settings from settings.yml using SETTINGS
require 'yaml'
SETTINGS = YAML.load(IO.read(Rails.root.join("config", "settings.yml")))
and when I needed something from the file, I would just say something like
Foo::Application.config.secret_token = SETTINGS["secret_token"]
This worked fine until I tried to run rake test, when it gave me uninitialized constant Rails (NameError) from the Rails.root.join call
My question is is this a good way to accomplish what i'm trying to accomplish? And if so, is there a better place to put the code that loads the settings file? It seems like I can load it before each individual call and just add something like "unless settings is defined" after the load, but that would be annoying to have to do everywhere
Note: For anyone who's curious, the files I would have had to copy were
secret_token.rb
production.rb (for config.action_mailer.default_url_options)
devise.rb (for config.pepper)
I expect more in the future as i start using more libraries (still new to this)
this was answered in a comment, so I'll copy Thomas's answer here to make it more clear.
Both figaro and econfig (by the creator of carrierwave and capybara) are great gems for that purpose. I personally use figaro with it's config/application.yml file that is excluded from version control to great success since a few months on open-source projects.
I was hoping you folks could recommend a "best practice" for source controlling binary assets for a Rails site in git.
My main concern is that as we work on the site, constantly adding and removing 500kb+ images from our git repo, the repo will eventually get pretty fat and unwieldy and we'll either have to manually remove those images from history (prone to disaster, as far as I can tell) or put up with a long initial download and extra wasted space on disk.
What are some alternatives for separating the app's logic from the assets? Git submodules? Anything else?
Thanks!
It seems this is pretty well covered elsewhere on SO and elsewhere. A start (after 5 seconds of searching):
On how binary files are tracked: http://osdir.com/ml/git/2009-04/msg01221.html
Seemingly relevant: Managing large binary files with git
If you're really seeing bloat in your repo and are wanting to keep the main rails app repo free of this, you can look into git submodules - all changes to the images would be kept within a separate assets (for example) repo, keeping your main repo free of incurred bloat.
Add the images folder to your .gitignore file. e.g.
/app/assets/images/*
I copied the 'config' directory and renamed it to 'config_dev' so that I don't alter the current settings. How do I configure Rails to use the config directory of 'config_dev'?
Well, i'm not sure whether you can rename that and still make it work or not. However, i would highly not recommend that approach. If you must do something like that, better rename the files inside the folder, like environment.rb.bak or the likes.
Generally speaking a config folder is where important settings initiate from and i think that changing that convention can lead to more problems. I could be wrong, but i would just change the files (that's what the rails 2 to rails 3 conversion plugin does as well).