Skip to content

Instantly share code, notes, and snippets.

View macedo's full-sized avatar

Rafael Macedo macedo

View GitHub Profile
@macedo
macedo / gist:3013447
Created June 28, 2012 19:49
RSpec matcher to test validations and error messages
RSpec::Matchers.define :have_errors_on do |attribute, *values|
@attribute = attribute
@values = values
@failed = []
match_for_should do |record|
@record = record
matches_against?(true)
end
@macedo
macedo / gist:3048586
Created July 4, 2012 17:53
RSpec matcher to test mass assignment
RSpec::Matchers.define :allow_assignment_for do |attribute|
@attribute = attribute
match_for_should do |record|
@record = record
matches_against?(true)
end
match_for_should_not do |record|
@record = record
@macedo
macedo / gist:3129234
Created July 17, 2012 12:51
RSpec matcher to test nested attributes
RSpec::Matchers.define :accept_nested_attributes_for do |association|
@association = association
match_for_should do |record|
@record = record
matches_against?(true)
end
match_for_should_not do |record|
@record = record
@macedo
macedo / gist:3129447
Created July 17, 2012 13:39
RSpec matcher to test associations
RSpec::Matchers.define :have_association do |association|
@association = association
match_for_should do |record|
@record = record
matches_against?(true)
end
match_for_should_not do |record|
@record = record
$base-width: 940px;
$tablet-width: 768px;
$mobile-portrait-width: 300px;
$mobile-landscape-width: 420px;
$num-columns: 16;
$margin: 25px;
@mixin clearfix {
@import "compass/css3";
.ribbon {
position: absolute;
top: 42px;
width: 200px;
padding: 1px 0;
background: #000;
color: #eee;
@include box-shadow(rgba(#000, 0.5) 0 0 10px);
def load_files_from(files=[])
postponed_files = []
files.each do |f|
begin
require f
rescue
postponed_files << f
end
end
load_files_from(postponed_files) unless postponed_files.empty?
@macedo
macedo / ar_monkey_patch.rb
Created October 1, 2012 13:12
Get model accessible attributes
class ActiveRecord::Base
def accessible_attributes
accessible_attributes = self.class.accessible_attributes.select do |attr|
!attr.blank?
end
attributes.slice(*accessible_attributes)
end
end
@macedo
macedo / http_verbs.rb
Created November 14, 2012 12:05
http verbs module
require 'net/http'
module HTTPVerbs
def get(url)
uri = URI.parse url
http_request = Net::HTTP::Get.new(uri.request_uri)
Net::HTTP.new(uri.host, uri.port).request(http_request)
end
end
@macedo
macedo / gist:4394072
Created December 28, 2012 02:56
Fill brazilian addresses from cep
$('.cep').blur(function() {
var cep = $(this).attr('value').replace(/-/, '');
$.ajax({
url : 'http://cep.correiocontrol.com.br/' + cep + '.json',
dataType: 'json',
success: function(data) {
console.log(data);
$('#address').attr('value', data.logradouro);
$('#neighborhood').attr('value', data.bairro);
$('#city').attr('value', data.localidade);