Skip to content

Instantly share code, notes, and snippets.

View agungyuliaji's full-sized avatar
🏠
Working from home

Agung Yuliaji agungyuliaji

🏠
Working from home
View GitHub Profile
@agungyuliaji
agungyuliaji / include_jquery_from_console.js
Created May 5, 2014 13:16
Include jQuery in the JavaScript Console
var jq = document.createElement('script');
jq.src = "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
// ... give time for script to load, then type.
jQuery.noConflict();
@agungyuliaji
agungyuliaji / mymodel.rb
Created May 2, 2014 12:07 — forked from aliang/mymodel.rb
Render from model in Rails 3
# yes, sometimes you need to do this. you get pilloried in a forum if you
# ask about it, though!
# this code taken from
# http://wholemeal.co.nz/blog/2011/04/05/rendering-from-a-model-in-rails-3/
class MyModel < ActiveRecord::Base
# Use the my_models/_my_model.txt.erb view to render this object as a string
def to_s
ActionView::Base.new(Rails.configuration.paths.app.views.first).render(
@agungyuliaji
agungyuliaji / download_and_compress_rails_api
Last active December 26, 2015 12:09 — forked from guzart/download
Download and Compress Rails API
#!/bin/bash
rm -rf api.rubyonrails.org/
wget -r -k -p http://api.rubyonrails.org/
rm rails_api.rar
rar a -r rails_api.rar api.rubyonrails.org/
@agungyuliaji
agungyuliaji / postgres_change_column_string_to_integer.rb
Last active December 20, 2015 10:49
Postgres change column string to integer
class ChangeColumnOnMovementShipmentDvcaAndDvcu < ActiveRecord::Migration
def change
rename_column :movement_shipments, :dvca, :dvca_s
rename_column :movement_shipments, :dvcu, :dvcu_s
add_column :movement_shipments, :dvca, :decimal, default: 0
add_column :movement_shipments, :dvcu, :decimal, default: 0
MovementShipment.reset_column_information
MovementShipment.find_each { |c| c.update_attributes({
@agungyuliaji
agungyuliaji / file_browser_live_image_preview.html
Last active December 18, 2015 11:59
Live image preview for file browser
<h1>Sample#preview_image_file_browser</h1>
<input id="one_image" name="one_image" type="file">
<div id="preview_container"></div>
<script type="text/javascript">
var setPreviewImage = function(file){
var reader = new FileReader(),
newImage = new Image();
@agungyuliaji
agungyuliaji / check_array_if_all_element_is_0.rb
Created March 6, 2013 08:03
Check array if all element is 0
[0,0,0,0,0,0].all? {|a| a.zero? } # => true
[0,1,0,0,0,0].all? {|a| a.zero? } # => false
@agungyuliaji
agungyuliaji / validate_text_field_with_some_words.html
Last active December 13, 2015 18:59
Validate Text Field with some words
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<input id="test">
@agungyuliaji
agungyuliaji / get_response_code_on_rescue_from.rb
Created February 13, 2013 09:17
Get Response Code on rescue_from
# code snippet is taken from app/controllers/application_controller.rb
rescue_from Exception do |exc|
@response = Rack::Utils.status_code(ActionDispatch::ShowExceptions.rescue_responses[exc.class.name]).to_s
case @response
when "401", "404", "422"
@text = "You may have mistyped the address or the page may have moved."
when "500"
@text = "There was an internal error (failure). Please wait a few minutes and try again."
@agungyuliaji
agungyuliaji / activerecord-find-by-year-day-or-month-on-a-date-field.rb
Last active September 12, 2023 00:13
ActiveRecord Find By Year, Day or Month on a Date field
Model.where(:date_column => date)
Model.where('extract(year from date_column) = ?', desired_year)
Model.where('extract(month from date_column) = ?', desired_month)
Model.where('extract(day from date_column) = ?', desired_day_of_month)