Skip to content

Instantly share code, notes, and snippets.

View thatrubylove's full-sized avatar

Jim OKelly thatrubylove

View GitHub Profile
#!ruby
# config/initializers/mysql_utf8mb4_fix.rb
require 'active_record/connection_adapters/abstract_mysql_adapter'
module ActiveRecord
module EmojiConnectionAdapters
refine AbstractMysqlAdapter do
NATIVE_DATABASE_TYPES[:string] = { name: "varchar", limit: 191 }
end
end
@thatrubylove
thatrubylove / rules.md
Created March 22, 2015 01:10
RubyLove Rails Rules
  1. Controller actions will not expose more than one instance variable
  2. Views are used in the context of a controller, they use instance variables
  3. Partials are meant to be sharable (multi controllers) so they should only use local variables
h = { a: nil, b: :c, c: nil }
result = h.keys.reduce([]) do |acc, k|
if h[k].nil?
acc << k unless acc.include? k
else
acc << h[k]
acc << k
end
acc
class Person
with_options if: :registration_complete? do |opts|
validates :zipcode, presence: true
validates :address1, presence:true
end
end
@thatrubylove
thatrubylove / keyword_analyzer.rb
Last active August 29, 2015 14:05
looking for words
module KeywordAnalyzer
extend self
def analyze(comments, positive_words, negative_words)
positives = scan_all(comments, positive_words)
negatives = scan_all(comments, negative_words)
{
positive: {
matches: positives,
@thatrubylove
thatrubylove / migration.rb
Created July 23, 2014 16:44
This is how you do quick inserts under a single connection
ids = Location.all.pluck(:id)
inserts = ids.map do |id|
ActiveRecord::Base.send(:sanitize_sql_array,
["insert into survey_requests (location_id) VALUES (?)", id])
end
ActiveRecord::Base.connection.execute(inserts.join(";"))
<entry>
<title>Papa John's</title>
<link rel="logo" type="image/jpg" href="https://print.entertainment.com/medias/sys_master/celum_assets/a2c/ebc/48c/0ac/8820935426078_C0000000000000D5.png" />
<category term="P" label="AlphabetIndex" scheme="http://api.entertainment.com/AtomServer3/feeds/offers?uuid=1401914342909&amp;location=75632&amp;distance=25&amp;category=25&amp;category=146" />
<category term="Dining" label="Category" scheme="http://api.entertainment.com/AtomServer3/feeds/offers?uuid=1401914342909&amp;location=75632&amp;distance=25&amp;category=25&amp;category=25" />
<category term="Fast Food" label="SubCategory" scheme="http://api.entertainment.com/AtomServer3/feeds/offers?uuid=1401914342909&amp;location=75632&amp;distance=25&amp;category=25&amp;category=38" />
<category term="Premium" label="OfferType" scheme="http://api.entertainment.com/AtomServer3/feeds/offers?uuid=1401914342909&amp;location=75632&amp;distance=25&amp;category=25&amp;category=470" />
<author>
<name>
@thatrubylove
thatrubylove / my_controller.rb
Created June 9, 2014 13:47
No if's in my controller thank you!
class SomeController < ApplicationController
put '/objects/:id' do |id|
object = Objectify.null_object(Object.find_by_id, id) #partial application method!
status object.status
object.update_attributes(params[:object])
json(object, errors: object.errors)
end
end
class SentinelObject
@thatrubylove
thatrubylove / approximation.rb
Created June 7, 2014 14:09
Approximation in Ruby
NUMBER_TO_GUESS = ARGV[0].to_i
BOUNDS = [1,1_000_000]
puts "The number to guess is #{NUMBER_TO_GUESS}"
module NumberGuesser
extend self
def guess(n1,n2)
(n1+n2)/2
end
@thatrubylove
thatrubylove / person.rb
Created June 4, 2014 11:02
intro-to-my-new-ruby-book-08
person.favorite_color = :green
person.save(some_database_save_ability)