Installing Rails on a fresh system
- Install Ruby from the Ubuntu repository:
sudo apt-get install ruby ruby-dev\
rubyis the meta package. If you want to explicitly install 1.8 or 1.9, installruby1.8orruby1.9instead (the same applies forruby-dev). - Do not install RubyGems from the repository but install the version from the webpage instead.
- Get Bundler:
sudo gem install bundler
Rails and other gems for a project should now be installed via bundle install from the...
console-for opens a Rails console remotely on a Capistrano deployment target
We're adding a script console-for to open a remote Rails console with one command. Also have a look at shell-for, which this script is relying on.
Run it from any project directory like this, passing a Capistrano multistage deployment target:
console-for staging
This script is part of our geordi gem on github.
Install the SQLite 3 gem for Ruby under Ubuntu
sudo apt-get install sqlite3 libsqlite3-dev
sudo gem install sqlite3-ruby
Hack of the day: A reverse for Rails' &:
Ever wondered if there is a reverse for Rails' .each(&:to_s) idiom? Turns out there is...
You probably already know, that you can abbreviate code like
dogs.each { |dog| dog.bark! }
with
dogs.each(&:bark!)
Now suppose it is the other way round and you have
bones.each { |bone| dog.eat!(bone) }
Good old Ruby already has a solution:
bones.each(&dog.method(:eat!))
Change the timestamp of a file in Ruby
This is somewhat similar to the touch command of Linux:
FileUtils.touch 'example.txt', :mtime => Time.now - 2.hours
If you omit the :mtime the modification timestamp will be set to the current time:
FileUtils.touch 'example.txt'
You may also pass an array of filenames:
FileUtils.touch %w[ foo bar baz ], :mtime => Time.now - 2.hours
Non-existent files will be created.
"no such file to load require_relative (MissingSourceFile)" after installing ruby-debug
If you encounter above mentioned failiure message after installing the ruby-debug gem then you have to explicitly require linecache version 0.43 in your Gemfile.
gem 'ruby-debug'
gem 'linecache', '=0.43'
Why developers should be force-fed state machines
Most web applications contain several examples of state machines, including accounts and subscriptions, invoices, orders, blog posts, and many more. The problem is that you might not necessarily think of them as state machines while designing your application. Therefore, it is good to have some indicators to recognize them early on. The easiest way is to look at your data model.
What’s Up With All These Changes in Rails?
Yesterday, there was a blog post entitled “What the Hell is Happening to Rails” that stayed at the number one spot on Hacker News for quite a while. The post and many (but not most) the comments on the post reflect deep-seated concern about the recent direction of Rails. Others have addressed the core question about change in the framework, but I’d like to address questions about specific changes that came up in the post and comments.
New Cucumber Factory makes it easier to associate records
I pushed a new version of the Cucumber Factory gem. This new release lets you refer to a previously created record by any string attribute:
Given there is a movie with the title "Before Sunrise"
And there is a movie with the title "Limitless"
And there is a movie with the prequel "Before Sunrise"
Note how we didn't have to explicitly give the prequel a name in the example above. This is still possible, but will rarely be necessary now:
Given "Before Sunrise" is a movie with...
A nicer way to run RSpec and/or Cucumber
geordi, our collection of awesome shell scripts, has been extended by three scripts to help you call RSpec or Cucumber:
cuc
This script runs Cucumber the way you want it:
- Prints some line feeds to easily find your test results when you come back to the console later
- Configures Cucumber to use cucumber_spinner if it is available in your
Gemfile - Runs Cucumber under
bundle exec - Uses an old version of Firefox for Selenium (Javascript) features...
How to test resource_controller hooks
When using the resource_controller gem you often hook onto events like this:
update.before do
do_something
end
For testing such things in your controller you should -- as always -- not trigger something that eventually calls the thing you want.\
Instead, in your specs, have resource_controller run those hooks like it does itself. Like that:
describe 'before update' do
...
Open a new tab with the current directory on a mac
I found a nice script on crazylittlehacks and modified it slightly. Put the attachment to /usr/local/bin, chmod +x and run it like this:
tab
You may also pass a command that will be run in that newly opened tab:
tab ruby script/server
Note: This does not play too nice with Visor, because Visor will always be "window 1" for AppleScript. By modifying the script and replacing in window 1 with `in window...
Dump a two-dimensional array to an Excel .xls spreadsheet
Copy the attached Ruby code to config/initializers, or paste it into your IRB console. You can now dump any two-dimensional array to an Excel .xls spreadsheet with a single method call:
table = [['user', 'email', 'hours']]
User.all.each do |user|
table << [user.name, user.email, user.hours]
end
table.dump_to_excel('users.xls')
The first row in the array will be dumped as the table head in bold type.
You need the spreadsheet gem in your Gemfile.
Note that there is a [limit of 65535 rows](https://makandracard...
Gem Versioning and Bundler: Doing it Right
When running an executable, ALWAYS use bundle exec. In some cases, running executables without bundle exec may work, if the executable happens to be installed in your system and does not pull in any gems that conflict with your bundle. However, this is unreliable and is the source of considerable pain. Even if it looks like it works, it may not work in the future or on another machine.
Execution of shell code in Ruby scripts
Deprecated ways to execute shell code in Ruby
This is just a reference for legacy code. For new code, always use capture3.
%x{ } or backticks – quick and easy
Returns the standard output of running the given command in a subshell. This is an alias for `...`, and you can use string interpolation.
Example:
name = 'ls'
result = `which #{name}`
It does not escape anything you inject in the string, so be aware of possible security vulnerabilities...
How to employ and run your tests with parallel_tests to speed up test execution
When your cucumber features grow massively over time, the test execution can take a lot of time.
One easy way to speed up your test execution is to use the parallel_tests gem.
It comes along with some useful rake tasks that let you setup your local test environment shortly to run your features, specs or unit-tests in parallel.
Follow these steps to get it to work.
-
Add the parallel_tests gem to your
Gemfiletest sections like that:# ./Gemfile group :development, :test do
...
Script to create and copy a production dump to your project root
Soon after having written our shell-for script, we wanted to easily get dumps of our productions machines, too. This is how we do it:
dump-for staging [-s]
It will copy the dump to your project's tmp directory and name it according to the capistrano stage you're calling for, here: staging.dump. When you pass the optional -s option, the dump will automatically been sourced into your local development database.
This script ...
Releasing geordi
After having written useful scripts into makandra notes for a long time, we’ve now tied them into a powerful new gem: geordi.
It gives you the power to
- get a dump from your production database with
dump-for production - install your local gems to the production machine with
install-gems-remotely - execute rake tasks to several environments at the same time with
power-rake db:migrate - and much more
Faker is now I18n aware, ships with unicorns
Recent versions of the Faker gem retrieve their strings from your locale file (e.g. config/locale/de.yml). This leads to awesome errors like this:
undefined method `shuffle' for "translation missing: de.faker.lorem.words":ActiveSupport::SafeBuffer
To fix this, copy the contents of a Faker locale file into your config/locale/de.yml.
This is stupid.
Updating a gem created with Bundler
Since May 2011 we are cutting new gems using Bundler, which is less painful than cutting gems using Jeweler. You know a gem was cut using Bundler if you see the word Bundler in a gem project's Rakefile.
This is how to update a gem that was cut using Bundler:
- Say
git pullor check out a repository from Github likegit clone git@github.com:makandra/geordi.git - Update the gem version in `lib/project...
Test that a number or money amount is shown with Cucumber
This is an awful way to test whether a number is shown on the screen:
Then I should see "5"
It is awful because the step above is green for 5, 5123 and -51.
This step definition below makes sure this doesn't happen. You can use it like this:
Then I should see the number 5
The step also works if you you'd like to test that the number is followed by a unit:
Then I should see the amount 5 €
The separator between the number and its unit is allowed to be either a space or a [nbsp](https://makandracards.com/makandra/838-generate...
Rails 3.1: Release candidate
Asset pipeline, HTTP streaming, jQuery as default framework, auto-reversable migrations, identity map for ActiveRecord.
Ruby 1.8.x support will be dropped with or after Rails 4.
Sudo a gem executable does not work on Ubuntu
Today I needed to execute a ruby gem executable with sudo. But, surprisingly, bash would tell me command not found for the gem that ran lovely without sudo.
Gem bins are installed to /var/lib/gems/1.8/bin, which is not in sudo’s PATH. Unfortunately, you can’t change the path, since sudo for Ubuntu is compiled with the --with-secure-path option.
#Solution A: symlink the gems (if you need only some few gems)
- for each gem you need for
sudo, run `ln -s /var/lib/gems/1.8/bin/gem_for_sudo /usr/local/bin/gem_for_sudo
#Soluti...