Sunday, April 13, 2014

Linkedin Share Button in Rails 4 with Turbolinks Enabled

While implementing Linkedin share-button on my Rails 4 application with Turbolinks enabled, I encounter problem how to reload Linkedin share-button correctly because my code always showing "duplicate in.js loaded, any parameters will be ignored".

Friday, November 15, 2013

Javascript Alert For Unsaved Changes with Turbolinks, Jquery, And Rails 4

jQuery change event can be used to detect form change. But because Turbolinks pages will change without full reload, we can't rely on DOMContentLoaded or jQuery.ready() to trigger change event. Here is how to write jQuery change event code with Turbolinks enabled. Tested with Rails 4.0.0.

# Coffeescript

ready = >
  # your jquery code
  # ...

# page:load is fired at the end of the loading process.
$(document).on('page:load', ready)

change = ->
  $("form input, form textarea").change( ->
    $('a').click( ->
      confirm('Unsaved changes on the page. Are you sure?')


# the page has been parsed and changed to the new version and on DOMContentLoaded
$(document).on('page:change', change)

# Javascript

# ...
change = function() {
  $("form input, form textarea").change(function() {
    $('a').click(function() {
      confirm('Unsaved changes on the page. Are you sure?');
# ...


Friday, September 6, 2013

Ruby Simple Progress Bar Without Ruby Gems

Simple progress bar script without Gem using Ruby and console.


#!/usr/bin/env ruby

progress = 'Progress ['
1000.times do |i|

# i is number from 0-999
j = i + 1

  # add 1 percent every 10 times
  if j % 10 == 0
    progress << "="
    # move the cursor to the beginning of the line with \r
    print "\r"
    # puts add \n to the end of string, use print instead
    print progress + " #{j / 10} %"

    # force the output to appear immediately when using print
    # by default when \n is printed to the standard output, the buffer is flushed. 
    sleep 0.05
puts "\nDone!"