Hover an element with Capybara < 2
You need this awkward command:
page.driver.browser.action.move_to(page.find(selector).native).perform
Note that there are better ways for newer Capybaras.
Ruby 2.1 returns a symbol when defining a method
Since Ruby 2.1, defining a method returns its name as a Symbol:
def foo() end # => :foo
define_method :foo do end # => :foo
You can use this to do Python-like decorators like so:
private def foo; end
memoize def foo; end
Ruby 2.0 introduces keyword arguments
"Keyword arguments" allow naming method arguments (optionally setting a default value). By using the double-splat operator, you can collect additional options. Default values for standard arguments still work (see adjective
).
def greet(name, adjective = 'happy', suffix: '!', count: 7, **options)
greeting = options[:letter] ? 'Dear' : 'Hello'
puts "#{greeting} #{adjective} #{name + suffix * count}"
end
Invoke the method like this:
greet('Otto', 'sad', suffix: '??', count: 9, include_blank: true)
In Ruby 2.1+,...
Assignable_Values 0.11.0 released
Introduces :include_old_value option to :assignable_xxx method.
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....
Atomic Grouping in regular expressions
A little-known feature of modern Regexp engines that help when optimizing a pattern that will be matched against long strings:
An atomic group is a group that, when the regex engine exits from it, automatically throws away all backtracking positions remembered by any tokens inside the group.
rbenv: How to update list of available Ruby versions on Linux
When you tell rbenv to install a Ruby it does not know about, you will get an error message.
$ rbenv install 2.1.2
ruby-build: definition not found: 2.1.2
You can list all available versions with `rbenv install --list'.
If the version you're looking for is not present, first try upgrading
ruby-build. If it's still missing, open a request on the ruby-build
issue tracker: https://github.com/sstephenson/ruby-build/issues
(Fun fact: Recent versions of ruby-build will give you a more helpful error message which...
EdgeRider 0.3.0 released
EdgeRider 0.3.0 adds support for Rails 4.1 and Ruby 2.1. It forward-ports ActiveRecord::Base.scoped
to Rails 4.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]
...
Silence specific deprecation warnings in Rails 3+
Sometimes you're getting an ActiveSupport deprecation warning that you cannot or don't want to fix. In these cases, it might be okay to silence some specific warnings. Add this to your initializers, or require it in your tests:
silenced = [
/Not considered a useful test/,
/use: should(_not)? have_sent_email/,
] # list of warnings you want to silence
silenced_expr = Regexp.new(silenced.join('|'))
ActiveSupport::Deprecation.behavior = lambda do |msg, stack|
unless msg =~ silenced_expr
ActiveSupport::Deprecation::DEFAULT_BEHAVI...
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...
patbenatar/jquery-nested_attributes
jQuery plugin that makes it easy to dynamically add and remove records when using ActiveRecord's nested attributes.
Discarding cached SQL query results in ActiveRecord
ActiveRecord caches results of SQL queries. If you want to discard the cached results for one model, you can call MyModel.connection.clear_query_cache
.
A saner alternative to SimpleForm's :grouped_select input type
SimpleForm is a great approach to simplifying your forms, and it comes with lots of well-defined input types. However, the :grouped_select
type seems to be overly complicated for most use cases.
Example
Consider this example, from the documentation:
form.input :country_id, collection: @continents,
as: :grouped_select, group_method: :countries
While that looks easy enough at a first glance, look closer. The example passes @continents
for a country_id
.\
SimpleForm actua...
Lightweight PDF viewer: MuPDF
MuPDF is a PDF reader that renders very quickly, yet still correctly.
It supports PDF 1.7 and all the fancy shenanigans that evince (Ubuntu's default PDF reader) fails to render properly.
On Ubuntu, MuPDF is available in the Universe sources. Simply install via APT:
sudo apt-get install mupdf
Interaction primarily happens via keyboard, but there is basic mouse support.\
See the manpage for more details on navigating.
One downside: There is no printing support, so if...
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...
Your First AngularJS App: A Comprehensive Tutorial
This is a great tutorials for beginners and intermediate AngularJS developers. It covers a lot of ground, including routing and data transfer between client and server.
Caching in Rails
The information in this card is only relevant for Rails 2.3-era apps.
This note gives a quick introduction into caching methods (page caching, action caching and fragment caching) in rails and describes some specific problems and solutions.
The descriptions below are valid for Rails 2 and 3. Recently, caching with timestamp- or content-based keys has become more popular which saves you the pain of invalidating stale caches.
How to enable/disable caching
To enable or disable caching in rails you ca...
When using "render :text", set a content type
When your Rails controller action responds with only a simple text, render text: 'Hello'
may not be what you want. You should not even use it on Rails 4.1+ any more.
By default, a "text" response from a Rails controller will still be a sent as text/html
:
render text: 'Hello'
response.body # => "Hello"
response.content_type # => "text/html"
While this may not be too relevant for a Browser client, the response's content type is simply wrong if you want to send a plain-text response, and can cause trouble. \
For example, con...