Using Typeahead.js without Bloodhound

A great library for adding typeahead support to your site is Twitter’s Typeahead.js. Even better is the excellent Bloodhound suggestion engine which comes with it. Sometimes though if you’re dealing with a remote suggestion engine like Elasticsearch’s completion suggester you don’t need to run remote results once again through another suggestion engine. Bypassing Bloodhound is as simple as hooking your own source function into your Typeahead definition.

 

Naming service classes in Rails

One of the core ideas of Rails is convention over configurations. Models go in app/models, controllers go in app/controllers and views go in app/views. The danger is that we stick to those conventions no matter what and we end up either with fat controllers, fat models or even worse a mixture of both.

Many times we don’t take enough advantage of Ruby’s object oriented nature and the ability to extract functionality out into separate classes. Doing so can make an entire application easier to extend, understand and test. I have tried approaching this from different angles in different projects and I’ve found that the two main hurdles to getting this right are naming classes and putting them in the right place.

I have experimented with naming such as UserAuthenticator and UserAuthenticationService, and always end up feeling uncomfortable I constantly wonder if the other name is better or if there is a better way entirely. Using agent nouns in class names is considered a code smell, but the more that I think about it the important part is picking a choice and sticking to it. I was looking at the GitLab repository and noticed that they’ve done exactly that, everything is named in a consistent manner. I think many may dislike that but it makes things extremely clear and easier for anyone contributing as to what they should name their classes and where they should put them.

Will VC eat itself

There was a post on TechCrunch a few days ago about the big lie in startups and how you can’t run a startup without lying about a million things. When I really think about all these funded startups it upsets me, mainly because I’ve tried to bootstrap all my endeavours. That does make me sound bitter, and I am if I’m honest about it. If you start a company, get funding and fail you’re still often seen as a winner in people’s eyes where as those that bootstrap a company and fail are just seeing as losers. When did we reach a point where getting VC was the main priority after starting a business? When did we lose sight of actually trying to make money first?

Nginx proxy_pass a folder to WordPress

After a rash of security updates for WordPress, I finally buckled down to move our install of WordPress off to it’s own server. This would be easy if we were running the blog on it’s own domain, but it was actually living under /blog on our main domain for SEO reasons. Running nginx as a reverse proxy to WordPress on a different machine was not an easy task. A lot of guides did not seem to have the information I needed so here are the steps I took to get it working.

Add the proxy_pass definition to your nginx config:

Update your WordPress settings. This is the key to getting your site to work correctly. Leave the WordPress Address as where the site is hosted, but change the Site Address to be where URLs are to be written as linking to

Screenshot 2015-07-27 21.43.38

I also had to add a rewrite to my .htaccess file to remove trailing slashes:


Be warned that the permalinks section will suggest a .htaccess file which has /blog/ as the RewriteBase, but this just sends things into a redirect loop. Leave it as the standard root setting.

Make sure you also check that the canonical tags generated by your theme are correct. You want to be sure that any references on the WordPress Address URL canonicalise to Site Address URLs.

Business Design Principles

Facebook’s Four Business Design Principles is an excellent reference that should be at the core of every business tool.

Help people learn and grow

Balance efficiency and effectiveness

Bring clarity to complexity

Be accurate and predictable

The majority of my time these days is spent writing tools for business as opposed to consumer facing sites and trying to find the balance between ease of use and providing all the elements necessary to complete a task can be very difficult at times. I had not heard of the Goldilocks Principal before but it perfectly summarises this fine balancing act.

With a complex piece of UI, if you don’t simplify it enough, people can’t figure out how to use it. But if you swing too far in the other direction and over-simplify it, you risk dismantling the very value that people are looking to access through the tool.

When immersed in a tool it’s easy to become blind to the experience of actually using it. A good example are the drop down menus on a site I’ve been working on these past few years. Even though we would all use them day to day they were never really given a second thought. It was only after looking into how they were actually performing that it was apparent that they were actually really awful to use. Our own familiarity had caused a massive disconnect between how effective they were to use.

BEM CSS

All too often I find that CSS quickly becomes a real tangle of clashing styles and names. No matter how hard I try something always ends up breaking the styles of something else. Moving to SASS helped a lot but I find on the flip side I end up with styling that’s overly nested.

I’ve recently come across BEM which is CSS methodology for naming classes. BEM stands for Blocks, Elements, Modifiers. I’ve previously looked into SMACSS but still found things fell apart (probably because I’m doing it wrong). BEM looks a lot simpler and easier to maintain. A simple example would be the following styling for an article.

I’m not going to go into much detail as there are already some great posts out there on the subject. I still feel like CSS itself is a bit of a broken solution, although I can’t articulate why or what would be a better option.

Who designs car parks?

Riddled with tight gaps and high kerbs they are the often the most anti-car friendly places on earth. I am convinced that the people who design car parks must be sadists.

I have yet to see a car park wall that isn’t covered in different colour streaks from all the mis-judged turns. Why not have sloped kerbs so that you don’t scuff your wheels or bodywork if you misjudge the spacing? Why not give a little bit more space instead of tight turns at every opportunity? You wouldn’t pay to stay at a hotel where they punch and kick you at every chance they get before you reach your room. Why is it acceptable for our cars?

I’m a pirate but I’m open to options

Pirating TV shows, music, games and applications is just so ubiquitous these days. Where as once it took some degree of knowledge, now everyone seems to do it. I don’t believe people pirate things because they want to steal but do because there are no reasonable alternatives.

For instance, since getting a Spotify Premium account, I haven’t downloaded any music. I dislike pirating shows as I feel that by doing so I’m not really showing any support for the ones I truly love. I could wait months (or even over a year in some cases) to catch it on Netflix or Amazon Prime but then I miss any ability to join in with the conversations with those that have watched it. I would happily pay for the ability to watch show as soon as they are broadcast, or even a day or two later, seeing as I mostly watch US shows. This is of course a very simplistic view and the politics and logistics around doing something like that are extremely complicated, but I can still dream. At the same time, just shut up and take my money!

Better sort by rating with Spree and spree_reviews

The blog post How Not To Sort By Average Rating continually pops up and got me thinking about how we currently implement sort by rating. We currently use spree_reviews for capturing ratings and it takes a very simplistic approach to storing the average rating for a product:

This exact scenario is mentioned in the blog post above:

Why it is wrong: Average rating works fine if you always have a ton of ratings, but suppose item 1 has 2 positive ratings and 0 negative ratings. Suppose item 2 has 100 positive ratings and 1 negative rating. This algorithm puts item two (tons of positive ratings) below item one (very few positive ratings). WRONG.

A better solution is using a Bayesian estimate which actually takes the number of reviews into consideration. This is how IMDB currently create their top 250 movie list:

weighted rating (WR) = (v ÷ (v+m)) × R + (m ÷ (v+m)) × C

where:

* R = average for the movie (mean) = (Rating)
* v = number of votes for the movie = (votes)
* m = minimum votes required to be listed in the Top 250 (currently 1300)
* C = the mean vote across the whole report (currently 6.8) for the Top 250, only votes from regular voters are considered.

With that, it’s fairly simple to approximate with spree_reviews. Just be sure to recalculate all your product ratings.

Into the abyss

Beware that, when fighting monsters, you yourself do not become a monster… for when you gaze long into the abyss. The abyss gazes also into you. Friedrich Nietzsche