Electron: How to build, package and sign 3rd-party binaries with Electron-Builder

Working on Blazing Bookkeeper, an ORC based document scanner, I had to build and package various libraries and binaries through electron-builder. I couldn’t find any guides detailing this, so I’ll outline the process we took to make it work.

Rails 4: Add data-error attribute to form field instead of using wrapper

I wanted to have data-error on the form field instead of the standard rails error wrapper <div class="field_with_errors"></div>. With a data attribute we can do some much nicer handling of the form field.

Non-obtrusive multi-language redirection (with RoR example)

At Spaces we’ve implemented a non-obtrusive redirection for our multi-language, multi-domain websites. Too many websites have the awful habit of forcing content to visitors that the visitor didn’t request in the first place.

How and why we implement Intercom to only load when required (maintaining the privacy of our users)

At Spaces we’ve been looking into various way of offering support. Intercom seemed to be a great solution, but we were surprised to see the lack of privacy, in that they record personal information for anyone who simply sign in by default.

The Intercom rails gem collected user id, email, name, and sign up date by default. We contacted them about how we could increase the privacy, and the response was in short that we had to use their API with a far worse UX, if we even could get that to work in the first place.

How to retry SSH, API, and external connections in Rails 4

This is pretty much a no-brainer, but in case you’re in doubt, this is an easy way to retry external connections. This way you don’t automatically halt the application at the slightest network issues.

I’m using Peakium as an example of API connection, but you can do this with pretty much anything as long as you rescue from the right exception type (should only be in case there is connectivity issue).

# models/concerns/peakium_retry.rb
module PeakiumRetry
  def peakium_retry(&block)
    retries = 1
    begin
      block.call
    rescue Peakium::APIConnectionError => e
      raise e if retries == 0
      retries -= 1
      sleep 0.5
      retry
    end
  end
end

This will retry the connection one time if failing (waiting half a second between tries), and throw the exception if it fails again.

You will just do the call like this:

events = peakium_retry do
  Peakium::Event.all(params, peakium_access_token).data
end

Straight forward, and easy.

Rails: How to fix issue with unique number under scope that doesn’t count correctly with find_or_create_by

The bug fixing was very frustrating. Take a look at the following example:

class Model < ActiveRecord::Base
...
  # Set a unique scoped number
  validate do
    errors.add(:scoped_unique, "is already taken") if parent_parent_model.parent_model.models(true).where(scoped_unique: scoped_unique).where.not(id: id).present?
  end
  before_validation on: :create do
    self.scoped_unique = (parent_parent_model.parent_model.models(true).collect { | model | model.scoped_unique }.max.to_i || 0) + 1 if self.scoped_unique.nil?
  end
...
end

This works just fine when you are creating a new model like this:

parent_model.models.create(...)

However using find_or_create_by it adds unexpectedly additional parameters to the scope the model itself with the parent_model (thus not restricting itself to parent_parent_model only)!

parent_model.models.create_with(...).find_or_create_by(...)

Turns out you have to unscope the whole query:

class Model < ActiveRecord::Base
...
  # Set a unique scoped number
  validate do
    Model.unscoped do
      errors.add(:scoped_unique, "is already taken") if parent_parent_model.parent_model.models(true).where(scoped_unique: scoped_unique).where.not(id: id).present?
    end
  end
  before_validation on: :create do
    Model.unscoped do
      self.scoped_unique = (parent_parent_model.parent_model.models(true).collect { | model | model.scoped_unique }.max.to_i || 0) + 1 if self.scoped_unique.nil?
    end
  end
...
end

Rails: button_to with [data-confirm] tries to confirm tries and nothing happens

If you are using jquery-ujs and happens to get two confirm dialogs when using [data-confirm] with a button, all you have to do is put the data confirm attribute in a form attribute:

Before:

<%= button_to registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete do %>
    Cancel my account
<% end %>

After

<%= button_to registration_path(resource_name), form: { data: { confirm: "Are you sure?" } }, method: :delete do %>
    Cancel my account
<% end %>

Rails: Unique constraint and acts_as_list

Here’s a quick tip to fix issues with unique constraints and acts_as_list gem. The issue stems from the moving model’s position not being set to NULL during the move.

Rails 4: How to set parent model on both left and right side in a has_many :trough relationship, models being created with nested attributes

Let’s say you have a many-to-many setup, with same parent for both left and right side. You might want to create an account, with a user and a project in one go.

Fix nokogiri install issue on Mac OS X 10.9

When installing nokogiri 1.6.1 on my RubyStack installation with Mac OS X Mavericks I got the following error.

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

Running ‘configure’ for libxml2 2.8.0… ERROR, review ‘tmp//ports/libxml2/2.8.0/configure.log’ to see what happened.

The configure.log said that the gcc/make could not be found. While figuring it out I found a lot of issues, and the information for fixing them was all deprecated.

The following steps solved my issues completely:

# sudo xcode-select -switch /Library/Developer/CommandLineTools
# gem uninstall nokogiri libxml-ruby
# gem update --system
# gem install nokogiri

Gem update is unnecessary, but good to have just in case. Hope this helps.