Skip to content

Instantly share code, notes, and snippets.

View virolea's full-sized avatar

Vincent Rolea virolea

View GitHub Profile
@virolea
virolea / flatten.rb
Created April 28, 2016 16:19
Flatten an array of nest arrays
def flatten(array)
return "Input must be an array" if !array.kind_of?(Array)
array.reduce([]) do |res, el|
Array === el ? res + flatten(el) : res << el
end
end
@virolea
virolea / customer.rb
Created April 28, 2016 16:21
Invite people from 100km of headquarters - from text file
class Customer
include Math
EARTH_RADIUS = 6371 # Earth Radius in km
attr_accessor :id, :name, :latitude, :longitude
def initialize(params)
@id = params["user_id"]
@name = params["name"]
@virolea
virolea / capybara_cheat_sheet.rb
Last active March 8, 2017 11:26
Capybara Cheat Sheet
# Navigating
visit('/projects')
visit(post_comments_path(post))
expect(page).to have_current_path(post_comments_path(post))
# Clicking links and buttons
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click_on('Link Text') # clicks on either links or buttons
@virolea
virolea / upload.js
Last active July 9, 2024 02:29
Tracking file upload progress using axios
upload(files) {
const config = {
onUploadProgress: function(progressEvent) {
var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
console.log(percentCompleted)
}
}
let data = new FormData()
data.append('file', files[0])
@virolea
virolea / settings.json
Created January 17, 2018 10:36
VScode config file - iTerm - Atom keybindings - Editor settings
{
"atomKeymap.promptV3Features": true,
"editor.multiCursorModifier": "ctrlCmd",
"editor.formatOnPaste": true,
"files.insertFinalNewline": true,
"files.trimTrailingWhitespace": true,
"editor.renderWhitespace": "all",
"editor.tabSize": 2,
"explorer.confirmDragAndDrop": false,
"terminal.external.osxExec": "iTerm.app",
@virolea
virolea / config.yml
Created April 10, 2018 10:02
Circleci 2.0 config.yml - Rails - Postgresql - Redis
# Ruby CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
#
version: 2
jobs:
build:
docker:
# specify the version you desire here
- image: circleci/ruby:2.4.1-node-browsers
@virolea
virolea / basic_auth.rb
Last active July 28, 2020 16:41
HTTP Basic Auth for Rack applications - Protect Rails app staging environment with HTTP Basic Auth
MyApp::Application.configure do
# Restrict access with HTTP Basic Auth for staging environment
if ENV['STAGING_AUTH']
config.middleware.use Rack::Auth::Basic do |username, password|
ENV['STAGING_AUTH'].split(':') == [username, password]
end
end
end
@virolea
virolea / index.js
Created October 23, 2019 09:40
Configure axios to automatically set the Rails CSRF token
import axios from 'axios'
const tokenEl = document.getElementsByName('csrf-token')[0]
if (tokenEl) {
const token = tokenEl.getAttribute('content')
axios.defaults.headers.common['X-CSRF-Token'] = token
}
axios.defaults.headers.common['Accept'] = 'application/json'
axios.defaults.headers.post['Content-Type'] = 'application/json'
# config/routes.rb
resources :documents do
scope module: 'documents' do
resources :versions do
post :restore, on: :member
end
resource :lock
end
end
@virolea
virolea / input_error.rb
Created January 21, 2022 10:16
Custom input field error for Rails forms
ActionView::Base.field_error_proc = proc do |html_tag, instance_tag|
fragment = Nokogiri::HTML.fragment(html_tag)
field = fragment.at("input,select,textarea")
html = if field
field["class"] = "#{field["class"]} is-invalid"
html = <<-HTML
#{fragment}
<p class="invalid-feedback">#{instance_tag.error_message.to_sentence}</p>
HTML