Dealing with "TypeError: Converting circular structure to JSON" on JavaScript
JavaScript structures that include circular references can't be serialized with a"plain" JSON.stringify. Example:
a = { name: 'Groucho' };
b = { name: 'Harpo', sibling: a };
a.sibling = b;
Doing a JSON.stringify(a) will throw an error:
TypeError: Converting circular structure to JSON
There is not much you can do about that except specifying a custom serializer function that detects and cleans up circular references. There are existing solutions so you do not need to think of one yourself, like <https://githu...
Deterministic ordering of records by created_at timestamp
Creating records in specs can be so fast that two records created instantly after one another might have the same created_at timestamp (especially since those timestamps don't have an indefinitely high resolution). When ordering lists by timestamps, you should therefore always include a final order condition using the primary key of the table.
class Photo < ActiveRecord::Base
scope :by_date, -> { order('created_at DESC, id DESC') }
end
Photo.by_date
Remember to include the id field in the database index.
CoffeeScript: How to instantiate a class with an attributes hash
This may be hard to find in the docs, but if you want CoffeeScript classes that instantiate their properties from a hash of attributes (like ActiveRecord), do it like this:
class User
constructor: ({ @name, @email }) ->
# empty contstructor is fine, CoffeeScript will do the magic.
Example:
var batman = new User(name: 'Bruce Wayne', email: 'batman@example.com')
batman.name # => 'Bruce Wayne'
Sending raw JSON data to a member action in a controller spec
This is what worked for me in a Rails 4:
# JSON data as first argument, then parameters
patch :update, { some: 'data' }.to_json, id: id, format: :json
Tagging in Rails 4 using Postgres Arrays
Usage:
class Document < ActiveRecord::Base
scope :any_tags, -> (tags){ where('tags && ARRAY[?]', tags) }
scope :all_tags, -> (tags){ where('tags @> ARRAY[?]', tags) }
end
Document.create(title: "PostgreSQL", tags: ["pg","rails"])
Document.any_tags('pg')
Document.all_tags(['pg', 'rails'])
Migration:
class CreateDocuments < ActiveRecord::Migration
def change
create_table :documents do |t|
t.string :title
t.string :tags, array: true, default: []
t.timestamps
end
add_index :documents, :ta...
Iterate over any enumerable with an index
tl;dr: Use with_index
ActiveRecord's find_each with index
If you do not provide a block to find_each, it will return an Enumerator for chaining with other methods:
Person.find_each.with_index do |person, index|
person.award_trophy(index + 1)
end
Ruby's map with index
Similarly, you may need an index when using other methods, like map, flat_map, detect (when you need the index for detection), or similar. Here is an example for map:
people...
Wrapping Your API In A Custom Ruby Gem
Nice tutorial about packaging Ruby bindings to your API in a Ruby gem, with tests using VCR casettes.
Eager-loading polymorphic associations
To avoid n+1 queries, you want to eager-load associated records if you know you need to access them later on.
The Rails docs say:
Eager loading is supported with polymorphic associations.
This is true, but has some caveats.
Example
Consider the following models:
class Image < ActiveRecord::Base; end
class Video < ActiveRecord::Base; end
class PageVersion < ActiveRecord::Base
belongs_to :primary_medium, polymorphic: true # may be Image or Video
end
class Page < ActiveRecord::Base
belongs_to ...
Capybara will not find links without an href attribute
Capybara and most assistive technology will fail to find <a> tags that are missing an href attribute. This will probably happen to you every now and then on JavaScript-heavy applications.
An example would be an AngularJS application where the following HTML actually works. [1]
<a ng-click="hello()">Hello</a>
Capybara will fail to find that link, even though looking it up via the DOM shows it:
>> find_link("Hello")
Capybara::...
Linux: Running a program with a different locale than your default
When your system is not running on English, you may sometimes want to run some applications and not use your system locale.
Use cases are scripts that parse output, or just using the (possibly) more common English labels or error messages. Here is how to do that.
I will use the date command and print the current weekday, just for the sake of an example.
Changing the locale using environment variables
Most often, setting LC_ALL for your command should be enough. The following was run on a system using a German locale.
$ date +%...
RSpec example groups can be named using symbols
Though nowhere to be found in the official docs, this works just fine.
describe Facebook::Post do
it_behaves_like :time_series
end
shared_examples_for :time_series do
# shared example code
end
Upgrade to LibreOffice 5.x on Ubuntu
Getting rid of your old LibreOffice
Remove your old LibreOffice:
sudo apt-get remove libreoffice*
You probably also want to get rid of the dead PPAs you might have installed for LibreOffice 4.x:
- Open *Ubuntu Software Center
- Go to Edit / Software Source ... / Other Software
- Find and uncheck lines like http://ppa.launchpad.net/libreoffice/libreoffice-4-3/ubuntu. Note that you will probably have at least two lines pertaining to libreoffice.
Installing a new LibreOffice
-------------...
Rails routes: Extracting collection actions into their own controllers
Let's say you have two screens:
- Show a given project
- Show a report for all projects
Ideally you want both screens to be handled by different controllers like this:
GET /projects/:id => ProjectsController#show
GET /projects/report => Projects::ReportsController#show
What seems like a simple requirement is a little awkward to configure in your routes.
Obviously the report should be a singleton resource, but how can we nest it into the Projects:: namespace?
What does not work is this:
resources :proj...
Rspec: Complex argument expectations for should_receive
Sometimes you need complex expectations on method arguments like this
SomeApi.should_receive(:find).with(:query => '*foo*', :sort => 'timestamp ASC', :limit => 100).and_return(['some result'])
This is not very flexible, and failure messages will be hard to read.
Instead, consider doing this:
SomeApi.should_receive(:find) do |params|
params[:query].should == '*foo*'
params[:sort].should == 'timestamp ASC'
params[:limit].should == 100
['some result']
end
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...
Testing drag&drop with Selenium
When using jQueryUI's Sortable plugin (either directly or via Angular's ui.sortable), you might struggle testing your nice drag&drop GUI since Selenium webdriver does not support native dragging events.
But jQueryUI uses jquery.simulate for their testing, so why shouldn't you? There is even an extension to it that makes testing drag & drop quite easy.
Here is what you need:
jquery.simulate.js- [`jquery.simula...
jQuery: Work with text nodes and comment nodes
Nearly all jQuery traversal functions ignore elements that are not HTML tags.
To work with other type of nodes (like text, comment or CDATA sections) you need to:
- Retrieve child nodes
contents()(which behaves likechildren()except that it returns all types of child nodes) - Filter manually using either plain Javascript or jQuery's
filter()method
Example
Let's write a function that takes a jQuery element and returns an array of all child nodes that are text nodes:
function selectTextNodes($container) {
retu...
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/...
Your Cronjobs should not rely on a perfect schedule
Due to network or hardware failures, it can happen that one of your cronjobs will not run at the time you specify in the schedule. Your code should be built in a way that it can be re-run at a later time (when the failure is resolved).
For example, if you are synchronizing data with another service once every day, your cronjob should not only synchronize changes from the last 24 hours. If you do this and a network failure will delay the execution of your job by 5 hours, you will only synchronize changes from hour 6-29, but forget change...
Machinist blueprints: Do not set associations without blocks
TL;DR In blueprints, always wrap associations in blocks.
# Broken
Task.blueprint(:vacation) do
project Project.make(:vacation)
hours 8
accounting_method 'none'
end
# Correct
Task.blueprint(:vacation) do
project { Project.make(:vacation) }
hours 8
accounting_method 'none'
end
Without the block, Project.make will only run once when the blueprint is parsed (usually when RSpec is loaded), which is not what you want.
Cucumber: Skipping steps in a scenario outline, based on the current example
In Cucumber, scenario outlines help avoiding tests that are basically the same, except for a few variables (such as different inputs). So far, nothing new.
The problem
Now what if your test should (or should not) do something, like filling in a field only for some tests?
Scenario Outline: ...
When I open the form
And I fill in "Name" with "<name>" # <= we want to do this only occasionally
Then everybody should be happy
Examples:
| name |
| Alice |
| Bob |
You could o...
Rails 4 drops support for the :assets group in Gemfile
Previously the assets group existed to avoid unintended compilation-on-demand in production. As Rails 4 doesn't behave like that anymore, it made sense to remove the asset group.
RSpec 3 no longer chooses a spec's type based on its directory
While RSpec 1 and 2 decided that specs inside spec/model are model specs, and those inside spec/features are feature specs (and so on), RSpec 3 will no longer do that by default.
This will result in errors such as missing routing helpers, etc.
There are 2 ways to fix this:
-
Explicitly set the type on each spec. For example:
describe '...', type: 'feature' do # ... end -
Add this to your
spec_helper.rb(inside theRSpec.configureblock) to restore the old behavior:...
Test if all your favicons exist
When you don't only have a favicon.ico in your project but also PNGs of different sizes and backgrounds, you should test if all those files are actually reachable.
Here are a few selectors to get you started:
'link[rel~="icon"]' # regular ones, matches "shortcut icon" and "icon"
'link[rel="apple-touch-icon"]' # iOS
'meta[content][name="msapplication-TileImage"]' # IE11
'meta[content][name^="msapplication-square"]' # IE11
A s...