Skip to content

Instantly share code, notes, and snippets.

View roychri's full-sized avatar

Christian Roy roychri

View GitHub Profile
@roychri
roychri / chain_conditions.rb
Created October 3, 2012 13:26
How to add activerecord query calls to existing
def a
e = Event.order("id DESC")
e = e.offset(3)
e = e.limit(1)
e
end
a.count
@roychri
roychri / method_wrapper.rb
Created October 5, 2012 21:37
Instance variable causes error: NoMethodError: undefined method `<<' for nil:NilClass on line 106
# Allows you to wrap every class method of your class around some code that
# can be run before and/or after every single class methods.
#
# ==== Example
#
# class MyClass
#
# extend MethodWrapper
# before_method :i_am_first
# after_method :i_am_last
@roychri
roychri / fubar.rb
Created October 17, 2012 19:15
Instance method of a class in a module to create an instance of the includer's class.
module Foo
class C
def bar
self.new # I want this to return an object of type Fubar
end
end
end
class Fubar
@roychri
roychri / prompt.sh
Created November 2, 2012 20:54 — forked from tobiassjosten/prompt.sh
Megred in most of the forks made my everyone up to now. Speed, keep existing PROMPT_COMMAND, add titlebar information, staged-unstaged, cleaning up
# Configure colors, if available.
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
c_reset='\[\e[0m\]'
c_user='\[\e[0;32m\]'
c_path='\[\e[1;34m\]'
c_git_clean='\[\e[0;37m\]'
c_git_staged='\[\e[0;32m\]'
c_git_unstaged='\[\e[0;31m\]'
else
c_reset=
@roychri
roychri / 20121112152734_create_users.rb
Created November 12, 2012 15:38
:counter_cache finally working
# db/migrate/20121112152734_create_users.rb
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.integer :groups_count, :default => 0
t.timestamps
end
end
@roychri
roychri / paths.rb
Created November 30, 2012 18:50
dynamic path helpers with an ActiveRecord object.
I have a controller/action with a helper path like this :
new_controller_path(:foo => {:record_type => 'Group', :record_id => group.id}) # This returns /foo/new?foo[record_type]=Group&foo[record_id]=123 which is what I want.
# I was hoping one of these two would return the SAME THING as above.
new_controller_path(Group.new(:record => group)) # This returns /foo/new but I want same thing as above.
new_controller_path(:foo => Group.new(:record => group)) # This returns /foo/new but I want same thing as above.
# My ultimate goal was to reduce the length of the helper line a bit.
# While on master which is up to date.
git checkout -b feature9
# <work>, <commit>, <work>, <commit>, ...
git fetch origin/master
git rebase master
git checkout master
git pull
git rebase feature9
# Oopps, I wanted to do -i
git reglog
+------------+-------------+------------+-------------+------------+
| date | video_views | clip_views | video_plays | clip_plays |
+------------+-------------+------------+-------------+------------+
| 2016-09-21 | 9166 | 2237 | 2342 | 1622 |
| 2016-09-22 | 63967 | 20235 | 23900 | 15616 |
| 2016-09-23 | 184224 | 69347 | 103441 | 51579 |
| 2016-09-24 | 62307 | 17288 | 25074 | 12300 |
| 2016-09-25 | 27367 | 7250 | 6499 | 5060 |
| 2016-09-26 | 51912 | 13901 | 12038 | 10471 |
| 2016-09-27 | 61551 | 19965 | 18761 | 15030 |
@roychri
roychri / README.md
Last active December 2, 2016 03:51
Temporary after_destroy rails3 callback using Observers

WARNING

Heavy metaprogramming ahead...

Summary

This is an attempt/experiment at being able to temporarily observe an active record through callbacks.

To Do

@roychri
roychri / README.md
Created February 13, 2018 03:34
Promise based repeater that wont start the loop unless the callback completed

This is an implementation similar to setInternal() but will not start the delay/loop again until the callback has finished (presuming it returned a promise). Im interested to know if there's a better way to do this. I choose this rather than using setInterval() because I wanted to make sure it would not start over if the callback took longer than the interval time.

The callback function will only be called once an initial delay has passed. It would be simple to add a call to cb() before calling the loop.

Let me know what you think!