Use Sass without Rails
You don't need a Rails application to use Sass. Even when you're working on a static site you can generate your CSS through Sass.
- Install Sass with
sudo gem install haml
- Create a folder
sass
in the folder, that stores your stylesheets, e.g.mkdir css/sass
- In a separate terminal window, run
sass --watch css/sass:css
. This will watch your sass files for changes and rewrite stylesheets as required.
This even works on Windows.
Note about your .gitignore
You might want to change our [typical .gitignor...
Run a rake task in all environments
Use like this:
power-rake db:migrate VERSION=20100913132321
By default the environments development
, test
, cucumber
and performance
are considered. The script will not run rake on a production
or staging
environment.
This script is part of our geordi gem on github.
Install RubyGems on Ubuntu/Debian
First of all: You could just use RVM which would make the pain go away. If for some reason you can't, proceed.
Do not use rubygems from the apt repository.
- Get the latest RubyGems TGZ
- Change to the download directory and extract it:\
tar xvfz rubygems-1.3.7.tgz
- If you previously had RubyGems installed via apt: \
sudo apt-get remove rubygems
- Install RubyGems:\
cd rubygems-1.3.7 && sudo ruby setup.rb
- Link
gem
togem1.8
:\
`sudo ...
Detect the language of a string
You can use the whatlanguage gem to detect the language of a Ruby string.
Note that it also has not been updated in quite a while and that there might be alternatives. However, it still works.
It has problems with short strings, but works quite well on longer texts.
Use it like this:
>> WhatLanguage.new(:all).language('Half the price of a hotel for twice the space')
=> :english
There is also a convenience method on Strings (you may need to require 'whatlanguage/string'
).
>> 'Wir ent...
rhulse's rails-css-views at master - GitHub
This gem is designed to provide CSS views to Rails, and a process to concatenate and minify these files to one file for production.
lib/bundler/capistrano.rb at master from carlhuda's bundler - GitHub
Capistrano task for Bundler.
Install a local Gemfile on a remote server
Call with the server's hostname (and user if you have no SSH agent), e.g.
install-gems-remotely my.server.com
# or without agent:
install-gems-remotely me@my.server.com
When you call it from a rails directory, it uploads your Gemfile
, Gemfile.lock
as well as the gemspecs of all vendored gems in to a temporary folder on the server and does a bundle install
there.
If you need to install gems from anothere Gemfile, just do it like this:
BUNDLE_GEMFILE=Gemfile.something; install-gems-remotely my.server.com
This scri...
Debug Ruby code
This is an awesome gadget in your toolbox, even if your test coverage is great.
-
gem install ruby-debug
(Ruby 1.8) orgem install debugger
(Ruby 1.9) - Start your server with
script/server --debugger
- Set a breakpoint by invoking
debugger
anywhere in your code - Open your application in the browser and run the code path that crosses the breakpoint
- Once you reach the breakpoint, the page loading will seem to "hang".
- Switch to the shell you started the server with. That shell will be running an irb session where you can step thr...
Override e-mail recipients in ActionMailer
Our gem Mail Magnet allows you to override e-mail recipients in ActionMailer so all mails go to a given address.
This is useful for staging environments where you want to test production-like mail delivery without sending e-mails to real users.
Automatically run bundle exec if required
There will probably be better solutions as we become more experienced with using Bundler, and more command line tools become Bundler-aware.
b
will use bundle exec
if there is a Gemfile
in the working directory, and run the call without Bundler otherwise.
b spec spec
This script is part of our geordi gem on github.
Load order of the environment
Rails 3, 4, 5, 6
config/application.rb
-
config/environment.rb
before theinitialize!
call (we don't usually edit this file) - The current environment, e.g.
environments/production.rb
- Gems
- Vendored plugins
- All initializers in
config/initializers/*.rb
-
config/environment.rb
after theinitialize!
call (we don't usually edit this file) - Your own code from
app
Rails 2
- Code in
config/preinitializer.rb
(if it exists) -
environment.rb
, code above theRails::Initializer.run
blo...
Scope to records with a given state in state_machine
The state_machine gem ships with a scope with_state
. This scope has some problems in complex queries or scope chains.
Use this instead:
named_scope :having_state, lambda { |*state_or_states|
state_or_states = Array.wrap(state_or_states).map(&:to_s)
{ :conditions => [ 'articles.state IN (?)', state_or_states ] }
}
If you want a scope with hash options (with the side effects you should know about):
named_scope :having_st...
Bundler for Rails 2.3.x
Update RubyGems and Passenger
Bundler requires Rubygems >= 1.3.6. Run gem update --system
if you have an older version.
It also is not compatible with older versions of passenger, so bring that up to date as well (2.2.15 works).
If you installed RubyGems through apt (which you should never do!), you may see a message giving you a hint to use apt to update.
Some people advise to install the 'rubygems-update-1.3.7' gem on Ubuntu systems if you used apt to install RubyGems.
I did that - and lost all...
Recursively remove unnecessary executable-flags
Sometimes files attain executable-flags that they do not need, e.g. when your Windows VM copies them over a Samba share onto your machine.
From inside your Rails project directory call regularly:
geordi remove-executable-flags
Runs chmod -x
on Ruby, HTML, CSS, image, Rake and similar files.
This script is part of our geordi gem on github.
Boolean attributes and pretty enumerations in Cucumber Factory 1.7
Boolean attributes can now be set by appending "which", "that" or "who" at the end:
Given there is a movie which is awesome
And there is a movie with the name "Sunshine" that is not a comedy
And there is a director who is popular
Instead of "and" you can now also use "but" and commas to join sentences:
Given there is a movie which is awesome, popular and successful but not science fiction
And there is a director with the income "500000" but with the account balance "-30000"
Update with `sudo gem install cucumber_facto...
How Rails and MySQL are handling time zones
When working with times and dates in Rails applications, you need to deal with the following problem:
- In Rails,
Time
objects have a time zone. You can get the zone name by doingtime_object.zone
. - This zone is considered when doing time calculations, e.g. 10 AM CEST minus 8 AM UTC is zero.
- A datetime in MySQL does not have a zone. It just stores the literal string "2010-05-01 12:00:00".
- That means that Rails must make assumptions about timestamps loaded from and written to MySQL.
Rails has two completely different modes ...
Run a POP3 server on a directory of mail files with popthis
popthis is awesome when used with inaction_mailer.
Install the gem:
sudo gem install popthis
Start the server:
popthis tmp/sent_mails/ # e.g. the folder containing the .txt-mails generated by inaction_mailer
Now, configure your mail client as follows:
Server: localhost
Protocol: POP3
Port: 2220
Username: anything
Password: anything
Marry Capybara with SSL-enabled applications
Capybara does not play nice with sites that have some actions protected by SSL, some not. A popular way to implement this in Rails is using the ssl_requirement plugin by DHH, which redirects a requests from HTTP to HTTPS if the requested action requires SSL and vice versa.
Capybara follows the redirect, but seems to forget the changed protocol for the next request. The only hack-free workaround right now is to use URLs in lieu of paths everywhere (links, form actions).
For a hackful fi...
rspec_candy is now a gem
Our awesome collection of rspec helpers (formerly known as "spec_candy.rb") is now available as a gem. It works, it is tested and there will be updates.
Usage
Add rspec_candy
to your Gemfile.
Add require 'rspec_candy/helpers'
to your spec_helper.rb, after the rspec requires.
List of features
Announcing YARD 0.6.0 (gnuu.org)
YARD 0.6 adds the ability to serve documentation for gems as well as the current project with yard server. Just like gem server in RubyGems, you can serve gem docs. The advantage to YARD’s server is that you don’t need to pre-generate the static docs (with a gem install) before running the server. If you installed your gem with --no-rdoc, YARD will just generate it on the fly!
Build a JSON API for a Rails application
Try our Apify gem which solves many problems you will be having.
Error installing the raspell gem
When you get this while installing the raspell gem:
ERROR: Error installing raspell:
ERROR: Failed to build gem native extension.
You need some libraries:
sudo apt-get install libaspell-dev
Aspell Error - No word lists can be found for the language XY
When you get this error:
No word lists can be found for the language "de".
An aspell dictionary is missing. Install it with
sudo apt-get install aspell-de
stefankroes's ancestry at master - GitHub
Ancestry is a gem/plugin that allows the records of a Ruby on Rails ActiveRecord model to be organised as a tree structure (or hierarchy). It uses a single, intuitively formatted database column, using a variation on the materialised path pattern. It exposes all the standard tree structure relations (ancestors, parent, root, children, siblings, descendants) and all of them can be fetched in a single sql query. Additional features are STI support, named_scopes, depth caching, depth constraints, easy migration from older plugins/gems, integrit...