Skip to content

Instantly share code, notes, and snippets.

View umar-webonise's full-sized avatar

Umar Siddiqui umar-webonise

View GitHub Profile
@umar-webonise
umar-webonise / tit_for_tat
Last active August 29, 2015 14:15
Tit For Tat Solution
(1..100).each do |num|
result = 'Tit' if num % 3 == 0
if num % 5 == 0
result &&= result + 'ForTat'
result ||= 'Tat'
end
result ||= num
puts result
end
@umar-webonise
umar-webonise / gist:73641a253a03d405e64e
Created March 5, 2015 09:15
chonse jquery and angular js directive
TreeniApp.directive('chosen', function() {
var linker = function(scope, element, attr) {
// update the select when data is loaded
scope.$watch(attr.chosen, function(oldVal, newVal) {
element.trigger('chosen:updated');
});
// update the select when the model changes
scope.$watch(attr.ngModel, function() {
element.trigger('chosen:updated');
@umar-webonise
umar-webonise / csv to array
Created March 10, 2015 12:58
CSV to Array
factory.CSVtoArray = function (text) {
var re_valid = /^\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*(?:,\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*)*$/;
var re_value = /(?!\s*$)\s*(?:'([^'\\]*(?:\\[\S\s][^'\\]*)*)'|"([^"\\]*(?:\\[\S\s][^"\\]*)*)"|([^,'"\s\\]*(?:\s+[^,'"\s\\]+)*))\s*(?:,|$)/g;
// Return NULL if input string is not well formed CSV string.
if (!re_valid.test(text)) return null;
var a = []; // Initialize array to receive values.
text.replace(re_value, // "Walk" the string using replace with callback.
function(m0, m1, m2, m3) {
// Remove backslash from \' in single quoted values.
if (m1 !== undefined) a.push(m1.replace(/\\'/g, "'"));
@umar-webonise
umar-webonise / units_seed_data
Created March 31, 2015 14:34
Seed Data for adding units for dynamice forms
unit_types = [
{ name: 'length' },
{ name: 'area' },
{ name: 'weight' },
{ name: 'volume' }
].each do |tag_info|
puts "Added #{UnitType.create(tag_info).inspect}"
end
@umar-webonise
umar-webonise / gist:8d5921b1892a3bff0931
Created April 10, 2015 10:10
Multi select drop down with tree indentation
<div class="control labelMove bottomSpace4">
<select
id="org_node"
name="orgNode"
class="chosen-select"
chosen="treeDropOptions"
required
ng-model="indicator.org_node_id">
<option ng-repeat="opt in treeDropOptions" ng-value="opt.id">
{{Array(opt.lvl * 7).join("&nbsp;")}} {{opt.name}}
@umar-webonise
umar-webonise / dynamice_active_model_serializers.rb
Last active August 29, 2015 14:19
Dynamic Fields for active_model_serializers
class YourDynamicModelSerializer < ActiveModel::Serializer
attributes :_id, :name
def _id
object._id.to_s
end
def attributes
fields = object.attributes
@umar-webonise
umar-webonise / bson.rb
Created May 27, 2015 05:30
Omiting $oid when coverting to json
# place code in config/initializers/bson.rb
module BSON
class ObjectId
def as_json(*args)
to_s
end
end
end
@umar-webonise
umar-webonise / $oid omit
Created June 15, 2015 06:13
Work ignoring mongoid $oid
# Patch for mongoid to return $oid instead of active record id
class << self
def serialize_from_session(key, _salt)
to_adapter.get(key[0])
end
end
@umar-webonise
umar-webonise / mongoid.yml
Created June 25, 2015 03:11
for treeni dp connectivity
development:
# Configure available database sessions. (required)
sessions:
# Defines the default session. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: treeni_development
# Provides the hosts the default session can connect to. Must be an array
# of host:port pairs. (required)
@umar-webonise
umar-webonise / etl_script_request.rb
Created June 30, 2015 05:03
Request to generate etl script
{
'source' => {
'type' => 'csv',
'file' => {
'path' => '/home/webonise/Projects/Data-Integration/dump/test.csv'
},
'options' => {
'col_sep' => ','
}
},