Skip to content

Instantly share code, notes, and snippets.

View lcguida's full-sized avatar
🏠

Leandro Guida lcguida

🏠
View GitHub Profile
@lcguida
lcguida / pre-push
Last active August 4, 2017 14:21 — forked from calebhaye/pre-push
Run tests before git push (within specified branches)
#!/bin/bash
branches_to_test=('master' 'staging' 'develop')
branch=`git rev-parse --abbrev-ref HEAD`
test_branch () {
local e
for e in "${branches_to_test[@]}"
do
@lcguida
lcguida / active_admin.scss
Last active June 27, 2017 08:29 — forked from seanlinsley/active_admin.scss
Make Active Admin's primary color change between dev/staging/production
// SASS variable overrides must be declared before loading up Active Admin's styles.
//
// To view the variables that Active Admin provides, take a look at
// `app/assets/stylesheets/active_admin/mixins/_variables.css.scss` in the
// Active Admin source.
//
// For example, to change the sidebar width:
// $sidebar-width: 242px;
$primary-color: dynamic_active_admin_color();
@lcguida
lcguida / example.sql
Created December 12, 2016 11:30
Running SQL Examaple
-- Dudu query:
select part_id,produced_at
from (select part_id,produced_at
,dense_rank () over (partition by part_id order by produced_at) as dr
from part_subhourly_data
where produced_at < now() - interval '100 days'
) p
@lcguida
lcguida / sendmail_staging.rb
Last active January 27, 2017 13:03
Custom delivery method to replace recipient on sendmail for a staging environment
# lib/utils/sendmail_staging.rb
class SendmailStaging < ::Mail::Sendmail
# mail object can be used to modify mail before we pass it
# to sendmail
def deliver!(mail)
mail['to'] = 'yourmail@yourcompany.com' #replace recipient
super(mail) #Let Sendmail do his work
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
@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 / 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 / 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 / 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 / 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