Mute Rails asset pipeline log messages
quiet_assets
helps with disabling asset pipeline log messages in the development log. When the gem is added, asset pipeline logs are suppressed by default.
If you want to disable muting temporarily, add config.quiet_assets = false
to your config/application.rb
.
Disabling Spring when debugging
Spring is a Rails application preloader. When debugging e.g. the rails
gem, you'll be wondering why your raise
, puts
or debugger
debugging statements have no effect. That's because Spring preloads and caches your application once and all consecutive calls to it will not see any changes in your debugged gem.
Howto
Disable spring with export DISABLE_SPRING=1
in your terminal. That will keep Spring at bay in that terminal session.
In Ruby, [you can only write environment variables that subproc...
How to create Rails Generators (Rails 3 and above)
General
- Programatically invoke Rails generators
-
Require the generator, instantiate it and invoke it (because generators are
Thor::Group
s, you need to invoke them withinvoke_all
). Example:require 'generators/wheelie/haml/haml_generator' Generators::HamlGenerator.new('argument').invoke_all
Other ways: Rails invokes its generators with
Rails::Generators.invoke ARGV.shift, ARGV
. From inside a Rails generator, you may call the [inherited Thor methodinvoke(args=[], options={}, config={})
](https://github...
Remove Rubygems deprecation warnings
Rubygems can produce lots of deprecation warnings, but sometimes, you cannot fix them. To have a tidy terminal with output that matters, add this to the top of your Gemfile
and enjoy silence:
Deprecate.skip = true if defined?(Deprecate.skip)
Gem::Deprecate.skip = true if defined?(Gem::Deprecate.skip)
# all gems go here ...
Hash any Ruby object into an RGB color
If you want to label things with a color but don't actually care which cholor, you can use the attached Colorizer
class.
To get a completely random color (some of which will clash with your design):
Colorizer.colorize(some_object) # => "#bb4faa"
To get similiar colors (e. g. bright, pale colors of different hues):
# random hue, saturation of 0.5, lightness of 0.6
Colorizer.colorize_similarly(some_object, 0.5, 0.6) # => "#bbaaaa"
Also see the color gem.
Debugging AJAX requests with better_errors
better_errors
is an awesome gem for enhanced error pages in development, featuring a live-REPL for some light debugging.
To debug the exception you got on an AJAX-Request, visit /__better_errors
on your app's root path (e.g. http://localhost:3000/__better_errors
). It shows the error page for the last exception that occurred, even when it has been triggered by an AJAX request.
No more file type confusion in TextMate2
When using TextMate2 with the cucumber bundle, it does not recognize step definitions (e.g. custom_steps.rb
) as such but believes they are plain Ruby files. But there is help!
Solution
Add these lines to the bottom of your .tm_properties
file (in ~/
for global settings, in any directory for per-project settings):
[ "*_steps.rb" ]
fileType = "source.ruby.rspec.cucumber.steps"
Apparently, this works for any files. Define a regex and specify custom settings. The attached article lists all available configuration options (whic...
Collection of Rails development boosting frameworks
Development environment setup
- Rails Composer
-
Basically a comprehensive Rails Template. Prepares your development environment and lets you select web server, template engine, unit and integration testing frameworks and more.
Generate an app in minutes using an application template. With all the options you want!
Code generators
- Rails Bricks
-
A command line wizard. Once you get it running, it creates sleek applications.
RailsBricks enables you to cre...
String#indent: Know your definitions!
String#indent
is not a standard Ruby method. When you use it, be sure to know where this method comes from. Many Gems shamelessly define this method for internal usage, and you'll never know when it may be removed (since it's usually not part of the Gem's API).
Unless you're using Rails 4 (which brings String#indent
in ActiveSupport), you'll be best of defining it yourself. This card has it for you.
Gems that define String#indent
(incomplete)
----------------------------...
Installing therubyracer and libv8 with Ruby 1.8 on OSX Mavericks
There seems to be no way to use therubyracer -v '0.11.4'
and libv8 -v '3.11.8.17'
on OS X Mavericks.
However, running bundle update therubyracer
worked for me. It installed therubyracer -v '0.12.1'
and libv8 -v '3.16.14.3'
and I had not side effects.
Log of my attempts to get it working
Probably you got here when bundling failed building native extensions for therubyracer
.
The libv8
(3.3.10.4) should have been installed when bundling. Unfortunately it is not building correctly on Mavericks, so the libv8.a
f...
The new Modularity 2 syntax
We have released Modularity 2. It has many incompatible changes. See below for a script to migrate your applications automatically.
There is no does method anymore
We now use traits with the vanilla include
method:
class Article < ActiveRecord::Base
include DoesTrashable
end
When your trait has parameters, use square brackets:
class Article < ActiveRecord::Base
include DoesStripFields[:name, :brand]
end
Note how you ...
Careful with '||=' - it's not 'memoize'
When you do something like this in your code:
def var_value
@var ||= some_expensive_calculation
end
Be aware that it will run some_expensive_calculation every time you call var_value if some_expensive_calculation returns nil.
This illustrates the problem:
def some_expensive_calculation
puts "i am off shopping bits!"
@some_expensive_calculations_result
end
When you set @some_expensive_calculations_result to nil, ||= runs some_expensive_calculation every time....
How to fix "undefined method `name' for Array" error when running bundled commands on Ruby 1.8.7 + Rails 2.3
On recent/fresh installations of Ruby 1.8.7 you may encounter this error why calling any bundled binary (or just bundle exec
):
/home/arne/.rvm/gems/ruby-1.8.7-p374@global/gems/rubygems-bundler-1.4.2/lib/rubygems-bundler/noexec.rb:75:in `setup': undefined method `name' for #<Array:0x7fe04783ef30> (NoMethodError)
from /home/arne/.rvm/rubies/ruby-1.8.7-p374/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in `map'
...
Apparently, this is due to bundler (or maybe the rubygems-bundler
that RVM supplies by default) no lon...
Howto provide a single page preview for PDF & TXT with carrierwave
Assert rmagick
provision ...
Gemfile
gem 'rmagick', '2.13.2' # at this moment the latest stable version
config/initializer/carrierwave.rb
require 'carrierwave/processing/rmagick'
... and define a custom processor
MyUploader.rb
class MyUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
def cover
manipulate! do |frame, index|
frame if index.zero? # take only the first page of the file
end
end
version :preview do
process :cover
process :resize_to_fit => [310,...
Installation of therubyracer fails with "undefined method `include_path'"
Today I ran into trouble installing therubyracer on Ruby 1.8. The installation failed with
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
...
Fix "An error occurred while installing debugger-linecache" with Ruby 1.9.3
You're better off using debugger-ruby_core_source
:
gem install debugger-ruby_core_source
If you can't do this, try the following.
Here is how to fix the following error when installing the debugger
gem fails:
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension
Note: The following example is for a project using Ruby 1.9.3-p448 -- adjust accordingly for your project.
-
Fetch the source for your Ruby version, if you do not yet have it:
rvm fetch ruby-1.9.3-p448
-
Install t...
Geordi: Choose your firefox version for cuc
Geordi 0.16+ supports running selenium tests with project-specific firefox versions.
Just update the gem. It will still default to using the old 5.0.1 firefox. If you want another one, add a file .firefox-version
to your project, containing your preferred version.
geordi cucumber
will prompt (and guide) you to install the given version. You can delete any old installation sitting in /opt/firefox-for-selenium
if you have one.
VCR: Alternative way of mocking remote APIs
If you need to test interaction with a remote API, check out the VCR gem as an alternative to Webmock or stubbing hell.
The idea behind VCR is that is performs real HTTP requests and logs the interaction in a .yml file. When you run the test again, requests and responses are stubbed from the log and the test can run offline.
It's a great way to mock network requests to an external service without going through the pain of log...
mysql2 and older ruby versions
The mysql2 gem in version 0.3.13 might break while compiling on older patch releases of Ruby 1.9.3 within rvm:
*** [err :: server] ruby: symbol lookup error: /path/to/deployment/shared/bundle/ruby/1.9.1/gems/mysql2-0.3.13/lib/mysql2/mysql2.so: undefined symbol: rb_wait_for_single_fd
*** [err :: server] ruby: symbol lookup error: /path/to/deployment/shared/bundle/ruby/1.9.1/gems/mysql2-0.3.13/lib/mysql2/mysql2.so: undefined symbol: rb_wait_for_single_fd
Fixating mysql2 to version 0.3.11 helped.
dusen and edge_rider gems no longer depend on Rails
dusen 0.4.8 and edge_rider 0.2.3 no longer depend on Rails (they still depend on ActiveRecord). That means you can use them e.g. with Sinatra.
"cannot load such file -- nokogiri/nokogiri" (or any other Gem with native extensions on rvm and Ruby >= 2)
After running bundler / gem install I could not load nokogiri lately. It died with cannot load such file -- nokogiri/nokogiri
.
This is not a problem of the gem but is due to a broken native extensions installation routine.
When installing nokogiri manually and with verbose output by using gem install -V nokogiri -v 1.5.6
, you can see the problem scrolling by when the native extension is built:
/usr/bin//install -c -m 0755 nokogiri.so /home/thomas/.rvm/gems/ruby-2.0.0-p247/gems/nokogiri-1.5.6/lib/home/thomas/.rvm/rubies/ruby-2.0.0-p...
Use bundle open to open a gem's code in your $EDITOR
bundle open BUNDLED_GEM
will open the BUNDLED_GEM's source code in your default editor.
Parallel gem installing using Bundler
Bundler 1.4.0 (still beta) can install gems in parallel, making a run of bundle install
much faster.