View

Testing shared traits or modules without repeating yourself

When two classes implement the same behavior (methods, callbacks, etc.), you should extract that behavior into a trait or module. This card describes how to test that extracted behavior without repeating yourself.

Note that the examples below use Modularity traits to extract shared behavior. This is simply because we like to do it that way at makandra. The same techniques apply for modules and overriding self.included.

Example —…

Render a view from a model in Rails

In Rails 5 you can say:

ApplicationController.render(
  :template => 'users/index',
  :layout => 'my_layout',
  :assigns => { users: @users }
)

If a Request Environment is needed you can set attributes default attributes or initialize a new renderer in an explicit way (e.g. if you want to use users_url in the template):

ApplicationController.renderer.defaults # =>
{
  http_host: 'example.org',
  https:      false,
  ...
}

``` …

External content

angular/angularjs-batarang

Extends the Chrome WebInspector so you can debug AngularJS applications and hunt down performance issues.

It's really, really good.

Rails: Disable options of a select field

Simply give the select helper an option :disabled, passing either a single value or an array. You need to specify the option's value, not its text.

= form.select :country, Address.countries_for_select, :include_blank => true, :disabled => ['disabled-value1', 'disabled-value-2']

Also see Cucumber: Check if a select field contains a disabled option on how to test this.

exception_notification 4.0.0+ makes it easier to ignore errors, crawlers

The new exception_notification has awesome options like :ignore_crawlers => true and :ignore_if => lambda { ... }. These options should be helpful in ensuring every notifications means something actionable (instead of a long log of failures that just scrolls by).

Note that you should not ignore crawlers by default. Ideally, cool URLs never change and always respond with a helpful redirect or similar.

Ignore Errors like this:

```
# config/initializers/exception_notification.rb

Ex…

External content

The Chokehold of Calendars

Most people don’t schedule their work. They schedule the interruptions that prevent their work from happening.

RSpec: Where to put custom matchers and other support code

Custom matchers are a useful RSpec feature which you can use to DRY up repetitive expectations in your specs. Unfortunately the default directory structure generated by rspec-rails has no obvious place to put custom matchers or other support code.

I recommend storing them like this:

spec/support/matchers/be_allowed_access.rb
spec/support/matchers/be_denied_access.rb
spec/support/blueprints.rb

To make this support code availabl…

External content

RubyLTS

RubyLTS is a long term supported fork of Ruby 1.8 that will continue to receive security updates for the forseeable future.

Many box shadows will make your app unusable on smartphones and tablets

Box shadows are awesome. Unfortunately they are also very costly to render. You will rarely notice the rendering time on a laptop or desktop PC, box shadows can make your app completely unusable on smartphones and tables. By "unusable" I mean "device freezes for 10 seconds after an user action".

But isn't it the future?

Not yet. Eventually mobile devices will become powerful enough to make this a no…

Cucumber: Check if a select field contains a disabled option

For Capybara, use this step:

Then /^"([^"]*)" should be a disabled option for "([^"]*)"(?: within "([^\"]*)")?$/ do |value, field, selector|
  with_scope(selector) do
    field_labeled(field).find(:xpath, ".//option[text() = '#{value}'][@disabled]").should be_present
  end
end

RubyMine detects syntax errors where there are none

To make this go away I had to wipe my RubyMine settings (~/.RubyMine) and configure everything again. Fun.

Consul 0.9 lets you optimize records checks

Consul 0.9 comes with many new features to optimize powers that only check access to a given record. e.g. Power.current.post?(Post.last). See below for details.

Powers that only check a given object

Sometimes it is not convenient to define powers as a collection. Sometimes you only want to store a method that
checks whether a given object is accessible.

To do so, simply define a power that ends in a question mark:

class Power
  ...

  power :upd...

RSpec: Where to put shared example groups

Shared example groups are a useful RSpec feature. Unfortunately the default directory structure generated by rspec-rails has no obvious place to put them.

I recommend storing them like this:

spec/models/shared_examples/foo.rb
spec/models/shared_examples/bar.rb
spec/models/shared_examples/baz.rb
spec/controllers/shared_examples/foo.rb
spec/controllers/shared_examples/bar.rb
spec/controllers/shared_examples/baz.rb

To ma…

External content

Remote Debugging on Android - Chrome DevTools

The Google Chrome DevTools allow you to inspect, debug, and analyze the on-device experience with the full suite of tools you're used to, meaning you can use the Chrome DevTools on your development desktop machine to debug a page on your mobile device.

Git: How to remove ignored files from your repository's directory

When you have files in your .gitignore they won't be considered for changes, but still you might want to get rid of them, e.g. because they clutter your file system.

While a regular git clean will ignore them as well, passing the -x switch changes that:

git clean -x

If you want to see what would happen first, make sure to pass the -n switch for a dry run:

git clean -xn

Clean even harder by passing the -f (force cleaning under certain circumstances; I think this is also required by default) or -d (removes director…

exception_notification 3.0.0+ lets you send errors as HTML e-mails

Exception notifications contain a lot of information: Backtraces, HTTP headers, etc. exception_notification tries its best to format this wall of information using ASCII art, but you can also make it send those notification as simple HTML e-mails that have some simple formatting for clarity, but no images etc. To do so, activate this option:

:email_format => :html

Those HTML notifications are still delivered with a text-only version, so if you are using a console cli…

External content

Webservice to mock HTTP responses

Sometimes in the course of development you may need to mock HTTP responses.

This is a simple service to return various HTTP responses.

External content

Maker's Schedule, Manager's Schedule

One reason programmers dislike meetings so much is that they're on a different type of schedule from other people. Meetings cost them more.

View
3474 cards