Search_spinner Cross_grey Magnifier
help

Search syntax

  • Search for repeating cards with repeats:yes
  • Search for unfinished cards with unfinished:yes
  • Search for cards in a certain topic with topic:"name"
  • Search for phrases with "a phrase"
  • Exclude a word from search: -word
  • Exclude a phrase from search: -"a phrase"
  • Exclude a topic from search: -topic:"name"

ActiveRecord: Automatically remove join records on has_many :through associations

# Given the following models

class Image < ActiveRecord::Base
  has_many :album_image
  has_many :albums, through: :album_images
end

class Album < ActiveRecord::Base
  has_many :album_image
  has_many :images, through: :album_images
end

class AlbumImage < ActiveRecord::Base
  # Join model
  belongs_to :album
  belongs_to :image
end

Destroying a record in this setup will only remove the record itself, and leave orphaned join records behind.

```
image = Image.last
image.destroy # removes only the image record,
# but…

External content

free-for-dev

List of cloud services and SaaS with a free tier for developers or that are generally free for open source software.

Rails 4 introduced collection_check_boxes

Starting from Rails 4.0, you can use a special form options helper called #collection_check_boxes. It behaves similar to #collection_select, but instead of a single select field it renders a checkbox and a label for each item in the collection.

form.collection_check_boxes :author_ids, Author.all, :id, :name_with_initial

You can customize the output by passing a block. The block will be called with a special builder ob…

Repeats

How to define constants with traits

This is pretty much like defining subclasses through traits with the Modularity gem.

module MyConstantTrait
  as_trait do
    
    self::MY_CONSTANT = 'hello universe'
    
  end
end

This would set MY_CONSTANT for any class using the trait.

Of course you can also use parameters (for logic or for setting values, like here):

module ValuesTrait
  as_trait do |*args|
    
    self::VALUES = args
    
  end
end

class Foo
  does 'values', :f...
External content

AngularJS Cheat Sheet (PDF)

This cheat sheet … aims at providing a quick reference to
the most commonly used features in AngularJS.

Auto-destruct in 50 days

Updated: Do not pass an empty array to ActiveRecord.where when using NOT IN

This is still broken in rails 4, also when using postgres!

Unfinished

A step-overriding convention for Cucumber

Cucumber steps cannot be overwritten or inherited because of their pattern-matching approach.

Sometimes you need to adapt a step for a certain use case. To make it clear that this is a variation of a step, I suggest appending | (<tags>) to the step definition. This prevents ambiguous step definitions and makes it clear this is an altered step, immediately telling what it is about.

Example

Spreewald: Then the "Terms of Service" checkbox should be checked
Custom step that will work with Selenium even if the checkbox input field is hid…

Fontawesome 4 helper classes

Fontawesome 4 ships with many useful CSS helper classes.

Enlarge Icon
Add fa-lg (133%), fa-2x, fa-3x, fa-4x or fa-5x.
Fixed-width Icon
Add fa-fw. Will give all icons the same width.
Styling Lists
Add fa-ul to a <UL> and fa fa-<icon name> fa-li to a <LI> to give the list items custom “bullets”.
Bordered Icon
Add fa-border to get a border around the icon.
Spinning Icon
Add fa-spin to make any icon rotate. Suggested icons: fa-spinner, fa-refresh, fa-cog. Doesn’t work in IE <10.
Rotated Icon
Add `f…
External content

Thread-safe collections in Ruby

There’s several gems providing thread-safe collection classes in Ruby.

thread_safe

The thread_safe gem provides thread-safe versions of Array and Hash:

sa = ThreadSafe::Array.new # supports standard Array.new forms
sh = ThreadSafe::Hash.new # supports standard Hash.new forms

There’s also a ThreadSafe::Cache class that sort of behaves like an unordered Hash, but is much faster.

hamster

hamster provides several collec…

External contentRepeats

InfoQ: How to Design a Good API & Why it Matters

A well-written API can be a great asset to the organization that wrote it and to all that use it. Given the importance of good API design, surprisingly little has been written on the subject. In this talk (recorded at Javapolis), Java library designer Joshua Bloch teaches how to design good APIs, with many examples of what good and bad APIs look like.

Repeats

Cucumber: Prevent outstanding AJAX requests to bleed into other scenarios

When using Cucumber with Selenium or some other Javascript-enabled browser, AJAX request can sometimes “bleed” into the next scenario. This happens when a scenario triggers an AJAX request near the end, but does not wait for it to complete.

To avoid this, add the attached code to your cucumber’s /features/support directory. It will disable AJAX requests at the end of the scenario and wait for outstanding requests to complete.

Requires spreewald and only works for requests triggered by jQuery.

Repeats

How to discard ActiveRecord's association cache

You know that ActiveRecord caches associations so they are not loaded twice for the same object. You also know that you can reload an association to make Rails load its data from the database again.

user.posts.reload
# discards cache and reloads and returns user.posts right away
# => [...]

If you want to discard the cache but not query the database (only the next time the association is accessed), you can use reset:

user.posts.reset
# discards cache, but does not load anything yet
user.posts
# SQL query hap...

Using form models (aka decorators) with Devise

To use a form model with devise, you can simply override #resource_class in a controller. A typical use case would be the registrations controller, as users will need some fields only on sign-up. Example:

class Frontend::Authentication::RegistrationsController < Devise::RegistrationsController

  private

  def resource_class
    FrontendUser::AsSignUp # my decorator class, extending from FrontendUser
  end

end

Fontawesome 4+ modifiers

Fontawesome 4 has introduced new naming conventions that make it easy to retrieve variants of a given icon.

The format is:

fa-[name]-[alt]-[shape]-[o]-[direction]

Note that this is a naming convention which doesn’t imply there’s an icon for any combination of tags.

name
The name of the icon, e.g. comment, print, bookmark etc. See the full list.
alt
An alternative icon.
shape
The icon inside a circle or square.
o
An outlined version of t…
Repeats

Your Rails sandbox console

Just found out about a great feature in Rails that seems to be around since Rails 2. Start a console with the --sandbox (or -s) parameter:

rails console --sandbox

All changes you make to the database will be rolled back on exit. Very safety, much great, so secure. Wow!

Auto-destruct in 55 days

Updated: How to coordinate distributed work with MySQL's GET_LOCK · Xaprb

I took a closer look at the docs for the table-less with_advisory_lock.

Unfortunately, it has an annoying caveat in MySQL:

With MySQL (at least <= v5.5), if you ask for a different advisory lock within a with_advisory_lock block, you will be releasing the parent lock (!!!). A NestedAdvisoryLockError will be raised in this case. If you ask for the same lock name, with_advisory_lock won’t ask for the lock again…

Browse Amazon S3 buckets with Ubuntu Linux

There are some frontends available, but they all suck, are no longer maintained or are hard to install.

As a surprisingly comfortable alternative I have found a command line tool s3cmd:

sudo apt-get install s3cmd

When you run s3cmd the first time it will ask you for your access key ID and secret access key. This information is cached somewhere so you only need to write them once.

Once you’re done setting up, s3cmd gives you shell-like commands like s3cmd ls or s3cmd del somefile.png.

Here is a full list of commands it …

rspec_candy 0.4.0 released

  • Now supports RSpec 3 and Rails 4
  • Drops support for state_machine, which has some issues with Rails 4 and isn’t actively maintained
2772 cards