Skip to content

Instantly share code, notes, and snippets.

View morshedalam's full-sized avatar
🏔️

Morshed Alam morshedalam

🏔️
View GitHub Profile
#config/initializers/secret_token.rb
# Be sure to restart your server when you modify this file.
require 'securerandom'
def find_token(file_name)
token_file = Rails.root.join(file_name)
f = File.open(token_file, 'w+')
token = f.read.chomp
# Use: it { should accept_nested_attributes_for(:association_name).and_accept({valid_values => true}).but_reject({ :reject_if_nil => nil })}
RSpec::Matchers.define :accept_nested_attributes_for do |association|
match do |model|
@model = model
@nested_att_present = model.respond_to?("#{association}_attributes=".to_sym)
if @nested_att_present && @reject
model.send("#{association}_attributes=".to_sym,[@reject])
@reject_success = model.send("#{association}").empty?
end
model.send("#{association}").clear
@morshedalam
morshedalam / choice_able.rb
Last active August 29, 2015 14:04
Populate drop-down options
#lib/choice_able.rb
module ChoiceAble
def choice_able_by(columns, orders = nil)
@choice_able_columns = columns
@choice_able_orders = orders
end
def choices(columns = nil, orders = nil)
columns ||= @choice_able_columns
orders ||= @choice_able_orders
@morshedalam
morshedalam / Multiple rows value as column
Last active August 29, 2015 14:02
Retrieve joined rows value as columns
DROP FUNCTION IF EXISTS SPLIT_STR;
CREATE FUNCTION SPLIT_STR(
x VARCHAR(255),
delim VARCHAR(12),
pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(
SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
-- SPLIT_STR MySQL Function
-- from http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
CREATE FUNCTION SPLIT_STR(
x VARCHAR(255),
delim VARCHAR(12),
pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
@morshedalam
morshedalam / focus-losing-callback.js
Last active August 29, 2015 14:01
Callback on losing window focus
document.onfocusout = function (e) {
if (e === undefined) {//ie
e = event;
if (e.toElement == null) {
//do your action on focus out
}
}
};
@morshedalam
morshedalam / dirty-checking.js
Last active August 29, 2015 14:00
Dirty checking
var dirtyChecking = function () {
$('input, select, textarea').each(function () {
var ele = $(this);
ele.attr('data-old', ele.val());
// Look for changes in the value
ele.on("change keyup paste click", function (event) {
if (ele.attr('data-old') != ele.val()) {
ele.addClass('unsaved');

##How Homakov hacked GitHub and the line of code that could have prevented it


Please note: THIS ARTICLE IS NOT WRITTEN BY THE GITHUB TEAM or in any way associated with them. It's simply hosted as a Gist because the markdown formatting is excellent and far clearer than anything I could manage on my personal Tumblr at peternixey.com.

If you'd like to follow me on twitter my handle is @peternixey


@morshedalam
morshedalam / export-git-changes
Last active August 29, 2015 13:56
Export git changes
#!/bin/bash
# Target directory
TARGET=update/changes
for i in $(git diff --name-only --stat origin/master)
do
# First create the target directory, if it doesn't exist.
mkdir -p "$TARGET/$(dirname $i)"
# Then copy over the file.
cp "$i" "$TARGET/$i"
@morshedalam
morshedalam / AutoComplete - AppModel
Last active March 7, 2017 10:14
AutoComplete using CakePHP and JQuery
public function autocomplete($conditions = array(), $id = 'id', $label = 'name', $value = 'name')
{
$responses = array();
$rows = $this->find('all', array(
'conditions' => $conditions,
'fields' => array(
"{$id}",
"{$label}",
"{$value}",