Skip to content

Instantly share code, notes, and snippets.

View thescubageek's full-sized avatar
🤿
Are you for scuba?

Steve Craig thescubageek

🤿
Are you for scuba?
  • @evidation
  • Wilmington, NC
View GitHub Profile
@thescubageek
thescubageek / sort_hub_clients_by_location_count.rb
Last active August 23, 2017 17:47
HUB: Sort Clients by Location Count
RISK_FACTORS = {
"Senior-Living": 32,
"Apartments": 15,
"Self-Storage": 20,
"Veterinarian": 9
}.freeze
def clients_by_vertical_and_location_count(vertical, g5_internal=false)
Client.where(vertical: vertical)
.where(g5_internal: g5_internal)
@thescubageek
thescubageek / active_record_in_order.rb
Created October 4, 2017 19:20
WIP: Find and return ActiveRecord models by attribute according to specific order
Widget.where(id: child_widget_ids).index_by(&:id).values_at(*child_widget_ids).squish
@thescubageek
thescubageek / steem_post_finder.rb
Last active February 28, 2018 16:28
SteemPostFinder
require 'radiator'
class SteemPostFinder
# the max results we can return for a single API query
BATCH_SIZE = 100
def initialize(tag_name)
@tag_name = tag_name
end
@thescubageek
thescubageek / steem_percentage.rb
Last active March 7, 2018 04:07
steem_percentage.rb
module SteemPercentage
extend ActiveSupport::Concern
STEEM_MAX_PERCENT = 10000.0
# converts to [0.0-1.0] => percent_decimal(8000) == 0.8
def percent_decimal(num)
num.to_f / STEEM_MAX_PERCENT
end
@thescubageek
thescubageek / steem_post_broadcaster.rb
Last active March 22, 2018 22:07
SteemPostBroadcaster
class SteemPostBroadcaster
attr_accessor :options
def initialize(opts={})
@options = opts
end
def post!
raise "Post is missing title, body, or tags" unless is_valid_post?
account.post!(post_options) unless ENV['DISABLE_AUTO_POSTING'].to_boolean
@thescubageek
thescubageek / bulk_insert_assignment.rb
Created October 22, 2018 16:16
Bulk assign punch item assignments
project_id = 69 # replace with project id
login_ids = [332, 331, 133, 267, 119, 249, 251, 250] # replace with assignee login ids
batch_size = 300 # replace with batch size
pi_ids = PunchItem.where(project_id: project_id).where("deleted_at IS NULL").pluck(:id)
already_used_ids = PunchItemAssignment.where(punch_item_id: pi_ids).pluck(:punch_item_id)
pi_ids = (pi_ids - already_used_ids).shuffle
idx = 0
PunchItem.includes(:punch_item_assignments).where(id: pi_ids).find_each(batch_size: batch_size) do |pi|
@thescubageek
thescubageek / long_division.rb
Created July 9, 2019 17:30
Long Division Coding Challenge
### BEST SOLUTION I HAVE SO FAR (unrealistic to acheive on first try)
# SUPER DRY'd up way of testing both techniques
def divide(x, y, technique='multiply')
sign = (x < 0 && y > 0 || x > 0 && y < 0) ? -1 : 1
x, y = x.abs, y.abs
return 0 if x == 0 || y == 0 || y > x
return sign * 1 if x == y
i = 1