Skip to content

Instantly share code, notes, and snippets.

View bernardobarreto's full-sized avatar

Bernardo bernardobarreto

  • Brazil
View GitHub Profile
# frozen_string_literal: true
# specify a directory to install
cask_args appdir: '/Applications'
brew 'bash'
brew 'bash-completion'
brew 'git'
brew 'hub'
brew 'rvm'
@bernardobarreto
bernardobarreto / mongoid_nested_embeds_many.rb
Created October 8, 2018 21:38 — forked from sporkd/mongoid_nested_embeds_many.rb
Explains how mongoid embeds_many is breaking when nesting more than 1 level deep
class Author
include Mongoid::Document
field :name, :type => String
embeds_many :posts
end
class Post
include Mongoid::Document
field :title, :type => String
embedded_in :author
# From bcrypt-ruby gem
require 'bcrypt'
class User
include DataMapper::Resource
attr_accessor :password, :password_confirmation
property :id, Serial
property :username, String, :required => true, :length => (2..32), :unique => true
property :password_hash, String, :length => 60
@bernardobarreto
bernardobarreto / gist:d90f63cac9c6b6c2afa6bd18f5fee4c8
Created September 9, 2018 00:10 — forked from victorwhy/gist:45bb5637cd3e7e879ace
How Sinatra routes PUT/PATCH and DELETE

HTML and Sinatra really only support the GET and the POST methods. In order to be able to use the PUT and DELETE methods in Sinatra, you kind of have to "trick" the form to go to the right place. Then you can name the routes the proper way - otherwise you can only really work with GET and POST.

I used the Craiglist Jr challenge for some examples. Let's look at a quick example of a POST form/method/route- in this case, we're creating a new Craigslist article:

POST form and corresponding route:

<form action="/article/new" method="post">
  --------------------------------
  YOUR FORM FIELDS HERE
@bernardobarreto
bernardobarreto / browser_testing_on_wsl.md
Created August 22, 2018 20:19 — forked from danwhitston/browser_testing_on_wsl.md
Browser testing for Ruby from within Windows Subsystem for Linux

This is a rough guide to setting up browser testing through Selenium on Windows Subsystem for Linux (WSL), aka Bash on Ubuntu on Windows. It assumes the following environment:

  • Windows 10, running WSL
  • A Ruby dev environment, running inside WSL
  • Code that we want to test using a web driver, in this case Selenium, with a Capybara and RSpec test framework

The coding project folders are stored in the main Windows filing hierarchy and accessed via dev/mnt, but that makes no real difference to development and testing other than making it possible to edit the code using a GUI based editor within Windows.

The problem with browser testing in WSL is that it relies on opening and controlling a web browser, and browsers don’t work on WSL at present as it deliberately doesn’t include X Windows or some other GUI manager - it’s meant to be command line after all. So while you can apt-get firefox, trying to actually run it isn’t going to work.

@bernardobarreto
bernardobarreto / searcheable.rb
Created December 23, 2017 19:30
2015 elasticsearch2 basic rails wrapper
module Searcheable
extend ActiveSupport::Concern
module ClassMethods
def pluck(*fields)
ElasticRelation.new(self).pluck(*fields)
end
def find_by(terms)
@bernardobarreto
bernardobarreto / visit.rb
Last active December 23, 2017 21:02
incrementable visits (right way)
class Visit
include Mongoid::Document
field :counter, type: Integer
def increment!
self.inc(counter: 1)
end
end
@bernardobarreto
bernardobarreto / visit.rb
Last active December 23, 2017 21:02
incrementable visits (wrong way)
class Visit
include Mongoid::Document
field :counter, type: Integer
def increment!
self.counter += 1
self.save!
end
end
@bernardobarreto
bernardobarreto / dashboard_controller.rb
Created November 20, 2017 10:38
controller using concern example
class DashboardController < ApplicationController
include IncrementableVisits
def index
end
end
@bernardobarreto
bernardobarreto / incrementable_visits.rb
Created November 20, 2017 10:32
rails controller concern example
module IncrementableVisits
extend ActiveSupport::Concern
included do
before_action :increment_visits, only: [:index, :show, :new, :edit]
end
def increment_visits
current_user.inc(visits: 1) # Mongoid ODM example
end