This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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%']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- List of all databases | |
\list | |
-- Connect to database | |
\c MyDatabase | |
-- List of tables and relations | |
\dt | |
-- Schema of a table |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; |