Skip to content

Instantly share code, notes, and snippets.

View michiels's full-sized avatar

Michiel Sikkes michiels

View GitHub Profile
class Organization
has_many :organization_histories
history_methods = [
:coc_number,
:coc_office
]
history_methods.each do |method|
class DashboardMetrics < MetricsDefinition
metric "github stargazers" do |m|
m.reason = "Hallo!"
m.fetch_with do
5
end
end
class Metric
attr_accessor :reason
end
class MetricsLoader
def self.load
file = File.open(Rails.root.join('app','metrics.rb'), 'r')
contents = file.read
@michiels
michiels / deploy.rb
Created August 21, 2013 14:15
Don't allow someone to deploy a feature branch when there's already one active
if ENV['BRANCH']
before "deploy", "deploy:check_branch"
end
after "deploy:finalize_update", "deploy:put_branch"
namespace :deploy do
task :check_branch do
current_branch = capture("cat #{current_path}/BRANCH; true")
@michiels
michiels / find_or_initialize_by.rb
Created January 15, 2013 11:31
So is this the new way of doing find_or_initialize_by?
user = User.where(email: row['email']).first || User.new(email: row['email'])
class OrganizationsController < ApplicationController
include ProductSortableListing
def show
@notification = notification_for_current_user
@categories = Category.top_level
@category = nil
@michiels
michiels / product_sortable_listing.rb
Created December 28, 2012 15:57
Refactored sortable code into concern
module ProductSortableListing
extend ActiveSupport::Concern
private
def sort_products_from_params(products)
if params[:sort] == "product_name"
if params[:direction] == "desc"
products = products.order("products.name desc")
else
def index
@organization = current_user.organization || Organization.new
if params[:q]
page_number = params[:page] || 1
per_page = 50
@products = Product.search(params[:q]).active.includes(:product_prices).where('product_prices.active = 1', true)
if current_user.searches_are_not_logged == false
@michiels
michiels / organizations_controller_before.rb
Created December 28, 2012 15:53
Before code for OrganizationsController
class OrganizationsController < ApplicationController
def show
@notification = notification_for_current_user
@categories = Category.top_level
@category = nil
if params[:category_id]
@michiels
michiels / ffmpeg_wrapper.rb
Created November 14, 2012 14:19
FFMPEG Wrapper
class FFMPEGWrapper
def duration_in_sections
command = IO.popen("ffmpeg -i #{uploaded_file} 2>&1 | awk '/Duration/ { print substr($2,0,length($2)-1) }'")
duration_output = command.read
matches = duration_output.match(/(\d{2}):(\d{2}):(\d{2}\.\d{1,2})\n/)
hours = matches[1].to_f
minutes = matches[2].to_f
seconds = matches[3].to_f
miliseconds = matches[4].to_f