Skip to content

Instantly share code, notes, and snippets.

View supremebeing7's full-sized avatar
💭
I may be slow to respond.

Mark J. Lehman supremebeing7

💭
I may be slow to respond.
View GitHub Profile
#Deploy and rollback on Heroku in staging and production
class RakeHerokuDeployer
def initialize app_env
@app = ENV["#{app_env.to_s.upcase}_APP"]
end
def run_migrations
push; turn_app_off; migrate; restart; turn_app_on; tag;
end
Reverse a string
this can be an easy one
no using ‘reverse’ :)
Word Count
count words in a block of text
count how many times each word appears
display a list of all unique words
Sports Statistics
@supremebeing7
supremebeing7 / UserMailer.rb
Last active August 29, 2015 14:06
Refactored mailer methods
# This is an exercise in refactoring. The attempt is to make the code more DRY (Don't Repeat Yourself) while
# maintaining or increasing readability. While it's certainly more DRY, readability seems to have suffered.
# Therefore, this may be a case where less DRY code is ultimately better for the project and future devs.
def report_email(user, report, template_name)
@user = user
@report = report
mail(to: @user.email,
from: "\"Company Reports\" reports@example.com",
subject: "#{template_name.chomp('_email').humanize} for #{@user.account.name}",
@supremebeing7
supremebeing7 / report.rake
Created September 6, 2014 04:37
Rake task for sending scheduled reports by day, week and month
# This task is written so that it can be run daily, but will only send weekly and monthly reports at the correct time intervals (i.e. once a week or once a month)
namespace :report do
task :all => :environment do
report_for_all_users('daily_report', 'day')
if Time.now.monday?
report_for_all_users('weekly_report', 'week')
end
if Time.now.day == 1
report_for_all_users('monthly_report', 'month')
@supremebeing7
supremebeing7 / user.rb
Last active August 29, 2015 14:06
Email Chunking for SMTP API
# This method separates emails into 1000 email chunks for bulk sending
# through an SMTP API such as SendGrid API
def self.chunk_emails(recipients)
if recipients.count < 1000
[recipients]
else
split_recipients = recipients.count.divmod(1000)
# Example, if recipients.count == 3200, then ...
quotient = split_recipients[0] # => 3
@supremebeing7
supremebeing7 / ruby_try_example.rb
Last active August 29, 2015 14:06
Get rid of extraneous conditionals
# If @user does not exist or is nil...
@user.name
# => throws an exception, "undefined method 'name' for NilClass"
# The easy fix is to solve with a conditional:
@user.name if @user
# The more fun (and probably dirtier) way is to use the #try method:
@user.try(:name)
@supremebeing7
supremebeing7 / report.rake
Created September 10, 2014 18:16
Rake task testing with Rspec
namespace :report do
task :daily => :environment do
report_for_all_users('daily_report', 'day')
end
def report_for_all_users(flag, time_period)
puts "Getting users who want #{flag.to_s.humanize}s"
users = User.where("admin = true AND settings -> '#{flag}' = '1'")
puts "Sending #{flag.to_s.humanize}s for #{users.count} users"
users.each do |user|
@supremebeing7
supremebeing7 / Preferences.sublime-settings
Created March 18, 2015 17:54
SublimeText2 Preferences
// While you can edit this file, it's best to put your changes in
// "User/Preferences.sublime-settings", which overrides the settings in here.
//
// Settings may also be placed in file type specific options files, for
// example, in Packages/Python/Python.sublime-settings for python files.
{
// Sets the colors used within the text area
"color_scheme": "Packages/Color Scheme - Default/Monokai.tmTheme",
// Note that the font_face and font_size are overriden in the platform
# config/locales/en.yml
en:
exception:
show:
not_found:
title: "Not Found"
description: "The page you were looking for does not exists."
internal_server_error:
title: "Internal Server Error"
@supremebeing7
supremebeing7 / .bash_profile
Created December 9, 2015 18:33
git aliases
alias gs='git status '
alias ga='git add '
alias gb='git branch '
alias gc='git commit'
alias gcm='git commit -m'
alias gd='git diff'
alias gco='git checkout '
alias gcob='git checkout -b '
alias got='git '