Skip to content

Instantly share code, notes, and snippets.

View marka2g's full-sized avatar
🎯
Focusing

Mark Sadegi marka2g

🎯
Focusing
View GitHub Profile
const memoize = (fn) => {
let cache = {}, key;
return (...args) => {
key = JSON.stringify(args);
return cache[key] || (cache[key] = fn.call(null, ...args));
}
};
@marka2g
marka2g / index.md
Created May 4, 2017 18:43 — forked from rstacruz/index.md
Rails models cheatsheet

Rails Models

Generating models

$ rails g model User

Associations

belongs_to

has_one

@marka2g
marka2g / description.markdown
Created May 3, 2017 22:08 — forked from runemadsen/description.markdown
Reverse polymorphic associations in Rails

Polymorphic Associations reversed

It's pretty easy to do polymorphic associations in Rails: A Picture can belong to either a BlogPost or an Article. But what if you need the relationship the other way around? A Picture, a Text and a Video can belong to an Article, and that article can find all media by calling @article.media

This example shows how to create an ArticleElement join model that handles the polymorphic relationship. To add fields that are common to all polymorphic models, add fields to the join model.

@marka2g
marka2g / ocr.markdown
Created May 3, 2017 03:36 — forked from henrik/ocr.markdown
OCR on OS X with tesseract

Install ImageMagick for image conversion:

brew install imagemagick

Install tesseract for OCR:

brew install tesseract --all-languages

Or install without --all-languages and install them manually as needed.

@marka2g
marka2g / ruby_meta.md
Created March 22, 2017 23:21 — forked from jamesyang124/ruby_meta.md
Ruby meta programming

#!/bin/ruby --verion => 2.0.0-p353

Self

In Ruby, self is a special variable that always references the current object.

  • Inside class or module definition, self refer to the Class or Module object.
  • Inside instance method, self refer to future instance object.
  • Inside class method, self refer to the class.i
@marka2g
marka2g / testing_rspec_models.rb
Last active March 8, 2017 03:09
Testing Rspec Models
require 'spec_helper'
describe Model do
it "has a valid factory" do
expect(build(:factory)).to be_valid
end
# Lazily load factory so its only used when needed.
let(:factory_instance) { build(:parent) }
@marka2g
marka2g / gist:69ed04503d69a1c12c84ca55c8134adc
Created March 5, 2017 09:03 — forked from alex-zige/gist:5795358
Rails Rspec API Testing Notes

Rails Rspec APIs Testing Notes

Folders Structure

  spec
  |--- apis #do not put into controllers folder. 
        |--- your_api_test_spec.rb  
  |--- controllers
  |--- models
  |--- factories
 |--- views
@marka2g
marka2g / child_spec.rb
Last active March 2, 2017 17:39
Using `let` and `context` to test `#capture_time_zone`
# PUT THIS IN THE `spec_helper.rb` file
# <><><><><><><><><><><>
RSpec.configure do |config|
# Allows for shorthand version of FactoryGirl syntax.
config.include FactoryGirl::Syntax::Methods
#...
end
# <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
@marka2g
marka2g / android_timezone_names.txt
Last active September 12, 2016 19:23
A List of Android Time Zone Names
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
@marka2g
marka2g / gist:07f71e10f3177b77af30
Created February 11, 2016 04:35
Rails Routes and Helpers
r = Rails.application.routes
r.recognize_path "/children"
r.methods - Object.methods