Not In queries in Rails with MongoDB and Mongoid

Sometimes you want a quick "not in" or "not equal" query in Rails, but you're running Mongo. ActiveRecord allows us to write where.not against relational databases, but this doesn't work when using Mongoid. Instead, you can use nin or it's alias not_in

Book.not_in(title: "Of mice and men")

The method is also chainable, so this is valid:

Book.where(author: "Steinbeck").not_in(title: "Of mice and men")

If you prefer not to chain your methods, you can also write the query by sending nin to a symbolised version of your model's property and then using the old hashrocket syntax to specify the value:

Book.where(author: "Steinbeck", :title.nin => ["Of mice and men"])

Module not found: Error: Can't resolve 'fs' using webpack and Electron

Module not found: Error: Can't resolve 'fs'

If you've set up webpack with an Electron application and you hit this strange error when trying to bundle your project, you've probably forgotten to include the target in your webpack config. This is easily fixed by editing webpack.config.js to include the target, something like this:

const path = require('path');

module.exports = {
entry: './app.js',
output: {
filename: 'bundle.js'
},
target: 'electron'
.
.
.
}

Fixing a broken npm tha errors with "cannot find module 'process-nextick-args’"

Following along with Free Code Camp's Node course, I was playing with how-to-npm from Node School, I followed the first exercise and ran how-no-npm verify. My npm version was out of date and following the prompt, I ran npm install npm -g. That seemed to complet successfully, however, running how-to-npm verify subsequently gave me a lovely string of errors, the top one being

Error: Cannot find module 'process-nextick-args’

Running any npm command seemed to result in something similar.

To fix this, I did the following - first upgrade Node itself with nvm:

Step 1 - upgrade node itself with nvm:

nvm install v8.9.3

Step 2 - install how-to-npm again:

npm install how-to-npm -g`

Step 3 - upgrade npm

npm i -g npm

Now I can run how-to-npm verify again and everything works

Add non blog pages to a middleman blog

Having set middleman up to be a static blog, you might find yourself wanting to add some static pages outside of the blog structure. For example, if you install any analytics on your site, you often need to include a static file with an authentication code to your analytics provider. These pages are often barebones, with no common header or footer and no styling and you almost certainly do not need them to be processed from markdown. In other words, you want to create a page that does not use the layouts that the rest of your site uses.

To set these up, create the html page and then edit your config.rb to include something like the following:

page "my_barebones_page.html", directory_index: false, layout:false

Using layout: false is reasonably self-explanatory; you are instructing middleman to forego any layout for this page. The inclusion of directory_index: false makes sure that the page is accessible with the .html suffix. This is especially useful if you are using pretty urls for your blog.

How to show hidden rake tasks

Rake tasks are easily listed from the command line with rake -T. However if you've written a rake task and neglected to provide a description for it with the desc as the first line, the task will not show up with the usual rake -T. These are essentially hidden rake tasks!

To display them, append -A to your normal rake command; so:

rake -T -A

and hey presto, your hidden tasks are visible.

Renaming a local git branch

The simplest way to rename a branch in git is first to checkout that branch and then do something like this:

git branch -m new-branch-name

How to configure Middleman to use backticks for fenced code blocks

Technical blogs are usually going to have code examples and if you're a Jekyll refugee exploring Middleman, you'll be accustomed to creating fenced code blocks surrounded by backticks. Something like this:

```
code_block = Codeblock.new(:with_backticks)
```

Middleman uses kramdown to parse and convert Markdown into Html and kramdown by default expects tildes for fenced code blocks:

~~~
code_block = Codeblock.new(:with_tildes)
~~~

To get a backtick flavoured code block, you need to specify a Github Flavoured Markdown (GFM) parser in your config.rb.

To set up your Middleman powered blog so that kramdown accepts backtick fenced code blocks, follow the usual setup guides and then, in config.rb add the following:

set :markdown_engine, :kramdown
set :markdown, fenced_code_blocks: true, input: "GFM"

How to add a meta tag to your shopify pages

What are meta tags

Meta tags are, as the name suggests, pieces of html that you add to your web pages to provide meta data. In other words, meta tags describe your web page and can include things like who authored the page, the keywords to associate with the page, a general description of the page and various other pieces of data that apply to your web page but aren't necesary for the end user to see

Why you might want to add meta tags to your shopify page

Meta tags are sometimes used to verify the ownership of a site. For example, if you want to hook up a Pinterest business account to your Shopify shop, Pinterest will ask you to add a meta tag to your site; the assumption being that only the owner of the site will have access to the underlying html and if you manage to shoe-horn your meta tag in, you've basically proven that you own the site.

How to add a meta tag to your Shopify page

The process of adding a meta tag to your Shopify site is pretty simple. It does, however, require you edit the template. Be warned, you'll be diving into the actual code of your chosen theme and modifying that. This can seem scary, but it's pretty simple in reality, and all you are doing is editing text.

Navigate to the store's theme

Log in to Shopify, click on the Online Store under Sales Chanel in the left hand menu.

Navigating to the theme

A secondary menu should slide in and Themes should already be selected for you. If not, click on Themes.

Secondary menu

Take a backup

It's always a good idea to backup your theme in case you break something so click on the elipses(three dots) towards the right hand side of the page near the Customize theme button and select Download theme to get a backup emailed to you.

Edit the html/css

Now you have your backup, click on the elipses again and this time select Edit HTML/CSS.

Edit html/css

You'll see a file listing in and a helpful message saying 'Pick a file from the left sidebar to start editing'.

In general, you'll need to edit a file called theme.liquid. Shopify uses a special type of markup or templating language which gets converted into HTML, which is why the file has a .liquid extension. These template files are a combination of HTML and essentially ruby code which allows for things like loops and other coding constructs not normally available in plain HTML.

HTML pages (and by extension, liquid templates) contain various sections which are contained within opening and closing tags. One of these sections is called the head. As it's name implies, the head section sits at the top of the page and it's here that meta tags are inserted. If you look at the template for your site, you'll probably already have a few meta tags. That's great.

To inlude your Pinterest meta tag, just copy the meta tag that Pinterest has supplied you and insert it somewhere between the <head> and </head> tags. If you have a few meta tags in your document, that's great, just paste your new one in below those and hit the Save button.

The Pinterest meta tag will probably look something like

<meta name="p:domain_verify" content="987e54321234a678909f7654321234"/>

Below you can see an example of the first few lines of a liquid template with a Pinterest meta tag included and highlighted in yellow.

Add meta tag to liquid template

Remember to save your work.

Fatal errors installing esqlite with mix

I'm playing with Phoenix and although postgres is supported by default, I wanted to tinker with n sqlite database for a project I'm working on.

It is simple enough to set up esqlite, however, when trying to run mix ecto.migrate, I bumped into the following error:

c_src/esqlite3_nif.c:21:21: fatal error: erl_nif.h: No such file or directory
compilation terminated

Mix was also good enough to inform me

** (Mix) Could not compile dependency esqlite, /home/b/.mix/rebar command failed. If you want to recompile this dependency, please run: mix deps.compile esqlite

To solve this, I had to install the erlang-dev package. I'm running Ubuntu so:

apt-get install erlang-dev

And all is right with the world again.

How to install inconsolata font on Ubuntu

This is a simple one-liner. Open a terminal and install the font as follows:

sudo apt-get install fonts-inconsolata

With the font installed, you can now set your terminal's font by clicking Edit->Profiles in the terminal menu and then selecting the profile from the windown that pops up and clicking the Edit button. Changing the terminal font should also change your vim font.

Previous Page | Next Page