Mailcatcher: An alternative to inaction_mailer
Looks simpler than inaction_mailer:
gem install mailcatcher
mailcatcher
Setup Rails to send mails to 127.0.0.1:1025. Usually you want the following config in config/environments/development.rb
and maybe in test.rb
or cucumber.rb
.
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'localhost',
:port => 1025
}
Now you can see sent mails in your browser when opening http://127.0.0.1:1080
Note: In order to s...
Start Rails console or server with debugger
When you require the Ruby debugger to be available from your Rails console (e.g. you want to inspect a method's magic), you need to enable it explicitly:
script/console --debugger
If you cannot access local variables etc, see this card.
WEBrick
For WEBrick, enable it similarly:
script/server --debugger
Properly adding fields with default values to a model
When adding a new field to your model's database table, don't set any defaults in the database.
It makes people wonder why they get such values when reading attributes.\
Why? Because nobody looks at the database layout since such things are part of your application's logic -- and thus they belong into the corresponding model.
How to
Do it like this:
-
In your migration, after adding the field, update all fields to your desired default:
update "UPDATE users SET locked = #{quoted_false};"
-
In your model, set a defau...
Install the Oniguruma gem
Oniguruma is an advanced regular expression engine for Ruby.
Install Oniguruma with binary dependencies like this:
sudo apt-get install libonig2 libonig-dev
sudo gem install oniguruma
On the Mac do:
brew install oniguruma
sudo gem install oniguruma
MySQL: For each group, retrieve a comma-separated list of values in a given column
The technique described in this card has an important caveat: The result of GROUP_CONCAT
is truncated to the maximum length that is given by the group_concat_max_len
system variable, which has a default value of 1024. This will cause horrible, data-destroying bugs in production. For this reason you should probably not use GROUP_CONCAT
ever. At least you must set the value of group_concat_max_len
to an insanely high value on every database server your application runs on.
Lik...
Using heredoc for prettier Ruby code
You can use heredoc to avoid endlessly long lines of code that nobody can read. Heredoc strings preserve linebreaks and can be used like this:
def long_message
puts(<<-EOT)
Here goes a very long message...
Sincerely,
foobear
EOT
end
<<-EOT
will be somewhat of a placeholder: anything you write in the line after you used it will be its value until you write EOT
in a single line.
You can use any string to flag your heredocs. To be more verbose you...
How to fix: Gems are unavailable although they are installed
- If Rails or Rake are complaining about a missing gem that is listed in your
Gemfile.lock
and the listed version is properly installed, something is seriously wrong and needs to be fixed. - If you accidently executed
bundle install some_gem
although you wantedbundle update some_gem
What is wrong
Let's say your Gemfile
asks for some-gem
which you can see when running gem list
but bundle show some-gem
just gives you an error:
Could not find gem 'some-gem', in any of the sources
Another indicator: Doing a `...
Use a Ruby method like a block or lambda
Sometimes you want to use a vanilla Ruby method like a block. You can use Object#method
to obtain a method reference that responds to #call
:
foo_plus = "foo".method(:+)
foo_plus.call("bar") # => "foobar"
The method reference also understands #to_proc
so you can feed it to block-taking methods by prefixing it with &
:
printer = method(:puts)
[1, 2, 3].each(&printer) # will print one line per number
Selenium WebDriver 2.5.0, 2.6.0 fails when selecting options from select boxes
We are consistently having trouble with selenium-webdriver > 2.5.0
where whenever we try to select an option from a <select>
Capybara complains:
No such option 'Foo' in this select box. Available options: 'Foo', 'Bar', 'Baz' (Capybara::OptionNotFound)
This seems to happen with both old and new versions of Firefox. Our workaround so far is to freeze the gem at version 0.2.2
.
sstephenson/execjs - GitHub
ExecJS lets you run JavaScript code from Ruby. It automatically picks the best runtime available to evaluate your JavaScript program, then returns the result to you as a Ruby object.
Auto-generate state_machine graphs as PNG images
The state_machine gem comes with a rake task that lets you generate PNG graphs from any model using state_machine
.
Install the required dependencies like this:
sudo apt-get install graphviz
sudo gem install ruby-graphviz
You can now generate a graph like this:
rake state_machine:draw CLASS=ModelUsingStateMachine
Replace ModelUsingStateMachine
with the name of your model class.
If it the raketask does not exist for you, add to Rakefile
(in your pr...
Defining custom errors in Ruby
class Errormaster
CoffeeIsOut = Class.new(StandardError)
# is prettier than
class CoffeeIsOut < StandardError; end
end
Reference such an error class with Errormaster::CoffeeIsOut
.
How to install a frozen version of Firefox for your Selenium tests
Whenever Firefox updates, all your Cucumber features that use Selenium break. This is annoying.
In order to remedy this, version 0.5.0 of our geordi gem comes with a script that helps you create an unchanging version of Firefox for your Selenium tests. In particular, this new copy of Firefox will have the following properties:
- It won't update itself with a newer version
- It can co-exist with your regular Firefox installation (which you can update at will)
- It will use a profile separate from the one...
mojombo/grit - GitHub
Grit gives you object oriented read/write access to Git repositories via Ruby.
Rails 3.1.0 has been released!
jQuery as new default Javascript library, streaming response support, attr_accessible with roles, prepared statements, easier migrations.
Ruby: Convert a time string to your local time zone
If you have a time given in a different time zone than your local one, parsing will convert it for you:
>> Time.parse('September 2nd, 3pm PST')
=> 2011-09-03 01:00:00 +0200
Note that in pure Ruby you need to require "tzinfo"
(Ruby 1.9) or require "time"
(Ruby 1.8) for Time.parse
to be available.
Fix: "undefined method `bytesize' for #<Array>"
I believe that when WEBrick has trouble bringing up your Rails application, the WEBrick component that is supposed to print you a pretty error message has a bug and sometimes fails with this message:
"undefined method `bytesize' for #<Array>"
Starting the application in Passenger gave me a stacktrace in log/development.log
that pointed to the actual problem.
Possible causes discovered by looking at the logs
-----------------------------------------------------...
How to use Rails URL helpers in any Ruby class
If you have any class which requires access to some path methods generated by your routes. Even though you could technically include Rails.application.routes.url_helpers
, this may include way too many methods and even overwrite some class methods in the worst case.
Instead, most of the time the following is advised to only make the desired methods available:
class Project
delegate :url_helpers, to: 'Rails.application.routes'
def project_path
url_helpers.project_path(self)
end
end
Invoices: How to properly round and calculate totals
While it might seem trivial to implement an invoice that sums up items and shows net, gross and vat totals, it actually involves a lot of rules and caveats. It is very easy to create invoices where numbers don't add up and a few cents are missing. A missing cent is a big deal for an accountant, so it is important for your invoices to list correct numbers.
Note that this is not legal advice. Also note that while this note has a number of code examples in Ruby and MySQL, the concepts apply to all programming languages and data stores.
When ...
Always show all form errors during development
You've been there: A form cannot be submitted, but you don't see a validation error because the field at fault has no corresponding input field on the form. Because this is usually a bug, you insert debug information listing all errors into the form view. And once the bug is fixed, you forget to take out that debug information.
There is a better way. By copying one of the attached initializers into config/initializers
, your forms will always render a small box listing all form errors in the bottom right corner of the screen. This box is n...
Time#utc, Time#gmt and Time#localtime are destructive methods
Calling Time#utc
, Time#gmt
or Time#localtime
will not create a converted copy. Instead these methods modify the receiving Time
object:
>> time = Time.now
=> Thu Aug 25 09:52:28 +0200 2011
>> time.utc
=> Thu Aug 25 07:52:28 UTC 2011
>> time
=> Thu Aug 25 07:52:28 UTC 2011
This can have unexpected side effects when other code is holding pointers to the Time
object you are modifying. To be safe, call these methods on a clone of the Time
object. You can clone a Ruby object by using [#dup
](http://...
Fix Rubygems warning: Gem.source_index is deprecated, use Specification
After updating Rubygems you see a wall of deprecation warnings like this:
NOTE: Gem::SourceIndex#add_spec is deprecated, use Specification.add_spec. It will be removed on or after 2011-11-01.
Gem::SourceIndex#add_spec called from /usr/local/lib/site_ruby/1.8/rubygems/source_index.rb:197.
NOTE: Gem::SourceIndex#add_specs is deprecated with no replacement. It will be removed on or after 2011-11-01.
Gem::SourceIndex#spec_dirs= called from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.14/lib/bundler/rubygems_integration.rb:175
...
Random list of ActiveSupport goodies
I recently browsed through the ActiveSupport code and found some nice stuff I did not know about:
ActiveSupport::Callbacks
-
ActiveRecord-like callbacks, if you need callbacks in non ActiveRecord objects
ActiveSupport::MessageEncryptor
-
encrypt and decrypt ruby objects
[ActiveSupport::MessageVerifier
](https://github.com/rails/rails/blob/mast...
Ruby GetText will eval scripts containing ActiveRecord classes
When the Ruby parser module of Ruby-GetText comes across a file in one of its search directories (e.g. lib/scripts/
) and finds out that you are defining ActiveRecord classes inside it, it evaluates the whole file. Here is how to avoid that.
What's happening?
Let's say you have the following script which is only run once, manually, via script/runner
:
# lib/scripts/doomsday.rb
class User < ActiveRecord::Base; end
User.destroy_all
In that case we ...