Pages

Monday, July 13, 2015

Ruby Inheritance, Encapsulation and Polymorphism

Inheritance
Inheritance is a relation between two classes. A child class inherits all the features of its parent class. Methods from the parent can be overridden in the child and new logic can be added.
Usually, inheritance is used to specialize a class. See the following example :
class Document
  def initialize; end

  # logic to deal with any document

  def print
    # logic to print any kind of document
  end
end
class XmlDocument < Document
  # logic to deal with any document

  def print
    # logic to print a xml document
  end
end
A class can only inherit from one class as opposed to c++ where multi-inheritance can be done (not always for the better).

Thursday, July 9, 2015

Send PDF attachments from Rails with WickedPdf and ActionMailer

In almost any web application you create, the question of generating PDF files will pop up pretty soon. 

Setup

As always, using a ruby gem in rails is pretty simple, you just add a couple of lines to the Gemfile
gem 'wicked_pdf'
#we need the new binary here, so that we can be OS independent
gem 'wkhtmltopdf-binary', github: 'pallymore/wkhtmltopdf-binary-edge', tag: 'v0.12.2'

Usage

This setup will work pretty straightforward in the controllers, because WickedPdf registers :pdf request format, and you can respond to it in the same fashion as html or js in a respond_to block. Code below is copied from the WickedPdf Readme page.