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).ready(ready)
$(document).on('page:load', ready)

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

$(document).ready(change)

# 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?');
    });
  });
}; 
# ...


Gist: https://gist.github.com/kuntoaji/7472721

Friday, September 6, 2013

Ruby Simple Progress Bar Without Ruby Gems

Simple progress bar script without Gem using Ruby and console.

Gist: https://gist.github.com/kuntoaji/6460383

#!/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. 
    $stdout.flush
    sleep 0.05
  end
end
puts "\nDone!"