$ rails g model User
belongs_to
has_one
# See http://jordan.broughs.net/archives/2012/07/enumerablecount_by-for-ruby | |
# Ruby >= 1.8.7 (incl 1.9.x) | |
module Enumerable | |
def count_by(&block) | |
Hash[group_by(&block).map { |key,vals| [key, vals.size] }] | |
end | |
end |
t = 236 # seconds | |
Time.at(t).utc.strftime("%H:%M:%S") | |
=> "00:03:56" | |
# Reference | |
# http://stackoverflow.com/questions/3963930/ruby-rails-how-to-convert-seconds-to-time |
# Question: | |
# Suppose you have an array of 99 numbers. | |
# The array contains the digits 1 to 100 with one digit missing. | |
# Write four different algorithms to compute the missing number. | |
# Two of these should optimize for low storage and two of these should optimize for fast processing. | |
# (Keep in mind, there are no duplicates and the array is not already sorted.) | |
####################################################### |
# Generate SHA256 hash | |
require 'digest' | |
Digest::SHA256.hexdigest "your_string" | |
# Generate UUID | |
SecureRandom.uuid.split('-').join |
class PolicyTest < ActiveSupport::TestCase | |
def assert_permissions(current_user, record, available_actions, permissions_hash = {}) | |
permissions_hash.each do |action, should_be_permitted| | |
if should_be_permitted | |
assert_permit current_user, record, action | |
else | |
refute_permit current_user, record, action | |
end | |
end |
HTTP status code symbols for Rails | |
Thanks to Cody Fauser for this list of HTTP responce codes and their Ruby on Rails symbol mappings. | |
Status Code Symbol | |
1xx Informational | |
100 :continue | |
101 :switching_protocols | |
102 :processing |
// Reference: http://lea.verou.me/2011/10/easily-keep-gh-pages-in-sync-with-master/ | |
$ git add . | |
$ git status // to see what changes are going to be commited | |
$ git commit -m 'Some descriptive commit message' | |
$ git push origin master | |
$ git checkout gh-pages // go to the gh-pages branch | |
$ git rebase master // bring gh-pages up to date with master | |
$ git push origin gh-pages // commit the changes |
At some point you’ll find yourself in a situation where you need edit a commit message. That commit might already be pushed or not, be the most recent or burried below 10 other commits, but fear not, git has your back 🙂.
git commit --amend
This will open your $EDITOR
and let you change the message. Continue with your usual git push origin master
.