Skip to content

Instantly share code, notes, and snippets.

@eprothro
eprothro / cache.rake
Last active December 14, 2015 09:29
A simple rake file with tasks to: 1) clear the rails cache 2) give statistics for a memcache protocol store (e.g., Dalli)
namespace :cache do
task :clear => :environment do
puts "=> Clearning Rails cache..."
Rake::Task["cache:stats"].invoke
puts "\n `Rails.cache.clear` => " + Rails.cache.clear.to_s + "\n\n"
# manually reset the stats, as clearing the cache doesn't do so
Rails.cache.dalli.reset_stats if Rails.cache.try(:dalli).respond_to? :reset_stats
Rake::Task["cache:stats"].reenable
@eprothro
eprothro / gist:5065952
Last active December 14, 2015 09:38
not crap makes...

Importing images for models done with missing images for the following 76 models:

  • /hyundai/elantra.jpg
  • /hyundai/azera.jpg
  • /honda/goldwing.jpg
  • /nissan/maxima_3.5.jpg
  • /saturn/ion.jpg
  • /chevrolet/k_1500.jpg
  • /chevrolet/c150.jpg
  • /chevrolet/express.jpg
@eprothro
eprothro / octopus.rb
Last active December 16, 2015 04:08
Octopus initializer for use with master/slave horizontal DB scaling with a Rails application on the Heroku stack. See the wiki page for more info: https://github.com/tchandy/octopus/wiki/Replication-with-Rails-on-Heroku
module Octopus
def self.shards_in(group=nil)
config[Rails.env].try(:[], group.to_s).try(:keys)
end
def self.followers
shards_in(:followers)
end
class << self
alias_method :followers_in, :shards_in
alias_method :slaves_in, :shards_in
irb(main):001:0> module A
irb(main):002:1>   def sound_off
irb(main):003:2>     puts 'module a'
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0>
irb(main):007:0* module B
irb(main):008:1>   def sound_off
@eprothro
eprothro / Compass Retina Spriting.md
Last active December 27, 2015 10:09
Sass sprite generation with compass for retina and non-retina images (example code for a Rails 3 application).

Overview

The below Sass+Compass/Ruby can be used to:

  • Generate retina and non-retina sprite maps
  • Allow a single style class to provide retina and non-retina support, per image
  • Automatically generate those sprite style classes

Example markup for a retina supported menu button:

%a.icon-hamburger
def select2_select(option, opts={})
# same as foundation_select, but for select2
# boxes used throughout the staff app.
originating_select_name = opts[:from]
originating_select_id = find("select[name='#{originating_select_name}']")['id']
custom_select = find(".select2-container[id$='#{originating_select_id}']")
# click dropdown
custom_select.find("a.select2-choice").click
# click option with correct text
module ErrorsWithTypesExtensions
  attr_reader :types

  # Pass in the instance of the object that is using the errors object.
  #
  #   class Person
  #     def initialize
  #       @errors = ActiveModel::Errors.new(self)
  #     end

I love the concept and usability of an ActiveModel::Errors paradigm. However, I and others often find it frustrating to work with the implementation.

I'd love to see a proper ActiveModel::Error object, where the errors attribute of a model including ActiveModel::Errors was an Enumerator of these objects.

There are various implied benefits and added flexibility that could be introduced in a backwards compatible way later, and immedate benefits.

Immediate Benefits

The error type could be retained

# Serializing 1 string attribute, 1 overriden uuid.to_s attribute,
# and 1 empty array, 30 times.
# bmbm:
# user system total real
# serializer 0.010000 0.000000 0.010000 ( 0.002572)
# delegator 0.000000 0.000000 0.000000 ( 0.000424)
# ips:
# delegator: 3600.6 i/s
# serializer: 427.6 i/s - 8.42x slower
class Test
  
  def foo(value=default_value)
    value
  end

  def default_value
    'default'
 end