Skip to content

Instantly share code, notes, and snippets.

View abarrak's full-sized avatar

Abdullah Barrak abarrak

View GitHub Profile
@abarrak
abarrak / extract_values_striped.rb
Last active May 13, 2017 11:07
Extract values within a hash stripped of whitespace
def extract_values_striped(hash, *keys)
hash.values_at(*keys).map { |value| value.respond_to?(:strip) ? value.strip : value }
end
# test case ...
test "#extract_values_striped helper takes a hash & keys list, then return their values stripe" do
my_collection = { book: 'the pragmatic programmer', language: 'ruby', hoppy: 'swimming' }
values = extract_values_striped(my_collection, :book, :hoppy, :toy)
@abarrak
abarrak / invalidate_badge.md
Last active May 14, 2017 00:42
How to force updating a cached image/badge on GitHub?
@abarrak
abarrak / ObjectDumper.cs
Last active November 29, 2018 11:28
C# Object Attibutes Dumper
/// Slighty adapted from: stackoverflow.com/a/10478008
using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
public class ObjectDumper
@abarrak
abarrak / array_extensions.js
Created July 15, 2017 12:14
Extends Array in vanilla javascript
Array.prototype.sample = function() {
return this[Math.floor(Math.random() * this.length)]
};
Array.prototype.remove = function(element) {
var index = this.indexOf(element);
if (index > -1) {
this.splice(index, 1);
}
return this;
@abarrak
abarrak / pundit_custom.rb
Last active July 18, 2017 11:59
Manual Pundit authorization for Polymorphic record
def authorize_for_my_action(child_record)
raise Pundit::NotAuthorizedError unless ParentPlociy.new(pundit_user, child_record).my_action?
skip_authorization
end
@abarrak
abarrak / gist:6db55951fb9ba20a8584fd49eeffa5e3
Created August 6, 2017 08:52 — forked from Andrew-Max/gist:305483febc3c367dbf57
Route lifecycle hooks guide from Ember meetup lightning talk
App.LibraryRoute = App.ApplicationRoute.extend({
activate: function () {
//no longer enter
this._super();
only called once on entering a route.
},
beforeModel: function () {
// any state you want in place before the model is initialized, this is called before any model promises are resolved
// also could be used to conditionally prevent access to a route by throwing transition.abort
@abarrak
abarrak / flattener.rb
Last active August 10, 2017 14:04
Flattener
# https://stackoverflow.com/a/35963024
module Flattener
def deep_flatten
flatten.map do |item|
case item
when Hash, Array
item.deep_flatten
else
item
@abarrak
abarrak / systme_update_rules.rake
Last active August 27, 2017 20:33
Run rake task after capistrano deployment
namespace :system do
desc 'Update the system rules '
task :update_rules do
on roles(:all) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, 'system:update_rules'
end
end
end
@abarrak
abarrak / array_to_relation.rb
Created September 7, 2017 08:09 — forked from lappi-lynx/array_to_relation.rb
Rails: Convert array into ActiveRecord Relation object
scope :with_scope, -> { where(id: ARRAY_COLLECTION.map(&:id)) }