Skip to content

Instantly share code, notes, and snippets.

@Killavus
Killavus / ugly_code_refactoring.coffee
Last active March 14, 2022 17:52
Example of possible refactoring of badly written code - loading photos and making it grayed after click
Photos = {}
class Photos.App
constructor: ->
@gui = new Photos.Gui($("#photos-list"))
@backend = new Photos.Backend()
start: =>
@backend.fetchPhotos()
.done(
@timknight
timknight / breakpoint.scss
Created May 5, 2014 12:48
A simple responsive breakpoint mixin that takes both attribute names and custom widths. See https://medium.com/p/889927b37740/
@mixin breakpoint($min: 0, $max: 0) {
$type: type-of($min);
@if $type == string {
@if $min == xs {
@media (max-width: 767px) { @content; } // Mobile Devices
}
@else if $min == sm {
@media (min-width: 768px) { @content; } // Tablet Devices
# config/routes.rb
resources :documents do
scope module: 'documents' do
resources :versions do
post :restore, on: :member
end
resource :lock
end
end
@adamjohnson
adamjohnson / publickey-git-error.markdown
Last active May 10, 2024 10:00
Fix "Permission denied (publickey)" error when pushing with Git

"Help, I keep getting a 'Permission Denied (publickey)' error when I push!"

This means, on your local machine, you haven't made any SSH keys. Not to worry. Here's how to fix:

  1. Open git bash (Use the Windows search. To find it, type "git bash") or the Mac Terminal. Pro Tip: You can use any *nix based command prompt (but not the default Windows Command Prompt!)
  2. Type cd ~/.ssh. This will take you to the root directory for Git (Likely C:\Users\[YOUR-USER-NAME]\.ssh\ on Windows)
  3. Within the .ssh folder, there should be these two files: id_rsa and id_rsa.pub. These are the files that tell your computer how to communicate with GitHub, BitBucket, or any other Git based service. Type ls to see a directory listing. If those two files don't show up, proceed to the next step. NOTE: Your SSH keys must be named id_rsa and id_rsa.pub in order for Git, GitHub, and BitBucket to recognize them by default.
  4. To create the SSH keys, type ssh-keygen -t rsa -C "your_email@example.com". Th
@rdetert
rdetert / dragonfly.rb
Created May 8, 2013 04:07
How to get Dragonfly working on with a Mongo Datastore using Mongoid 3 on both localhost and Heroku. It's a little bit of a pain and here is the quick and dirty approach without Monkey Patching.
require 'dragonfly'
app = Dragonfly[:images]
mconfig = Mongoid.load!("config/mongoid.yml", Rails.env)
app.datastore = Dragonfly::DataStorage::MongoDataStore.new
app.datastore.configure do |c|
cfg = mconfig['sessions']['default']
class ActionDispatch::Routing::Mapper
def draw(routes_name)
instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
end
end
BCX::Application.routes.draw do
draw :api
draw :account
draw :session
@edouard
edouard / dominant_colors.rb
Created February 10, 2012 08:53
A ruby script to get the most dominant colours in an image (uses ImageMagick)
require 'RMagick'
TOP_N = 10 # Number of swatches
# Create a 1-row image that has a column for every color in the quantized
# image. The columns are sorted decreasing frequency of appearance in the
# quantized image.
def sort_by_decreasing_frequency(img)
hist = img.color_histogram
# sort by decreasing frequency
@peteboere
peteboere / jquery.alterclass.js
Created December 24, 2011 12:49
jQuery alterClass plugin: Remove element classes with wildcard matching. Optionally add classes.
/**
* jQuery alterClass plugin
*
* Remove element classes with wildcard matching. Optionally add classes:
* $( '#foo' ).alterClass( 'foo-* bar-*', 'foobar' )
*
* Copyright (c) 2011 Pete Boere (the-echoplex.net)
* Free under terms of the MIT license: http://www.opensource.org/licenses/mit-license.php
*
*/
@cdmwebs
cdmwebs / friendly_urls.markdown
Created September 11, 2011 15:50 — forked from jcasimir/friendly_urls.markdown
Friendly URLs in Rails

Friendly URLs

By default, Rails applications build URLs based on the primary key -- the id column from the database. Imagine we have a Person model and associated controller. We have a person record for Bob Martin that has id number 6. The URL for his show page would be:

/people/6

But, for aesthetic or SEO purposes, we want Bob's name in the URL. The last segment, the 6 here, is called the "slug". Let's look at a few ways to implement better slugs.