Skip to content

Instantly share code, notes, and snippets.

@tehprofessor
tehprofessor / git-changelog.sh
Last active December 19, 2015 19:09
Git-Changelog Installer (only tested on Debian 7.0 and Mac OS X 10.8)
#!/bin/bash
unamestr=`uname`
platform_path='unknown'
if [[ "$unamestr" == "Linux" ]]; then
platform_path='/usr/bin'
if [[ $UID != 0 ]]; then
echo "Please run this script with sudo:"
echo "sudo $0 $*"
exit 1
@tehprofessor
tehprofessor / backgrounding.rb
Last active December 18, 2015 17:18
Parallelism w/ Use Case and Example
# Let's say you have a collection of users you want to email once a week.
# Now using Sidekiq, it's easy to move this off the main thread, so that you're not blocking
# incoming requests to the Rails application.
#
#Chances are you have a worker that looks like the EmailUsersWorker below.
#
class EmailUsersWorker
include Sidekiq::Worker
@tehprofessor
tehprofessor / model.rb
Last active December 18, 2015 12:39
DataMapper won't save/update object if called in #each block I'm sure I'm doing something übern00b here (as I am a DM n00b).
instances = Model.all
# If I try to perform this on all the instances, all return true but the changes are not persisted.
instances.each do |instance|
desc = instance.description
desc.gsub!(/some\swords/, '')
instance.update(:description => desc) # doesn't work
instance.description = desc
instance.save # doesn't work and returns true.
@tehprofessor
tehprofessor / promotion_with_flatuikit.rb
Last active December 18, 2015 01:29
RubyMotion Example: Promotion Screen using Grouper's FlatUIKit w/ navigation bar
class RootScreen < ProMotion::Screen
title "Bienvenue"
def will_appear
UIBarButtonItem.configureFlatButtonsWithColor([89, 91, 91].uicolor, highlightedColor: [64, 65, 65].uicolor, cornerRadius: 2.0)
set_navigation_appearnce!
set_attributes self.view, {
backgroundColor: [222, 226, 225].uicolor
}
@label ||= UILabel.alloc.initWithFrame(CGRectZero)
@label.text = "Application"
@tehprofessor
tehprofessor / home_screen.rb
Last active December 18, 2015 00:18
No method error when trying to use FlatUIKit's configureFlatButtonsWithColor https://github.com/Grouper/FlatUIKit
# Notes:
#
# FlatUIKit: https://github.com/Grouper/FlatUIKit
#
# Their Example:
# [UIBarButtonItem configureFlatButtonsWithColor:[UIColor peterRiverColor]
# highlightedColor:[UIColor belizeHoleColor]
# cornerRadius:3];
#
@tehprofessor
tehprofessor / objc_method.m
Last active December 18, 2015 00:18
Calling an objective c method in Ruby Motion
[UIBarButtonItem configureFlatButtonsWithColor:[UIColor peterRiverColor]
highlightedColor:[UIColor belizeHoleColor]
cornerRadius:3];
@tehprofessor
tehprofessor / model.rb
Created May 9, 2013 03:46
How to determine if an instance was created (e.g. not previously persisted) after it has been saved. Very much use case specific.
class Model < ActiveRecord::Base
attr_accessor :was_new_record
def was_new_record?
@was_new_record == true ? true : false
end
end
@tehprofessor
tehprofessor / watir_phantomjs_user_agent_settings.rb
Created May 1, 2013 18:28
Configuring Watir to use a custom user agent with the PhantomJS driver. The user agent gem has been deprecated, and this functionality is not documented anywhere (obvious).
require 'watir-webdriver'
capabilities = Selenium::WebDriver::Remote::Capabilities.phantomjs("phantomjs.page.settings.userAgent" => "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36")
driver = Selenium::WebDriver.for :phantomjs, :desired_capabilities => capabilities
browser = ::Watir::Browser.new driver
# Quick test to make sure it's set
browser.goto 'http://www.useragentstring.com/'
browser.textarea(:id => "uas_textfeld").value
@tehprofessor
tehprofessor / Image_dimensions_validation_with_carrierwave_vips.rb
Last active December 15, 2015 19:59
Image dimensions validation with carrierwave-vips
require 'vips'
class Photo < ActiveRecord::Base
# Include the VIPS moduel in model so we have access to the `Image` class
include VIPS
# Basic attributes
attr_accessible :image, :image_cache, :crop_x, :crop_y, :crop_w, :crop_h
# mount Carrierwave
mount_uploader :image, PhotoUploader
# Call the minimum_dimensions method on validation
validate :minimum_dimensions
@tehprofessor
tehprofessor / object_id.rb
Created June 15, 2012 02:42
bson-1.6.4/lib/bson/types/object_id.rb
# encoding: UTF-8
# --
# Copyright (C) 2008-2011 10gen Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0