Skip to content

Instantly share code, notes, and snippets.

Avatar

Adrian Duyzer adriand

View GitHub Profile
View gist:9353194
Ruby/Rails
- Use latest stack (Ruby 2.1.x, Rails 4.x, Postgresql)
- Prefer the new hash syntax, but use the old one when necessary (ie. when you need to use strings as keys)
- Code in ActiveRecord models should pertain directly to storing or retrieving data
- Use Draper to decorate model instances and test your decorators with RSpec (app/decorators is the place for this)
- Put business logic into service classes and test them with RSpec (app/services is a fine place for this, you can get more specific if your services folder gets fat)
- I like to name my service classes after what they do, or give them cute names occasionally (AddsProductstoCart or CartStuffer)
- Don’t hesitate to namespace related models or services into modules.
- Use Resque and Redis for background job handling.
View gist:10890369
Ruby/Rails
- Use latest stack (Ruby 2.1.x, Rails 4.x, Postgresql)
- Prefer the new hash syntax, but use the old one when necessary (ie. when you need to use strings as keys)
- Code in ActiveRecord models should pertain directly to storing or retrieving data
- Use Draper to decorate model instances and test your decorators with RSpec (app/decorators is the place for this)
- Put business logic into service classes and test them with RSpec (app/services is a fine place for this, you can get more specific if your services folder gets fat)
- I like to name my service classes after what they do, or give them cute names occasionally (AddsProductstoCart or CartStuffer)
- Don’t hesitate to namespace related models or services into modules.
- Use Resque and Redis for background job handling.
- Testing should be the rule, not the exception
View tenon_faq.md

Q: I changed a SASS variable but the change is not reflected in Tenon. I even restarted my app and there's no change!

A: Delete the entire tmp/cache folder and try again

Q: How do I install the config file, main menu nav, and custom colours file into my app?

A: bundle exec rails generate tenon:install

Q: I got the error "Could not infer a decorator for MyModel" but the decorator is there!

View broken_hpricot.rb
#!/usr/bin/ruby
require 'rubygems'
require 'open-uri'
require 'hpricot'
require 'ruby-debug'
require 'cgi'
f = open("http://forum.cakewalk.com/tm.aspx?m=1859288#1859354")
doc = Hpricot(f)
View split_up.rb
require 'rubygems'
require 'ruby-debug'
def media_type_params(content_type)
debugger
return {} if content_type.nil?
content_type = content_type.split(/\s*[;,]\s*/)[1..-1]
content_type = content_type.collect { |s| s.split('=', 2) }
content_type = content_type.inject({}) { |hash,(k,v)| hash[k.downcase] = v ; hash }
content_type
View line_9.rb
inject({}) { |hash,(k,v)| hash[k.downcase] = v ; hash }
View a_bit_of_rack.rb
# The media type parameters provided in CONTENT_TYPE as a Hash, or
# an empty Hash if no CONTENT_TYPE or media-type parameters were
# provided. e.g., when the CONTENT_TYPE is "text/plain;charset=utf-8",
# this method responds with the following Hash:
# { 'charset' => 'utf-8' }
def media_type_params
return {} if content_type.nil?
content_type.split(/\s*[;,]\s*/)[1..-1].
collect { |s| s.split('=', 2) }.
inject({}) { |hash,(k,v)| hash[k.downcase] = v ; hash }
View past_interval.rb
# given hash like { :weeks => 1 } or { :months => -2 }, always return hash with a negative value
def past(interval)
k,v = interval.to_a[0]
interval[k] = v.abs * -1
interval
end
View shorter_past.rb
def past(interval)
interval.inject({}) { |interval,(k,v)| interval[k] = v.abs * -1; interval }
end
View bottles_base.rb
# When we create our Rackup file, we'll already be requiring RubyGems and Sinatra,
# so we don't require them again if they've already been loaded.
require 'rubygems' unless defined? ::RubyGems
require 'sinatra' unless defined? ::Sinatra
require 'rack' # more on the decision to include this below
require 'dm-core'
require 'haml'
require 'ruby-debug'
# If you want changes to your application to appear in development mode without having to
You can’t perform that action at this time.