Skip to content

Instantly share code, notes, and snippets.

View morshedalam's full-sized avatar
🏔️

Morshed Alam morshedalam

🏔️
View GitHub Profile
require 'securerandom'
def find_secure_token
token_file = Rails.root.join('.secret_token')
if File.exist? token_file
File.read(token_file).chomp
else
token = SecureRandom.hex(64)
f = File.new(token_file, 'w')
f.write(token)
class Time
def diff_to_i(from = nil)
from = Time.now if from.blank?
(from - self).to_i
end
def diff_to_words(from = nil)
diff = self.diff_to_i(from)
case diff
@morshedalam
morshedalam / Remove empty value on submit
Last active December 30, 2015 08:29
Remove form field with empty value on submit
var disableEmptyFormField = function (ele){
ele.find('input,select').each(function () {
if ($(this).val() == '') {
$(this).attr("disabled", "disabled");
}
});
ele.find('button').attr("disabled", "disabled");
$('.filter-manufacturers input[type="checkbox"]').removeAttr('checked');
}
@morshedalam
morshedalam / CakePHP-Update paging params
Last active December 30, 2015 08:29
Remove empty field from CakePHP pagination
foreach($this->Paginator->options['url']['?'] $ key => $val){
if(empty($val))
unset($this->Paginator->options['url']['?'][key]);
}
@morshedalam
morshedalam / htaccess-CakePHP
Created December 5, 2013 15:32
Using directory from outside of app in CakePHP
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(downloads)($|/) - [L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
@morshedalam
morshedalam / sorting using php
Created December 9, 2013 13:20
Sorting multidimensional array
//Sorting using a column
usort($rows, function ($a, $b) {
return strcmp($a[0], $b[0]);
})
//Redirect from www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
//Redirect from non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
$( '.basic-filter-options input[type="checkbox"]' ).click(function(e){
$('.basic-filter-options input[type="checkbox"]').filter(':checked').not(this).removeAttr('checked');
});

Ruby on Rails coding standard

{{toc}}

Code style

  • Use UTF-8. It’s 21 century, 8bit encodings dead now.
  • Use 2 space indent, not tabs
  • Use Unix-style line endings
  • Keep lines not longer than 80 chars
<?php
App::import('Inflector');
class SluggableBehavior extends ModelBehavior
{
private $_settings = array();
function setup(&$model, $settings = array())
{