Skip to content

Instantly share code, notes, and snippets.

View cybersamx's full-sized avatar

Samuel Chow cybersamx

  • Los Angeles, Ca
  • 23:43 (UTC -07:00)
View GitHub Profile
@cybersamx
cybersamx / update_without_touch.rb
Last active August 29, 2015 14:05
Update Rails ActiveRecord object without triggering validation and updating updated_at
object = MyModel.create(name: 'Sam')
# update_attribute bypass validation but will touch update_at
object.update_attribute(:name, 'Yo')
object.name == 'Yo' # true
object.name_changed? # false
# update_column bypass validation but will not touch update_at
object.update_column(:name, 'Yo')
object.name == 'Yo' # true
@cybersamx
cybersamx / find_remove_old_activerecord.rb
Last active June 10, 2021 16:34
Find/Remove ActiveRecord older than n days
# Get objects older than 3 days.
@models = MyModel.where('created_at < :time', { time: 3.days.ago })
@models = MyModel.where(['created_at < ?', 3.days.ago])
# Get objects within the last 3 days.
@models = MyModel.where('created_at >= :time', { time: 3.days.ago })
@models = MyModel.where(['created_at >= ?', 3.days.ago])
# Multiple conditions.
@models = MyModel.where(['created_at >= ? and name like ?', 3.days.ago, 'Sam%'])
@cybersamx
cybersamx / git_primer.sh
Last active August 29, 2015 14:04
Git Primer
# Find the remote origin
git remote show origin
git config --get remote.origin.url
# Describe the git repo (only works if there're tags added to the repo)
# Output: [nearest_tag_name]-[number_of_commits]-g[commit_identifier]
git describe
# List of tags
git tag # List of local tags
@cybersamx
cybersamx / postgres_sys_commands.sql
Last active August 29, 2015 14:04
PostgreSQL System Commands
-- List of all databases
\list
-- Connect to database
\c MyDatabase
-- List of tables and relations
\dt
-- Schema of a table
@cybersamx
cybersamx / InjectAngularJSFilterToJasmine.js
Created August 6, 2014 18:28
Inject a custom AngularJS filter object to a (Jasmine) unit test spec.
// Simple custom filer
var filters = angular.module('myNameSpace.filters', []);
filters.filter('reverse', function() {
return function(text) {
return text.split('').reverse().join('');
};
});
// 2 ways to test
@cybersamx
cybersamx / InjectAngularJSDirectiveToJasmine.js
Created August 6, 2014 18:26
Inject a custom AngularJS directive object to a (Jasmine) unit test spec.
// Simple AngularJS directive
var directives = angular.module('myNameSpace.directives', []);
directives.directive('directive', function() {
var directive = {};
directive.restrict = 'E'; // Indicates an element directive.
directive.template = 'Hello World';
return directive;
@cybersamx
cybersamx / InjectAngularJSServiceToJasmine.js
Last active August 29, 2015 14:04
Inject a custom AngularJS service object to a (Jasmine) unit test spec.
// 2 ways to inject custom service to a Jasmine test spec.
// Method 1
describe('myNameSpace.services', function() {
describe('MyService', function() {
var createService;
beforeEach(inject(function() {
var $injector = angular.injector(['myNameSpace.services'])
@cybersamx
cybersamx / InjectAngularJSControllerToJasmine.js
Created August 6, 2014 18:22
Inject a custom AngularJS controller object to a (Jasmine) unit test spec.
// 3 ways to inject custom AngularJS controller to a Jasmine test spec.
// Method 1
describe('myNameSpace.controllers', function() {
describe('MyController', function() {
var scope, createController;
beforeEach(module('myNameSpace.controllers'));
beforeEach(inject(function($injector) {
@cybersamx
cybersamx / NSDateFormatter_DateFormatSpecifiers.m
Last active August 29, 2015 14:04
NSDateFormatter date format specifiers
[dateFormatter setDateFormat:@"E, d M y"]; // Output: Sun, 1 5 2011
[dateFormatter setDateFormat:@"EE, dd MM yy"]; // Output: Sun, 01 05 11
[dateFormatter setDateFormat:@"EEE, ddd MMM yyy"]; // Output: Sun, 001 May 2011
[dateFormatter setDateFormat:@"EEEE, dddd MMMM yyyy"]; // Output: Sunday, 0001 May 2011
[dateFormatter setDateFormat:@"EEEEE, ddddd MMMMM yyyyy"]; // Output: S, 00001 M 2011
// Common date format specifiers
//
// y = year
// Q = quarter
@cybersamx
cybersamx / CoordinatesOfSubView.m
Created August 6, 2014 17:58
To find the coordinate of subview in the view controller view coordinate system.
// Create nested subviews. self.view > subview1 > subview2
UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20, 300, 300);
[self.view addSubview:subview1];
UIView *subview2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30, 200, 200);
[superview addSubview:subview2];
// To find the coordinate of subview in the view controller view coordinate system.
CGPoint point = [subview1 convertPoint:subview2.frame.origin toView:self.view];