Skip to content

Instantly share code, notes, and snippets.

View abarrak's full-sized avatar

Abdullah Barrak abarrak

View GitHub Profile
@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 / 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 / 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 / invalidate_badge.md
Last active May 14, 2017 00:42
How to force updating a cached image/badge on GitHub?
@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 / assert_differences.md
Last active April 17, 2017 23:14
Assert Difference With Multiple Count Values
# Runs assert_difference with a number of conditions and varying difference
# counts.
#
# Call as follows:
#
# assert_differences([['Model1.count', 2], ['Model2.count', 3]])
#
def assert_differences(expression_array, message = nil, &block)
 b = block.send(:binding)
@abarrak
abarrak / test_helper.rb
Last active June 24, 2022 11:58
Perform Enqueued Mail Job in Rails Tests
# Extend in your test_helper.rb or any other `/supprot` setup you have.
class ActiveSupport::TestCase
##
# === NOTICE:
# Ensure you have `ActionMailer::TestHelper` in the test class ..
#
# This method performs any enqueued job during tests manually,
# Helpful when testing the queueing of your jobs then the result of their execution.
@abarrak
abarrak / application_helper.rb
Created February 24, 2017 20:05
Day greeting phrase for Rails
##
## Depends on ActiveSupport
##
# Initial imple: stackoverflow.com/a/32279088
def greeting_phrase
now = Time.zone.now
today = Date.current.to_time
morning = today.beginning_of_day
noon = today.noon
@abarrak
abarrak / alerts.swift
Created February 19, 2017 11:05
Alert snippet in Swift
//
// ViewControllerExtension.swift
// VirtualTourist
//
// Created by Abdullah on 2/15/17.
//
import UIKit
extension UIViewController {
@abarrak
abarrak / stringToDate.swift
Created February 3, 2017 12:58
String to Date in Swift Lang
//
// Convert string to a proper formatted date type.
//
// Swift 3.0
private func stringToDate(dateString: String?) -> Date? {
if let str = dateString {
let formatter = DateFormatter()
formatter.dateFormat = "MMM dd, yyyy h:mm a"
formatter.amSymbol = "AM"