Multi-column PDF generating with Prawn in ruby

For example, you need to generate a content (or table) with width 20-30% of page and it should not break the page if height is too big but should continue on the same page in the right side.

It will look like that:

Prawn has very useful methodfor doing that called ‘column_box’.

# Example of 2 columns table with some styling
def render_left_products_list
    column_box([-5, cursor], columns: 2, height: cursor, position: :left, width: bounds.width, spacer: 40) do

      @categories.each do |category|
        move_down 6
        table line_item_rows(category), position: :left, cell_style: { size: 7, text_color: "000000", borders: [:bottom], border_lines: [:dotted], padding: [2, 0, 4, 0] } do
          column(1).style align: :right
          style(row(0), text_color: HEADER_COLOR)
          align                = { 0 => :left, 1 => :right}
          self.width           = 285
          self.header          = true
          column(1).font_style = :bold
          row(0).size          = 8
          row(0).font_style    = :bold
          row(0).borders       = []

  def line_item_rows(category)
    [[, '']] + do |product|
      [, "#{format('%.2f', product.price)} USD"]

Ruby on Rails convert array into Relation object

scope :with_scope, -> { where(id: }

Language plural rules (includes I18n)

Table with plural rules: Читать дальше »


Rails 4.1 html and tags chars sanitize

To remove all tags and chars like ‘nbsp;’ in rails 4.1 do that:
= sanitize(post.description).truncate(350, :separator => " ").html_safe

Rails timezones difference

  • In Rails, refers to the Rails (not system) timezone, set by config.time_zone (typically in application.rb)
  • DateTime and Time are not otherwise Rails aware, therefore, and both return times in the system timezone.
  • 1.month.ago and similar methods use the Rails timezone, but uses the system timezone
  • When retrieving things from ActiveRecord, they will be timezoned to the Rails timezone, so Product.first.created_at will give you a time in the Rails timezone (not the system timezone)

So, don’t mix Rails timezone methods like “1.month.ago” with system timezone methods like “”


Отправка формы с валидацией, задержкой и с текстом благодарности

$('form').submit(function (e) {
    var form = this;
    if ($(this).find('.phone_number').val().length) {
      setTimeout(function () {
      }, 3000); // in milliseconds
    } else {

Fast and Simple youtube embedding

You can embed as many videos as you want and it still be fast.


  <div class="video_name">Test video</div>
   <div class="video" style="background-color:#ccc; width:420px; height:315px;">
      <a href="" class="youtube"></a>


<script type="text/javascript">
    function getYoutubeID(url) {
        var id = url.match("[\\?&]v=([^&#]*)");
        id = id[1];
        return id;
    $('').each(function() {
        var id = getYoutubeID( this.href ); = id;
        var thumb_url = ""+id+"/0.jpg";
        $('<img width="100%" src="'+thumb_url+'" />').appendTo($(this.parentNode));


    $('.video').on('click', function(e) {
        var link = $(this).find('a')[0];
        var id = getYoutubeID( link.href );
        $(this).html('<iframe width="420" height="315" src="//'+ id +'?autoplay=1" frameborder="0" allowfullscreen></iframe>').css('background', 'none');