Skip to content

Instantly share code, notes, and snippets.

View acuppy's full-sized avatar

Adam Cuppy acuppy

View GitHub Profile
@acuppy
acuppy / document_types.rb
Created July 21, 2013 04:53
All major document mime types (Microsoft Office, Apple iWork, Adobe PDF) as a Ruby Module
module DocumentFileTypes
module Microsoft
WORD = %w(
application/msword
application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/vnd.openxmlformats-officedocument.wordprocessingml.template
application/vnd.ms-word.document.macroEnabled.12
application/vnd.ms-word.template.macroEnabled.12
)
# To prevent the vulnerable server from running on your machine
# (this does not impact Zoom functionality), run these two lines in your Terminal.
pkill "ZoomOpener"; rm -rf ~/.zoomus; touch ~/.zoomus && chmod 000 ~/.zoomus;
pkill "RingCentralOpener"; rm -rf ~/.ringcentralopener; touch ~/.ringcentralopener && chmod 000 ~/.ringcentralopener;
# (You may need to run these lines for each user on your machine.)
@acuppy
acuppy / 20141027-active-record-left-outer-joins.md
Last active December 18, 2018 03:53
Using SQL JOINS to Retrieve ActiveRecord Objects Without Associated Records

Scenario: you're pulling all User records that do not have any associated Activities. In Rails you may find yourself doing something like this...

User.all.select { |user| user.activities.blank? }

Pretty simple to implement, but a performance hog: Rails will load a new ActiveRecord Object for each User, then call for all activities related to the user; simply to determine, which do and don't have any Activites: bad idea.

We can achieve the same result with a fraction of the overhead, using LEFT OUTER JOIN.

class A
def foo
:bar
end
end
a = A.new
puts a.foo # => :bar
@acuppy
acuppy / rake_logger_example.rb
Created July 26, 2016 01:04
An example of how to tap into Rake and run a log line prior to every Rake Task run.
# lib/rake/logging.rb
require 'rake'
module Rake
module Logging
def invoke_task *args
puts "Running: #{parse_task_string args.first}" # ...or do whatever
super
end
class AddFirstNameAndLastNameToUsers < Arborist::Migration
model :User
data do
model.find_each do |user|
first_name, last_name = user.full_name.split ' '
user.first_name = first_name
user.last_name = last_name
user.save!
namespace :data do
desc "Split the user's fullname into first and last names"
task :split_fullname => :environment do
User.reset_column_information
User.find_each do |user|
first_name, last_name = user.full_name.split ''
user.first_name = first_name
user.last_name = last_name
user.save!
class AddCategoriesToPosts < ActiveRecord::Migration
# Fill the void
class ::Category < ActiveRecord::Base
end unless defined? ::Category
def change
Post.find_each do |p|
p.categories.create
end
class AddAdminToUsers < ActiveRecord::Migration
# Fill the void
class User < ActiveRecord::Base
end unless defined? ::User
def change
add_column :users, :admin, :boolean, default: false
# convert existing users to admin
class AddAdminToUsers < ActiveRecord::Migration
def change
add_column :users, :admin, :boolean, default: false
# convert existing users to admin
User.reset_column_information
User.find_each do |u|
u.update admin: true
end
end