In the following example the method update_offices_people_count won't be called when office_id changes, because it gets overwritten by the second line: after_save :update_offices...

office_id_changed? || trashed_changed? end Or, move the conditions into the callback. This also allows you test the conditions more easily using a unit test:

...DEBUG or MiniMagick.debug = true (deprecated) before the processing happens. This will print all ImageMagick calls to stdout and you can get an idea where the problem is coming from.

RSpec lets you define the arguments with which you expect a method to be invoked: subject.should_receive(:say).with('hello...

Sometimes you need complex expectations on method arguments like this SomeApi.should_receive(:find).with(:query => '*foo*', :sort => 'timestamp ASC', :limit...

...change the behavior of a library if there's no better way. We can call the method we're overriding inside our monkey patch: class Foo def bar(argument)

...also yield blocks. This is handy if the returning object is receiving a block call. Consider this, where you cannot say and_return [] because of the block: def crawl_messages...

...your initializers/exception_notification.rb file: require 'resolv-replace' require 'exception_notification/rails' ExceptionNotification.configure do |config| ... end This calls the Ruby DNS resolver before configuring ExceptionNotifier so the DNS lookup always works as expected...

.../users and we want to prevent network interaction in our unit test. The AJAX call is asynchronously and we don't have control over when it returns (and hence when...

...it calls the callback function). Here is a test that does all that: describe 'reloadUsers', -> it 'replaces the user list with a fresh copy from /users', (done) -> # Mock network connections...

...Backbone models and inheritance, at some point you want to overwrite inherited methods but call the parent's implementation, too. In JavaScript, there is no simple "super" method like in...

...we want initialize of BaseClass to run as well -- and if you want to call a method in JavaScript, you need to do exactly that. Backbone offers a shortcut that...

github.com

Stackprof is a sampling call-stack profile for Ruby 2.1+. Instead of tracking all method calls, it will simply collect the current stack trace of a running program at fixed...

...cast_from_database (activerecord-4.2.5.2/lib/active_record/type/integer.rb:15) samples: 7472 self (6.9%) / 7472 total (6.9%) callers: 7253 ( 97.1%) block (2 levels) in ActiveRecord::Result#cast_values 218 ( 2.9%) ActiveRecord::Attribute::FromDatabase...

...In the example above, role_select is now a Capybara::Driver::Node. You can call a number of methods on such a node, e. g. in order to click it...

...are e.g. the headless Rack::Test or Selenium). It is a stupid idea to call #node on such a Capybara node, as you see here: selection = role_select.node.search(".//option[@selected = 'selected...

...and most likely RSpec 2 and later versions. When you have a helper that calls content_for and want to check its behavior you should probably write a feature instead...

...script that takes an arbitrary amount of arguments and passes them on to another call), you may not want to do a “$1 $2 $3 $4 ...”. Use $@ instead, like in...

$ cat welcome #!/bin/bash echo Hello to $@ When called, the above would produce this: $ ./welcome the world and universe Hello to the world and universe Note that $@ works for...

If you use awesome and make a Slack call, you'll constantly have an overlay window pop up taking over half of your screen. To fix this, add the following...

...rule to your awful.rules.rules section in your rc.lua: awful.rules.rules = { -- ... { rule = { name = "Slack Call Minipanel" }, properties = { floating = true, ontop = true...

Then go to Edit -> User Profile -> Scripts and add the script for "call incoming". Text notification You can additionally get a notification bubble in your window manager, by...

Calling #gsub on a string that was previously marked as #html_safe will lead to unexpected behavior. E. g. backreferences to captured groups ($1, $2) will be nil even if...

If the argument list is the same every time: expect(object).to receive(:foo).with('argument').and_return('response 1...

makandra dev

...have a second module 'client' that exports a function helloTwice(). All this does is call hello() two times: // client.js import { hello } from 'lib' function helloTwice() { hello() hello() } export helloTwice

...our test we would like to show that helloTwice() calls hello() twice. We find out that it's really hard to spy on the imported hello() function: // test.js import { hello...

...error.level === "WARNING") Errors containing the phrase Failed to load resource To delete these defaults, call BrowseConsole.ignore_nothing before your own ignore rules: # Remove default rules BrowserConsole.ignore_nothing # Add your own...

...delegation in Rails can help you to keep your code organized and avoid deep call chains (law of demeter) by forwarding calls from one object to another. Rails provides several...

...for forwarding a small number of methods without cluttering your model Delegating all method calls via method_missing and respond_to_missing? But what do we do if we want...

...all ActiveRecord classes or record instances. It doesn't matter on which object you call it, since they all delegate to the current thread's database connection. If you don...

...in Rails, ActiveRecord::Base#save automatically opens a transaction. Hence changes you make in callbacks, nested attributes processing, etc. will automatically run inside a transaction. What happens if two transactions...

...JOIN refer to this card 2. Embed models into your migrations You can never call models in a migration because your models will eventually evolve away from your migrations. This...

Also note the use of reset_column_information. As a rule of thumb, call it once for every migration model, as Rails might have cached an old schema. In...

...any strings you insert. If you want to insert HTML verbatim, you need to call #html_safe. However, #html_safe does not "unescape" a string. It merely marks a string...

...as safe for unescaped insertion. How html_safe works Calling html_safe on a String returns a new object that looks and acts like a String, but actually is a...

...I am [object Window]" When we assign that same function to an object and call it as a method, that object becomes the this value: let object = new String("Alice...

...side of the dot"). When we pluck the function property from the object and call it without a receiver, we see that this has reverted to its default value: