Skip to content

Instantly share code, notes, and snippets.

View mehulkar's full-sized avatar

Mehul Kar mehulkar

View GitHub Profile
@mehulkar
mehulkar / gist:4695682
Last active December 12, 2015 01:59
interpretation of man pages
  • [] = optional arguments
  • {} = flags that provide differnet functionality
  • | = or
  • <> = optional arguments for optional arguments
App.ServerValidatedTextField = Ember.View.extend
tagName: 'input'
type: 'text'
###
This is handy if you want your server to validate an input field.
Your server is expected to return a boolean value
When that `validInput` attribute changes, we'll replace the layoutTemplate
with something that indicates valid or invalid.
You'll need three templates: `_loadingTemplate`, `_validTemplate`, `_invalidTemplate`
# Lines 16-17 and 21-22 don't make sense to me.
# If I stub a method on the user object,
# why is it not stubeed for the same object on the association?
describe "#approved" do
let(:user) { FactoryGirl.create(:user) }
subject(:bulletin) { FactoryGirl.build(:bulletin_post) }
it "is false if the user is not approved" do
# user.stub(:approved?) { true }
@mehulkar
mehulkar / gist:5873197
Created June 27, 2013 01:07
truncating and adding ellipsis to string in ruby
# this is probably a fairly common use case
# but there must be a more elegant solution?
# shortened_url returns a bit.ly link or nil in development
class Post
def tweet_text
link_length = shortened_url ? shortened_url.length : ""
separator = "-"
available_chars = link_length - separator.length

protected methods can be called by all inherited classes, private methods can only be called by the class they're in.

In ruby, self.some_protected_method works, self.some_private_method doesn't work.

Here are examples

class Y
  def send_private;
 self.private_method_x
@mehulkar
mehulkar / git-shove
Created November 15, 2013 22:13 — forked from dnf/git-shove
#!/bin/sh
if [ "master" = "$(git rev-parse --abbrev-ref HEAD)" ] ; then
echo "\033[1;31mYou may not shove master\033[0m"
exit 1;
fi
exec git push --force origin HEAD
### Keybase proof
I hereby claim:
* I am mehulkar on github.
* I am mehulkar (https://keybase.io/mehulkar) on keybase.
* I have a public key whose fingerprint is 0124 35B0 BA24 1473 594F 5712 8565 B49E 13F9 BF48
To claim this, I am signing this object:
@mehulkar
mehulkar / gist:dca4b66e9e2e25f6f591
Last active August 29, 2015 14:07
ActiveModelSerializers

I always forget all the different ways of serializing associations things, so putting down notes:

has_many

class AuthorSerializer < ActiveModel::Serializer
  has_many :posts
emd
# Inspired by @mrmrs http://designbytyping.com/2014/10/17/cloning-repos/
user="mehulkar"
# curl the pages directly instead of saving
# the urls to a txt file first
for page in {1..3}; do
curl "https://api.github.com/users/$user/repos?page=$page&per_page=100" >> repos.json
done
@mehulkar
mehulkar / gist:6964b68b3e2302e81f9b
Last active August 29, 2015 14:08
git precommit hook for rspec
#!/usr/bin/env bash
# Add this file to .git/hooks/pre-commit
# and make it executable.
# 1. greps the repo for "focus: true"
# 2. counts number of results
# 3. Strips the whitespace from the resulting number string
num=$(git grep "focus: true" | wc -l | xargs)