Skip to content

Instantly share code, notes, and snippets.

View bernardobarreto's full-sized avatar
🚴‍♂️

Bernardo bernardobarreto

🚴‍♂️
  • 17:01 (UTC -03:00)
View GitHub Profile
@bernardobarreto
bernardobarreto / termux.sh
Created May 19, 2018 23:34 — forked from paresharma/termux.sh
Rails on Termux
apt update && apt upgrade
apt install ruby vim git nodejs
apt install ruby-dev libxml2-dev libxslt-dev pkg-config make clang
gem install nokogiri -- --use-system-libraries
apt install libsqlite-dev
gem install sqlite3
@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
@bernardobarreto
bernardobarreto / application_controller.rb
Created November 20, 2017 10:25
bad practice application_controller example
class ApplicationController < ActionController::Base
before_action :increment_visits, only: [:index, :show, :new, :edit]
private
def increment_visits
current_user.inc(visits: 1) # Mongoid ODM example
end
end
@bernardobarreto
bernardobarreto / mongoid.yml
Last active November 20, 2017 09:55
Mongoid: two databases yml sharded configuration
production:
sessions:
default:
database: db_development
username: my_username
password: my_password
hosts:
- myserverip:27017
secondary:
database: db2_development
@bernardobarreto
bernardobarreto / url.rb
Created November 20, 2017 09:33
mongodb model using secondary client and database with sharding example
class Url
include Mongoid::Document
store_in database: 'db2_development', client: 'secondary'
shard_key :id
end
@bernardobarreto
bernardobarreto / url.rb
Created November 20, 2017 09:32
mongodb model using secondary client and database example
class Url
include Mongoid::Document
store_in database: 'db2_development', client: 'secondary'
end