dpickett / has_barcode

Nice class method wrapper for barby
MIT License
51 stars 18 forks source link

= has_barcode

{}[http://travis-ci.org/dpickett/has_barcode]

A nice wrapper for Barcode generation using {barby}[https://github.com/toretore/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 # => .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}[https://github.com/anujluthra/barcode-generator] – commonly rely on {gbarcode}[https://github.com/ahx/gbarcode], a GNU Barcode C library. This is problem since you need to install GNU barcode which {Heroku does not support}[https://github.com/perezd/barcoder/issues/1]. 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}[http://devcenter.heroku.com/articles/read-only-filesystem].

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

@coupon.barcode_data.html_safe

== Note on Patches/Pull Requests

== Copyright

Copyright (c) 2009 Dan Pickett. See LICENSE for details.