The browser padlock is important. It is critical for e-commerce and other applications that depend on HTTPS for sending sensitive information securely. Unfortunately many sites, even major retailers have trouble keeping that browser padlock green.
On my teams I've seen it happen over and over. You release a web application that uses SSL. At launch time there is manual QA to ensure the browser padlock is green. All good. 6 months later, the padlock is broken becuase somebody misconfigured the webfonts, or the YouTubes, etc.
The problem here is that you have a manual QA step! Rack-Padlock is a tool I wrote to remove that manual step. Building this was soo awesome! I got to read the CSP specification. My rack-fu has gotten pretty good. And my final solution is crazy easy to use!
Check out the sample application, or just drop it into your own Rails app.
Saturday, February 9, 2013
Tuesday, January 22, 2013
Tips for Easy UTF-8 Ruby Adventuring
Getting that search box working in Esperanto? Cherokee? Pull on your wading boots because your walking into deep waters. I can't make you an expert in UTF-8 but I can recommend that you know the following stuff before you venture forth:
- Make sure your DB is configured to support UTF-8. Configuration is DB specific so please see documentation for your respective DB.
- Make sure your Ruby source code supports UTF-8. You might be surprised to find out that ruby 1.9 encodes your source code as US-ASCII by default. Take some time to learn about the magic encoding comment.
- Make sure your regexes support UTF-8. Use posix character properties instead of standard ASCII character classes like \w \s \d
- Upcase and downcase won't work for UTF-8 strings, but there is a gem for that! Checkout unicode_utils
- If you want to compare unicode strings in MySQL, have a look at collation in their documentation and know the difference between: utf8_general_ci and utf8_bin. You might be surprised how loose the default matching is.
Saturday, December 1, 2012
CarrierWave::InvalidParameter (invalid cache id)
I'm using carrierwave to handle uploads for a project, a glorified lolcat generator. In my carrierwave uploader I had a processor that was calling model.save. My specs were passing but the app was throwing CarrierWave::InvalidParameter (invalid cache id) whenever I called recreate_versions!. I eventually tracked the cause of this down to calling model.save. It turns out that call was unnecessary. Carrierwave must do that for you. My specs were passing because I had processing turned off. config.enable_processing = false. Nobody else on the internets seemed to be having this problem. Hope it helps somebody.
Monday, October 29, 2012
Add Refinery Blog to an Existing Rails App
This is pretty easy, but there is alot of cruddy old documentation out there to confuse you. Here are the steps I took to add a refinery blog to a simple Rails app.
And that is pretty much it. My blog shows up at "/cms/blog". My app is still at "/". And refinery's admin stuff is found at "/cms/refinery".
- Add the refinery blog dependencies to my Gemfile
- Tweak rails dependencies to mesh with refinery dependencies
- Generate refinery and refinery blog stuff
- Fix routes
- Fix layouts that refinery destroys
Here is a gemfile snippet that shows what I added to make step 1 and 2 happen. The only trouble I had was that refinery depends on an older version of jquery-rails than rails 3.2.8. Bundler kept installing an ancient version of refinery because i guess it's dependency rules were more liberal. Make sure you get a version of refinery that is greater than 2.
Once you have those dependencies you generate the refinery CMS.
rails generate refinery:cms --fresh-installationThat will create lots of stuff, migrate and seed your db, and it will also delete your application layout. Not a big deal if you are using git, we can put things right. Before we do that let's just generate the blog too.
rails generate refinery:blog rake db:migrate rake db:seedOk, more stuff generated, migrated and seeded. Refinery has installed itself in the root of our application's routes so let's put it in a subdirectory. Open up routes.rb and change the refinery mount to have a different 'at' location. Now notice that refinery has deleted your application layout. You probably want to have separate layouts for your existing application and the refinery application. What I did was to use git to bring back my old layout and then I renamed it to appname.html.erb Then inside application_controller.rb I dynamically set the layout as follows: This basically checks to see if the controller is inside the Refinery module and if it is it sets the appropriate layout. Refinery uses the default layout (which shouldn't exist in your project because it is set inside the refinery engine).
And that is pretty much it. My blog shows up at "/cms/blog". My app is still at "/". And refinery's admin stuff is found at "/cms/refinery".
Friday, September 7, 2012
The mochila pattern
A mochila (pronounced mo-chee-uh is Spanish for pouch) was used for carrying mail by the Pony Express in the nineteenth century. I'm discussing a technique of combining related HTML snippets into one HTML response by wrapping each of the snippets into separate envelopes. This single HTML response that contains multiple envelopes is a mochila. This technique can reduce the number of HTTP requests you need, and offers the ability to re-use existing partials without needing to create a JS template that duplicates a partial.
The example I'll use to illustrate the mochila pattern is an index page that shows a list of video thumbnails. When you hover over a thumbnail, it plays in the foreground while another version of the same video plays fullscreen in the background. The background video has a beautifully subdued alpha and gradient overlay. And the effect adds lots of interest and an artful quality to our page.
In our page the thumbnail video and the background video are separate HTML elements that live on different parts of the page. The background videos are just inside the body tag. The thumbnails are inside of an unordered list. However they are clearly related. If we want to add another video to the page with ajax we need to add snippets of HTML to both the list and the body for the respective new thumbnail and background videos. Here is the skeleton markup to illustrate that:
There are many ways we could add another video dynamically: multiple requests, json requests that populate JS templates, but we're interested in using a mochila. Here is a simple illustration of the technique:
Note how we can use jQuery to manipulate the HTTP response just like we would use it for any other part of the page. This makes it simple to grab the separate envelopes from the mochila. I think the other techniques have their place as well. If you've already got a JS templating system in place, that can be a powerful technique. But for a simple rails app you might be content to use this technique to get a little more mileage from your already built partials.
Like any technique the mochila can be abused. Filling a mochila with unrelated content or too many envelopes could create confusion and would lead to ugly JS pre-processing on the client. Add this trick to your arsenal, but don't abuse it. Know your options. Have a look at underscore.js templates if you haven't already.
The example I'll use to illustrate the mochila pattern is an index page that shows a list of video thumbnails. When you hover over a thumbnail, it plays in the foreground while another version of the same video plays fullscreen in the background. The background video has a beautifully subdued alpha and gradient overlay. And the effect adds lots of interest and an artful quality to our page.
In our page the thumbnail video and the background video are separate HTML elements that live on different parts of the page. The background videos are just inside the body tag. The thumbnails are inside of an unordered list. However they are clearly related. If we want to add another video to the page with ajax we need to add snippets of HTML to both the list and the body for the respective new thumbnail and background videos. Here is the skeleton markup to illustrate that:
There are many ways we could add another video dynamically: multiple requests, json requests that populate JS templates, but we're interested in using a mochila. Here is a simple illustration of the technique:
Note how we can use jQuery to manipulate the HTTP response just like we would use it for any other part of the page. This makes it simple to grab the separate envelopes from the mochila. I think the other techniques have their place as well. If you've already got a JS templating system in place, that can be a powerful technique. But for a simple rails app you might be content to use this technique to get a little more mileage from your already built partials.
Like any technique the mochila can be abused. Filling a mochila with unrelated content or too many envelopes could create confusion and would lead to ugly JS pre-processing on the client. Add this trick to your arsenal, but don't abuse it. Know your options. Have a look at underscore.js templates if you haven't already.
Monday, July 23, 2012
Greed scoring solution for the Edgecase Ruby Koans.
Edgecase has some nice koans for learning Ruby. Going through them is a good refresher for some of the Ruby features you don't use that often, or the parts of Ruby where the principle of least surprise has gotten into the weeds (multiple assignment i'm talking to you).
I wrote mine as a class and did TDD so I have my own test suite, but when I plug it into the koans, all is well. My original solution had a bunch of conditional statements, but some refactoring got rid of most of them. Using modulo was the key. Have a look, feedback is welcome.
Tuesday, July 17, 2012
Scale worker processes in Heroku for Sinatra with Delayed Job
I've done this a few times for some of my personal projects and it works pretty well. Every single time I do it I always have the same problems so here is a checklist to keep you from going off the rails (HAHAHA!).
- Make sure you've got the right gems: delayed_job, delayed_job_active_record, workless, heroku
- Make sure you've configured delayed job to use your workless process scaler, and your scaler matches the heroku stack you are running
- Create a Procfile with your worker definition (see gist above)
- Set all required environment variables: APP_NAME, HEROKU_PASSWORD, HEROKU_USER
- I usually use the heroku scheduler app add-on to run my rake task that adds stuff to my jobs table.
I've seen the following HTTP errors when running misconfigured apps:
- 404. This is what you'll get if you are missing the APP_NAME because if you look at the ps_scale method in the heroku gem, it is using that name to build a URL so without it your URL will be broken.
- 401. This is what you'll get if you forget to set the username and password environment variables because the ps_scale method of the heroku api requires authentication.
- 422 (Unprocessable Entity). This is what you'll get if you forget the procfile. It is a shitty error message that is trying to tell you that heroku doesn't know how to create a worker process if you don't define it. Ick.
Allright. Best of luck future me, and whoever else finds this useful.
Subscribe to:
Posts (Atom)