Skip to content

Instantly share code, notes, and snippets.

View DonSchado's full-sized avatar
🐢
null

Marco DonSchado

🐢
null
View GitHub Profile
@DonSchado
DonSchado / layer-css
Created November 23, 2014 07:00
Different depth of nodes will use different colour allowing you to see the size of each element on the page, their margin and their padding. Now you can easily identify inconsistencies.
* { background-color: rgba(255,0,0,.2); }
* * { background-color: rgba(0,255,0,.2); }
* * * { background-color: rgba(0,0,255,.2); }
* * * * { background-color: rgba(255,0,255,.2); }
* * * * * { background-color: rgba(0,255,255,.2); }
* * * * * * { background-color: rgba(255,255,0,.2); }
@DonSchado
DonSchado / render_or_redirect.rb
Created June 8, 2014 19:11
an experiment for open discussion, to eliminate duplicate controller logic
# eliminate all the duplication
def render_or_redirect(_action, _path, &block)
if block.call
redirect_to _path, success: I18n.t('common.success')
else
flash.now[:alert] = I18n.t('common.error')
render _action
end
end
@DonSchado
DonSchado / example_spec.rb
Last active December 22, 2015 20:18
Thoughtbot retracted their initial implementation of strong parameters matchers in v2.0.0 of shoulda-matchers, so we decided to build our own until new official ones are released. The following is a small matcher for testing what params should be permitted in controllers. The matcher's syntax is based on validation matchers. If you're not follow…
# assuming your subject is the UsersController with a method user_params
describe UsersController do
describe "params" do
# per default the matcher extracts the subject and params method
it { should permit_params(:email, :name, :role) }
# to overwrite the params method use explicit .params_method()
it { should permit_params(:first_name, :last_name).params_method(:other_user_params) }
@DonSchado
DonSchado / gist:3499614
Created August 28, 2012 16:10 — forked from brumm/gist:3437594
sublime shortcuts
# expand selection to word, create new selection
cmd + d
# skip over a match
cmd + k, d
# expand selection to all matches
cmd + ctrl + g
@DonSchado
DonSchado / card_deck.rb
Created June 29, 2012 17:22
A deck of cards in ruby
suits = %w{s h d c}
ranks = %w{2 3 4 5 6 7 8 9 T J Q K A}
deck = suits.product(ranks)
def pull_card
deck.sample
end
@DonSchado
DonSchado / protocol_adapter.rb
Created January 8, 2012 14:04
Adapter method that pulls the destination protocol off of the message (...) and add a new adapter
def adapter_for(message)
protocol = message.to.scheme.downcase
adapter_name = "#{protocol.capitalize}Adapter"
adapter_class = self.class.const_get(adapter_name)
adapter_class.new
end
@DonSchado
DonSchado / clickable-button-group.js
Created November 19, 2011 17:39
When a button is clicked, the selected class is removed from all other buttons and will be added it to the one clicked.
$('div.button a.available').click(function(e){
e.preventDefault();
$('a').removeClass('selected');
$(this).addClass('selected');
});
@DonSchado
DonSchado / Three_State_Button.js
Created November 19, 2011 11:08
Add a click event listener on each of the button links. When clicked each toggle the class 'selected'. event.preventDefault stops the browser from following the link
$(document).ready(function(){
$('a.button').click(
function(event){
event.preventDefault();
$(this).toggleClass('selected');
});
});
@DonSchado
DonSchado / sort_stings.rb
Created October 26, 2011 18:05
Given a string "Sort words in a sentence", it should return "a in Sort words sentence".
def sort_string(string)
string.split(' ').sort{|x, y| x.length <=> y.length}.join(' ')
end
@DonSchado
DonSchado / find_and_count_words.rb
Created October 26, 2011 17:48
Given a sentence containing multiple words, find the frequency of a given word in that sentence.
def find_frequency(sentence, word)
sentence.downcase.split.count(word.downcase)
end