Skip to content

Instantly share code, notes, and snippets.

View lcguida's full-sized avatar
🏠

Leandro Guida lcguida

🏠
View GitHub Profile
#!/bin/bash
usage()
{
cat << EOF
usage: $0 options
This script set ownership for all table, sequence and views for a given database
Credit: Based on http://stackoverflow.com/a/2686185/305019 by Alex Soto
require 'rubygems'
require 'dm-core'
require 'dm-migrations'
require 'dm-timestamps'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, "mysql://root@localhost/db_development")
class Tester
include DataMapper::Resource
@lcguida
lcguida / gem_downloader.rb
Last active August 29, 2015 14:03
Downloads all the gems from a Gemfile.lock. Inspired by https://gist.github.com/flavio/1722530
require 'rubygems'
require 'bundler'
require 'fileutils'
TMP_DIR = "/tmp/gems"
#If directory exists, delete it and recreates.
FileUtils.rm_rf(TMP_DIR) if File.exists?(TMP_DIR)
FileUtils.mkdir TMP_DIR
@lcguida
lcguida / universities_to_yaml.rb
Last active August 29, 2015 14:22
Universities Worldwide to YAML (Converts all information from http://univ.cc to a YAML file)
require 'open-uri'
require 'nokogiri'
require 'yaml'
start = 1
INCREMENT = 50
has_more = true
File.open('universities.yml', 'w') do |file|
while has_more do
@lcguida
lcguida / pundit.rb
Created October 15, 2015 09:52
Creates a `grant_permission_to` matcher to test Pundit policies.
# spec/support/pundit.rb
# Creates a 'grant_permission_to' matcher to better test Pundit policies
# See: http://thunderboltlabs.com/blog/2013/03/27/testing-pundit-policies-with-rspec/
RSpec::Matchers.define :grant_permission_to do |action|
match do |policy|
policy.public_send("#{action}?")
end
@lcguida
lcguida / have_content_type_matcher.rb
Created October 23, 2015 15:33
Custom matcher to verify content type of a request
RSpec::Matchers.define :have_content_type do |content_type|
MIME_TYPES = { json: 'application/json' }
match do |response|
response_content_type = response.header['Content-Type']
response_content_type.split(";").include?(MIME_TYPES[content_type])
end
description do
@lcguida
lcguida / application_helper.rb
Created March 7, 2016 18:06
dl_for viewer helper
def dl_for(instance, attribute)
content = content_tag(:dt, class: "input-lg") do
instance.class.human_attribute_name(attribute)
end
content << content_tag(:dd, class: "input-lg") do
if block_given?
yield
else
@lcguida
lcguida / app_config.rb
Last active March 18, 2016 19:24
Dynamic Loading Settings
class AppConfig < ActiveRecord::Base
validates :parameter, uniqueness: true, presence: true
def self.get(parameter)
AppConfig.where(parameter: parameter).first.try(:value)
end
def self.set(parameter, value)
param = AppConfig.find_or_initialize_by(parameter: parameter)
@lcguida
lcguida / network_explorer_controller.rb
Created March 23, 2016 18:02
Network Explorer (Tree view with different models)
class NetworkExplorerController < ApplicationController
def index
end
def get_root_nodes
roots = NetworkExplorerService.roots(current_user)
respond_with(roots)
end
@lcguida
lcguida / collection_math.rb
Created July 28, 2016 08:21
Math helpers for collection
module CollectionMath
class << self
# Returns nil instead of 0 for an empty array
def sum_values(collection)
values = if block_given?
collection.map{ |item| yield item }.compact
else