What we know about PDFKit
What PDFKit is
- PDFKit converts a web page to a PDF document. It uses a Webkit engine under the hood.
- For you as a web developer this means you can keep using the technology you are familar with and don't need to learn LaTeX. All you need is a pretty print-stylesheet.
How to use it from your Rails application
- You can have PDFKit render a website by simply calling
PDFKit.new('http://google.com').to_file('google.pdf')
. You can then send the...
Interacting with a Microsoft Exchange server from Ruby
Microsoft Exchange service administrators can enable Exchange Web Services (EWS) which is a rather accessible XML API for interacting with Exchange. This allows you to read and send e-mails, create appointments, invite meeting attendees, track responses, manage to-do tasks, check user availability and all other sorts of things that are usually only accessible from Outlook.
You can implement an EWS by hand-rolling your XML (the [docs](http://msdn.microsoft.com/en-us/...
Feedjira
Great gem to consume RSS feeds. I was missing some features on Ruby's RSS::Parser
that I found in Feedjira:
- Speed
- Does not break on slightly malformed RSS feeds (like a missing
length
attribute on an<enclosure>
tag on gizmodo.de's feed) - It automatically resolves Feedburner-mangled URLs (hooray!)
The GitHub project has only a minimalistic readme. You can find its documentation on their homepage.
gazay/ids_please
Parses URLs of social networks to extract IDs or screen names.
It does not get confused by child routes: you may also pass URLs like a user's twitter photo stream and the gem will extract their twitter ID .
Note that it just parses URLs, and does nothing magic like looking up IDs when the URL contains only a screen name (e.g. the Instagram API requires you to send the user ID almost always while you at first only know their screen name).
Use byebug on Ruby 2+
The debugger
gem does not seem to be properly working on Ruby 2. Use byebug
instead!
Byebug is a simple to use, feature rich debugger for Ruby 2. It uses the new TracePoint API for execution control and the new Debug Inspector API for call stack navigation, so it doesn't depend on internal core sources. It's developed as a C extension, so it's fast. And it has a full test suite so it's reliable. Note that byebug works only for ruby 2.0.0 or newer. For...
Rails Assets
Automatically builds gems from Bower packages (currently 1700 gems available). Packaged Javascript files are then automatically available in your asset pipeline manifests.
Why we're not using it
At makandra we made a choice to use bower-rails instead. While we believe Rubygems/Bundler to be superior to Javascript package managers, we wanted to use something with enough community momentum behind it that it won't go away in 10 years...
whenever: Make runner commands use bundle exec
In whenever you can schedule Ruby code directly like so:
every 1.day, :at => '4:30 am' do
runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
end
Combined with the best practice to hide background tasks behind a single static methods you can test, this is probably preferable to defining additional Rake tasks.
Unfortunately when whenever register a runner command, it doesn't use bundle exec
in the resulting crontab. This gets you errors like this:
`gem_original_require': no suc...
shoulda-matcher methods not found in Rails 4.1
So you're getting an error message like the following, although your Gemfile lists shoulda-matchers
and it has always worked:
NoMethodError:
undefined method `allow_value' for #<RSpec::ExampleGroups::Person::Age:0x007feb239fa6a8>
This is due to Rails 4.1 (specifically, Spring) revealing a weak point of shoulda-matchers
-- jonleighton explains why.
Solution
The solution is to follow [the gem's installation guide](https://github.com/thoughtbot/sh...
3 ways to run Spring (the Rails app preloader) and how to disable it
spring ...
The most obvious way to use spring is to call it explicitly:
spring rails console
spring rake db:migrate
Binstubs
Binstubs are wrapper scripts around executables. In Rails they live inside bin/
. If you run spring binstub --all
, your binstubs will be using Spring.
bin/rails console
bin/rake db:migrate
bundle exec rails ...
Bundle exec
is inconsistent when it comes to spring. Some commands will use it, some won't.
bundle exec rails console # starts Spring...
How to test bundled applications using Aruba and Cucumber
Aruba is an extension to Cucumber that helps integration-testing command line tools.
When your tests involve a Rails test application, your tool's Bundler environment will shadow that of the test application. To fix this, just call unset_bundler_env_vars
in a Cucumber Before block.
Previously suggested solution
Put the snippet below into your tool's features/support/env.rb
-- now any command run through Aruba (e.g. via #run_simple
) will have a clean Bundler envir...
mattheworiordan/capybara-screenshot
Using this gem, whenever a Capybara test in Cucumber, Rspec or Minitest fails, the HTML for the failed page and a screenshot (when using capybara-webkit, Selenium or poltergeist) is saved into $APPLICATION_ROOT/tmp/capybara.
Link via Binärgewitter Podcast (German).
If Guard takes forever to start...
For me guard recently took a very long to start (as in "minutes"), because I had lots of images in public/system
.
Upgrading the listen
gem (which is a dependency) to 2.7.7 fixed this.
assignable_values 0.11.0 can return *intended* assignable values
As you know, assignable_values does not invalidate a record even when an attribute value becomes unassignable. See this example about songs:
class Song < ActiveRecord::Base
belongs_to :artist
belongs_to :record_label
assignable_values_for :artist do
record_label.artists
end
end
We'll create two record labels with one artist each and create a song for one artist. When we change the song's record label, its artist is still valid.
makandra = RecordLabel.create! name: 'makandra records'
dominik...
Things to consider when using Travis CI
Travis CI is a free continuous integration testing service. However, it is really fragile and will break more than it will work.
If you choose to use it anyway, learn the lessons we already learnt:
Use a compatible Rubygems for Rails 2.3 on Ruby 1.8.7
Ruby 1.8.7 is not compatible with current Rubygems versions (> 2.0). Runnig rvm rubygems latest-1.8 --force
will fix this and install Rubygems version 1.8.29.
To make Travis CI do this, add `before_script: rvm rubygems latest-1....
How to remove RSpec "old syntax" deprecation warnings
RSpec 3.0 deprecates the :should
way of writing specs for expecting things to happen.
However, if you have tests you cannot change (e.g. because they are inside a gem, spanning multiple versions of Rails and RSpec), you can explicitly allow the deprecated syntax.
Fix
Inside spec/spec_helpber.rb
, set rspec-expectations
’ and/or rspec-mocks
’ syntax as following:
RSpec.configure do |config|
# ...
config.mock_with :rspec do |c|
c.syntax = [:should, :expect]
...
Removing MiniTest warnings from Rails 4 projects
Warnings like those below may originate from rspec
or shoulda-matchers
or other gems that have not updated yet to the new MiniTest API.
One
Warning: you should require 'minitest/autorun' instead.
Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'
# (backtrace)
Solution: Add gem 'minitest'
to your Gemfile, before any rspec gem.
Another
MiniTest::Unit::TestCase is now Minitest::Test. From /Users/makandra/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/1.9.1/tes...
A Ruby script that installs all gems it is missing
So you want your Ruby script to install missing gems instead of dying? Take this method:
def installing_missing_gems(&block)
yield
rescue LoadError => e
gem_name = e.message.split('--').last.strip
install_command = 'gem install ' + gem_name
# install missing gem
puts 'Probably missing gem: ' + gem_name
print 'Auto-install it? [yN] '
gets.strip =~ /y/i or exit(1)
system(install_command) or exit(1)
# retry
Gem.clear_paths
puts 'Trying again ...'
require gem_name
retry
end
Use it like this:
insta...
Fix Rubygems error: undefined method `source_index' for Gem:Module
You are probably using Ruby 1.8.7 with a too recent versions of Rubygems.
Downgrade your Rubygems to the latest version that works with 1.8.7.
See also
- Fix Rubygems warning: Gem.source_index is deprecated, use Specification
- Fix Rubygems binary error: undefined method `activate_bin_path' for Gem:Module (NoMethodError)
- [Bundler: Gemfile.lock is corrupt & gems are missing from the DEP...
Strong params: Raise in development if unpermitted params are found
Rails 4:
config.action_controller.action_on_unpermitted_parameters
enables logging or raising an exception if parameters that are not explicitly permitted are found. Set to :log
or :raise
to enable. The default value is :log
in development and test environments, and false in all other environments.
Rails 3:
If you include the strong_params
gem, see the Readme for handling unpermitted keys.
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.