Skip to content

Instantly share code, notes, and snippets.

View denniskuczynski's full-sized avatar

Dennis Kuczynski denniskuczynski

View GitHub Profile
@denniskuczynski
denniskuczynski / spec_helper.rb
Created June 22, 2012 21:42
spec_helper for changing configuration based on RSpec example options
config.before(:each) do
with_transaction_callbacks = example.options[:with_transaction_callbacks]
if with_transaction_callbacks
DatabaseCleaner.strategy = :truncation
else
DatabaseCleaner.strategy = :transaction
end
DatabaseCleaner.start
end
@denniskuczynski
denniskuczynski / monit.cfg.erb
Created June 21, 2012 20:12
Example Monit Configuration ERB file to leverage Ruby for Config
<% PWD = ENV["PWD"] %>
<% PID_DIR = '/usr/local/var/run' %>
<%
ADDITIONAL = ''
if ENV['RVM']
ADDITIONAL = "export PATH=#{ENV['PATH']} && export GEM_HOME=#{ENV['GEM_HOME']} && export GEM_PATH=#{ENV['GEM_PATH']} && export HOME=#{ENV['HOME']} && "
end
%>
#
@denniskuczynski
denniskuczynski / generate.rake
Created June 21, 2012 20:02
Example Rake task to process ERB version of Monit Configuration File to leverage Ruby in the Config
namespace :monit do
desc 'Generate the monit configuration'
task :generate do
require 'erb'
PWD = ENV['PWD']
CONFIG_TEMPLATE = File.join(PWD, 'config', 'monit.cfg.erb')
templ = ERB.new(File.read(CONFIG_TEMPLATE))
CONFIG = File.join(PWD, 'monit.cfg')
File.open(CONFIG, 'w') {|f| f.write(templ.result) }
end
@denniskuczynski
denniskuczynski / common_data.rb
Created May 11, 2012 13:51
Sharing common attributes and validations in Rails ActiveRecord to stay DRY
# Sharing common attributes and validations in Rails ActiveRecord to stay DRY
module CommonData
def self.included(clazz)
clazz.class_eval do
validates :duplicate_field, presence: true, length: { maximum: 255 }
attr_accessible :duplicate_field
end
end
@denniskuczynski
denniskuczynski / update_model_in_place.rb
Created May 9, 2012 15:14
Example of updating a Rails Model attribute in place
it "tries to update a Model's string attribute in place" do
person = Person.new
person.name = ""
person.save!
person.reload
person.name = ""
person.name << "TEST"
person.save!
person.reload
@denniskuczynski
denniskuczynski / routes.rb
Created April 26, 2012 13:31
mount beanstalkd_view
mount BeanstalkdView::Server, :at => "/beanstalkd"
@denniskuczynski
denniskuczynski / beanstalkd_configuration
Created April 26, 2012 13:29
beanstalkd_configuration
# Single instance running locally
ENV['BEANSTALK_URL'] = 'beanstalk://localhost/'
# Two instances running locally on different ports
ENV['BEANSTALK_URL'] = 'beanstalk://localhost/,beanstalk://localhost:12400/'
class Example.Routers.TestRouter extends Backbone.Router
routes:
"index" : "index"
"example/:example_num" : "example"
".*" : "index"
@denniskuczynski
denniskuczynski / test_router_spec.coffee
Created April 3, 2012 20:00
An example of Backbone Route Testing
describe 'Example.Routers.TestRouter:', ->
beforeEach ->
@router = new Example.Routers.TestRouter()
it "check index route", ->
url_fragment = '#'
method = RouteHelpers.getRouteHandlerMethod(@router, url_fragment)
expect(@router.routes[method]).toEqual('index')
it "check example/1 route", ->
@denniskuczynski
denniskuczynski / spec_helper.coffee
Created April 3, 2012 19:58
A Helper for testing Backbone route matching
window.RouteHelpers = {
getRouteHandlerMethod: (router, url_fragment) ->
routeStripper = /^[#\/]/;
url_fragment = url_fragment.replace(routeStripper, '')
routes = Object.keys(router.routes)
return _.find routes, (route) =>
regexp = router._routeToRegExp(route)
if (regexp.test(url_fragment))
return true
else