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"

Use Capybara commands inside an IFRAME

If you need to follow links, click buttons, etc. using Capybara inside an <iframe>, you can do it like this:

page.driver.within_frame('iframe-id') do
  fill_in 'E-mail', with: 'foo@bar.com'
  fill_in 'Password', with: 'secret'
  click_button 'Submit'
end

This is tested with the Selenium driver; I haven’t tested it with Chrome.

If you’re also using Cucumber you could make a meta-step like this:

```
When /^(.?) inside the (.?) frame$/ do |step_text, frame_id|
page.driver.within_frame(frame_id) do
step …

Repeats

Don't name columns like counter_cache columns

ActiveRecord has a feature called counter caching where the containing record in a has_many relationship caches the number of its children. E.g. when you have House has_many :rooms, Rails can cache the number of rooms in House#rooms_count.

Mind that when a model has a column that looks to Rails like a counter-cache column, Rails will apply counter-cache logic to your model, even if you’re not using counter caches.

E.g. you have a house with 12 rooms, but `house.r…

External content

Copy & Paste & The Web | CSS-Tricks

Insanely detailled guide about controlling copy & paste behavior using web technology in 2015.

Fix: Capybara is very slow when filling out fields in large forms

In large forms (30+ controls) new Capybara version become [extremely slow] when filling out fields. It takes several seconds per input. The reason for this is that Capybara generates a huge slow XPath expression to find the field.

The attached code patches fill_in with a much faster implementation. It’s a dirty fix and probably does a lot less than Capybara’s own fill_in so don’t use it unless you are having problems with test suites that are unusable because of this problem.

To use the patch in Cucumber, co…

input: A DOM event that is fired whenever a text field changes

If you’re supporting IE9+, you can listen to input to see if a text field changes.

Other than change, it fires while the user is typing and doesn’t wait until the user blurs the field.

Older workarounds included polling the field every X ms to see if its value changed.

Repeats

Know the side effects of using hashes for conditions

Find conditions for scopes can be given either as an array (:conditions => ['state = ?', 'draft']) or a hash (:conditions => { 'state' => 'draft' }). Each of these options has considerable side effects you should know about. This applies to both Rails 2 and old patch levels of Rails 3. The issue is fixed in Rails 3.0.5, has been re-introduced in Rails 3.2 and has been fixed again in Rails 4. If you’re using our Rails 3.x forks you are not affected. If you are using Rails 2.3.x you are always affected, even if you are using our forks …

External contentRepeats

Github Cheat Sheet

All the hidden and not hidden features of Git and GitHub.

Faster debugging with RubyMine macros

In my RubyMine I have recorded two macros for debugging and linked them to some keyboard shortcuts. Since I believe everyone could benefit from having those I wanted to share this.

The first one simply inserts

binding.pry

and the second one

.tap { |object| binding.pry }

for when you do not have a reference to the object you want to inspect.

In order to record a macro you simply follow the path Edit > Macros > Start Macro Recording.

Then you simply type binding.pry or whatever you want to record and stop recor…

External contentRepeats

image-to-DataURI converter: Duri.me

Small web application where you can upload an image (PNG, JPEG, GIF) and generate a base64-encoded version of it.

You can copy the result as
- HTML <img> tag with data URI,
- CSS rule with background-image and data URI,
- plain Base64-encoded data URI string.

Repeats

Cancelling event propagation with jQuery

Within an event handler, there are no less than 4 methods to cancel event propagation, each with different semantics.

  • event.preventDefault()

    Only prevents the default browser behavior for the click, i.e. going to a different url or submitting a form.

  • event.stopPropagation()

    Only prevents the event from bubbling up the DOM. Note this effectively also cancels any event handlers attached through jQuery’s live method, since those depend on bubbling.

  • event.stopImmediatePropagation()

    Prevents the event from bubbling up t…

Repeats

Casting ActiveRecord scopes or instances to ActiveType extended model classes

When working with ActiveType you will often find it useful to cast an ActiveRecord instance to its extended ActiveType::Record variant.

Starting with active_type 0.4.0 you can use ActiveType.cast for this:

class User < ActiveRecord::Base
  ...
end

class SignUp < ActiveType::Record[User]
  ...
end

user = User.find(1)
sign_up = ActiveType.cast(user, SignUp)
sign_up.is_a?(SignUp) # => true

This is basically like [ActiveRecord#becomes](http://apidock.com/rails/v4.2.1/ActiveRecord/Pers…

Rails 3 ActiveRecord::Persistence#becomes does not copy changed attributes

Note: ActiveRecord::Base#becomes has a lot of quirks and inconsistent behavior. You probably want to use ActiveType.cast instead.


This issue will be encountered when relying on attribute_was methods of ActiveModel::Dirty after casting a model which has defaults to a form model, for example.

In my case a record with an assignable_values legacy value beca…

External contentRepeats

Ruby: What extend and include do

All Rubyists should be familiar with the common definitions for include and extend. You include a module to add instance methods to a class and extend to add class methods. Unfortunately, this common definition isn’t entirely accurate. It fails to explain why we use instance.extend(Module) to add methods to an instance. Shouldn’t it be instance.include(Module)? To figure this out we’re going to start by discussing where methods are stored.

Further reading

After you understood include and extend you might want to look …

ZenTest "invalid gemspec" / "Illformed requirement"

Today I ran into this:

Invalid gemspec in [/usr/local/rvm/gems/ruby-1.9.3-p194/specifications/ZenTest-4.9.3.gemspec]: Illformed requirement ["< 2.1, >= 1.8"].

You need a newer Rubygems version. Try this: gem update --system 1.8.29

Repeats

Beware of "nil" values in params

Recent rails security updates have shown that people make incorrect assumptions about the possible contents of the params hash.

Just don’t make any! Treat it as what it is: potentially unsafe user input. For example:

/pages/edit?foo    --> params == {:foo => nil}
/pages/edit?foo[]  --> params == {:foo => [nil]} # at least in older rails 3 and in rails 2.x

Be especially wary about stuff like

User.find_by_password_reset_token(params[:password_reset_token])

If params[:password_reset_token] is nil, you’ll retriev…

Repeats

A non-weird replacement for grouped_collection_select

Rails comes with grouped_collection_select that appears to be useful, but isn’t.

Use flat_grouped_collection_select instead:

```
def flat_grouped_collection_select(field, collection, group_label_method, value_method, label_method, options = {}, html_options = {})
hash = collection.group_by(&group_label_method).collect_hash do |group_label, group_entries|
list_of_pairs = group_entries.collect { |entry|
[entry.send(label_method),…

External content

When should you use DateTime and when should you use Time?

The differences are subtle. You probably want to use Time, except when you want to use DateTime. See the attached article for details.

Repeats

Why has_many :through associations can return the same record multiple times

An association defined with has_many :through will return the same record multiple times if multiple join models for the same record exist. If that isn’t what you want, you need to add the :uniq => true option.

Example

Say you have an Invoice with multiple Items. Each Item has a Product:

class Invoice < ActiveRecord::Base
  has_many :items
  has_many :products, :through => :items
end

class Item < ActiveRecord::Base
  belongs_to :invoice
  belongs_to :product
end

class Product < Act...
2822 cards