Brocade generates barcodes for Rails ActiveRecord models.

I extracted this from one of my projects and, although the code is nice and extensible, right now it only does what I need in that project. So for example it could produce barcodes in any symbology -- but currently only does Code 128.

There are two parts to Brocade: barcode generation and file management (because the barcodes are saved as image files). I use Barby to generate the barcodes and code copied from Paperclip to manage the files.


  • No configuration necessary...or possible ;)
  • Supports Code 128 symbology. Could support any symbology.
  • Generates barcode images as PNGs via ImageMagick. Could support other output formats.
  • Stores barcode images on disk. Could support other storage.

Basic Usage

Brocade is simple to use:

class Item < ActiveRecord::Base

  def barcodable

First declare declare your model has_barcode. Second override the barcodable method to return the name of the method Brocade should call to get the data to barcode.

Now you get this:

>> item = Item.create :serial_number => 42, :name => 'Deep Thought'
# writes barcode to /path/to/your/app/public/system/barcodes/items/3615/code128.png

-- assuming item's id is 3615.

>> item.update_attributes :name => 'Deeper Thought'
# no change to barcode

>> item.update_attributes :serial_number => 153
# writes barcode to /path/to/your/app/public/system/barcodes/items/3615/code128.png
# i.e. writes out a new barcode image over the top of the original one

>> item.barcode_path
# => "/path/to/your/app/public/system/barcodes/items/3615/code128.png"

>> item.barcode_url
# => "/system/barcodes/items/3615/code128.png"

>> item.destroy
# deletes barcode image.

'Advanced' Usage

You can pass options to Brocade to control the PNG it generates. For example:

class Item < ActiveRecord::Base
  has_barcode :margin => 5, :height => 40

These options are passed through to Barby's PNG Outputter. Note that setting the width makes no difference.


Install as a gem. In your config.rb:

config.gem 'brocade'


The Barby and PNG gems, and ImageMagick.


Please use GitHub's issue tracker.

To do

  • Tests. Yes, yes, I know.
  • Configurable way to specify data to be barcoded.
  • Configurable symbology.
  • Multiple symbologies per model.
  • Configurable file path and URL.
  • Other outputters.
  • Other storage.

Further reading


Intellectual Property

Copyright (c) 2010 Andy Stewart. See LICENSE for details.