has_barcode

<img src=“https://secure.travis-ci.org/dpickett/has_barcode.png” />

A nice wrapper for Barcode generation using barby.

class Product
  include HasBarcode

  has_barcode :barcode,
    :outputter => :png,
    :type => :code_39,
    :value => Proc.new { |p| p.number }

  def number
    self.id
  end
end

Product.new.barcode       # => Barby::Code39 object
Product.new.barcode_data  # => <Barby::Code39 object>.to_png

You can also pass the Barby barcode type directly like:

require 'barby/barcode/code_39'
class Product
  include HasBarcode

  has_barcode :barcode,
    :outputter => :png,
    :type => Barby::Code39,
    :value => Proc.new { |p| p.number }
end

Why has_barcode is a good choice for Heroku

Other libraries – such as barcode_generator – commonly rely on gbarcode, a GNU Barcode C library. This is problem since you need to install GNU barcode which Heroku does not support. Luckily has_barcode doesn’t have this dependency.

A typical Heroku setup might look like:

class Coupon < ActiveRecord::Base
  include HasBarcode

  has_barcode :barcode,
    :outputter => :svg,
    :type => :code_39,
    :value => Proc.new { |c| c.id }

end

Notice we’re using the svg outputter. This is good choice since it doesn’t need to store images in the file system, which can be an issue given Heroku’s read only file system.

To display your barcode in the view, do something like:

@coupon.barcode_data.html_safe

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but

    bump version in a commit by itself I can ignore when I pull)
    
  • Send me a pull request. Bonus points for topic branches.

Copyright © 2009 Dan Pickett. See LICENSE for details.