Skip to content

Instantly share code, notes, and snippets.

View sahidursuman's full-sized avatar

Sahidur Rahman Suman sahidursuman

View GitHub Profile
@sahidursuman
sahidursuman / matrix.rb
Created May 15, 2018 06:30 — forked from emad-elsaid/matrix.rb
Create Matrix Like screen using Ruby
#!/usr/bin/env ruby
require 'gosu' # gem install gosu
$width, $height = 200, 200
$number_of_v_lines,$number_of_h_lines = 10, 10
$chars = ('a'..'z').to_a
class Entity
def initialize(x,y,vel, win)
@pos, @vel = {x:x, y:y}, vel
@sahidursuman
sahidursuman / recurssive.tree.js
Created May 14, 2018 06:41 — forked from alonronin/recurssive.tree.js
Create recursive tree from json using lodash transform without recursion. Can have unlimited nesting.
var _ = require('lodash');
var arr = [
{"name":"my2child1","title":"My 2 Child 1","parent":"my2"},
{"name":"my2child2","title":"My 2 Child 2","parent":"my2"},
{"name":"parent","title":"A single parent"},
{"name":"child-parent","title":"A child parent","parent":"child1"},
{"name":"my","title":"My"},
{"name":"my2","title":"My2"},
{"name":"child1","title":"Child 1","parent":"my"},
@sahidursuman
sahidursuman / simple_authentication.rb
Created May 10, 2018 10:26 — forked from tomdalling/simple_authentication.rb
A simple Sinatra app that demonstrates basic authentication
#!/user/bin/env ruby
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'sinatra', '~> 1.4'
gem 'bcrypt', '~> 3.1'
end
require 'sinatra/base'
@sahidursuman
sahidursuman / binarytree.rb
Created May 9, 2018 05:47 — forked from yuya-takeyama/binarytree.rb
Binary Tree implemented in Ruby.
module BinaryTree
class Node
attr_reader :word, :count, :left, :right
include Enumerable
def initialize(word)
@word, @count = word, 1
end
@sahidursuman
sahidursuman / sessions_controller.rb
Created May 8, 2018 11:26 — forked from jhowarth/sessions_controller.rb
A session controller for Devise that allows ajax logins.
class SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => :failure)
return sign_in_and_redirect(resource_name, resource)
end
def sign_in_and_redirect(resource_or_scope, resource=nil)
scope = Devise::Mapping.find_scope!(resource_or_scope)
resource ||= resource_or_scope
@sahidursuman
sahidursuman / Update remote repo
Created May 3, 2018 12:39 — forked from mandiwise/Update remote repo
Transfer repo from Bitbucket to Github
// Reference: http://www.blackdogfoundry.com/blog/moving-repository-from-bitbucket-to-github/
// See also: http://www.paulund.co.uk/change-url-of-git-repository
$ cd $HOME/Code/repo-directory
$ git remote rename origin bitbucket
$ git remote add origin https://github.com/mandiwise/awesome-new-repo.git
$ git push origin master
$ git remote rm bitbucket
@sahidursuman
sahidursuman / dry.rb
Created April 29, 2018 19:48 — forked from harssh-sparkway/dry.rb
Don’t Repeat Yourself (DRY) in Ruby on Rails
#Don’t Repeat Yourself (DRY) in Ruby on Rails
#DRY (Don’t Repeat Yourself) is a principle of Software Development to reducing repetition of information or codes. We can #apply DRY quite broadly to database schema, test plan, system, even documentation. And in this post, we will take example of DRY #in Ruby on Rails development.
#In particular case, if you find some methods whose definitions are more or less similar, only different by the method name, it #may use meta programming to simplify the things to make your model more clean and DRY. Consider this simple example where we #have an article with three states.
#Before
class Article < ActiveRecord::Base
@sahidursuman
sahidursuman / reset.rb
Created April 19, 2018 09:08 — forked from seyhunak/reset.rb
Rails - Reset sequence based on ActiveRecord connection adapter
# Interest.destroy_all
case ActiveRecord::Base.connection.adapter_name
when 'SQLite'
update_seq_sql = "update sqlite_sequence set seq = 0 where name = 'interests';"
ActiveRecord::Base.connection.execute(update_seq_sql)
when 'PostgreSQL'
ActiveRecord::Base.connection.reset_pk_sequence!('interests')
when 'Mysql2'
update_seq_sql = "ALTER TABLE interests AUTO_INCREMENT = 1;"
ActiveRecord::Base.connection.execute(update_seq_sql)
@sahidursuman
sahidursuman / custom-tree.js
Created April 19, 2018 05:30 — forked from florent-blanvillain/custom-tree.js
Easyui drag and drop tree save extension
/*
* Extends Easyui tree and makes use of a surrounding panel to save the newly ordered tree.
* Minimum tree options are url, saveUrl (this one doesn't exist in easyui tree) and dnd:true
* The surrouding panel must have save and cancel tool ('.icon-save', '.icon-cancel')
* @flo
*/
$.extend(jQuery.fn.tree.methods, {
getDataTree: function (jq) {
var definitions = [], methods = this;
this.getRoots(jq).forEach(function (root) {
@sahidursuman
sahidursuman / date_scope.rb
Created April 17, 2018 11:29 — forked from corny/date_scope.rb
Flexible rails scope for date-attributes in a PostgreSQL table
#
# Scopes for date attribute
# https://gist.github.com/2235839
#
module Common::DateScope
extend ActiveSupport::Concern
included do
unless respond_to?(:year)