Skip to content

Instantly share code, notes, and snippets.

View wojtekmach's full-sized avatar

Wojtek Mach wojtekmach

View GitHub Profile
select
question_id, cnt, total_cnt, 1.0 * cnt / total_cnt as ratio
from (
select question_id, sum(case when correct='f' then 1 else 0 end) as cnt,count(question_id) as total_cnt from quiz_questions group by question_id
)
order by ratio desc
@wojtekmach
wojtekmach / contain_within_matcher.rb
Created May 25, 2011 22:09 — forked from mattsnyder/contain_within_matcher.rb
Contain Within Matcher for use in your view specs. Faster than css_select and assert_select.
# Must have Nokogiri gem installed and save this file in your spec/support dir
# Allows you to write cleaner/faster specs in your views (50% faster than css_select)
# Increased readability in your spec doc
# ex.
# rendered.should contain('my heading').within('h1') # searches all H1 tags for 'my heading'
# or
# rendered.should contain('my string') # searches entire rendered string for 'my string'
class Within
def initialize(expected, css_selector)
@expected = expected
@wojtekmach
wojtekmach / gist:1137015
Created August 10, 2011 14:58
Ruby one-liners
# factorial
(1..5).inject(&:*) # => 120
# reverse
(1..5).inject([]) { |val, v| [v] + val } # => [5, 4, 3, 2, 1]
# fibonacci numbers
(1...5 - 1).inject([0, 1]) { |val, v| val << val[-2] + val[-1] } # => [0, 1, 1, 2, 3]
@wojtekmach
wojtekmach / post_test.rb
Created August 28, 2011 12:19
minitest + valid_attribute
gem "minitest"
require "minitest/spec"
require "minitest/autorun"
require "active_model"
require "test/unit"
require "valid_attribute"
require "turn"
class MiniTest::Unit::TestCase
extend ValidAttribute::Method
@wojtekmach
wojtekmach / post_test.rb
Created August 28, 2011 12:33
minitest + shoulda-matchers
gem "minitest"
require "minitest/spec"
require "minitest/autorun"
require "active_model"
require "turn"
require "shoulda-matchers"
class MiniTest::Unit::TestCase
include Shoulda::Matchers::ActiveModel
extend Shoulda::Matchers::ActiveModel
@wojtekmach
wojtekmach / post_spec.rb
Created September 4, 2011 12:45
minitest-matchers + valid_attribute
# you have to use https://github.com/wojtekmach/valid_attribute/tree/minitest
require "minitest/autorun"
require "minitest/matchers"
require "active_model"
require "valid_attribute"
class Post
include ActiveModel::Validations
attr_accessor :title
@wojtekmach
wojtekmach / test.rb
Created September 5, 2011 21:36
minitest/spec hooks?
gem "minitest"
require "minitest/spec"
require "minitest/autorun"
class ControllerSpec < MiniTest::Spec
def self.inherited klass
p "id: #{klass.object_id}"
p "name: #{klass.name}"
super
end
@wojtekmach
wojtekmach / application.css.sass
Created October 18, 2011 20:27
bootstrap + simple_form boolean/radio/check_boxes
.clearfix
label
margin-right: 20px
&.boolean, &.check_boxes span, &.radio span
padding-left: 150px
height: 27px
label
float: left
width: auto
@wojtekmach
wojtekmach / gist:1336382
Created November 3, 2011 12:32
Capybara + polymorphic_url
module Capybara::PolymorphicVisit
def visit(*args)
if args.first.is_a? String
super *args
else
super polymorphic_url(*args)
end
end
end
@wojtekmach
wojtekmach / slim.rb
Created January 3, 2012 23:36
Slim + handlebars
module Tilt
class HandlebarsSlimTemplate < ::Slim::Template; end
register HandlebarsSlimTemplate, :handlebars_slim
end
class Slim::EmbeddedEngine
register :handlebars, TagEngine, tag: :script, attributes: { type: "text/x-handlebars" }
register :handlebars_slim, TagEngine, tag: :script, attributes: { type: "text/x-handlebars" }, engine: StaticTiltEngine
end