Skip to content

Instantly share code, notes, and snippets.

@ljkbennett
ljkbennett / gist:5495596
Last active December 16, 2015 20:49 — forked from durran/gist:315227
module MultiParameterAttributes
def filter_time(attributes, name)
attrs = attributes.collect do |key, value|
if key =~ /^#{Regexp.escape(name.to_s)}\((\d+)(\w)\)$/
[$1.to_i, value.send("to_#$2")]
end
end.compact.sort_by(&:first).map(&:last)
attributes.reject! {|key,value| key =~ /^#{Regexp.escape(name.to_s)}\((\d+)(\w)\)$/ }
attributes[name] = Time.zone.local(*attrs) unless attrs.empty?
@ljkbennett
ljkbennett / 1. not_working
Last active December 19, 2015 16:28
Mongoid route testing problems
require 'spec_helper'
describe "artifact routes" do
it "routes post /artifacts to artifacts#create" do
{post: "/artifacts"}.should route_to(controller: "artifacts", action: "create")
end
it "routes delete /artifacts/:id to artifacts#create" do
artifact = FactoryGirl.build(:artifact)
{delete: "/artifacts/#{artifact._id}"}.should route_to(controller: "artifacts", action: "destroy", id: artifact._id)
@ljkbennett
ljkbennett / dropdown.html
Last active December 20, 2015 16:49
Coffeescript for a dropdown that can contain checkboxes and not close when one is clicked, allowing multiple checks. The button text is updated to reflect the values selected.
<div class="btn-group">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
Please select
</a>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu">
<li><input type="checkbox" name="option1" id="option1" value="option1"/><label for="option1">Option 1</label</li>
<li><input type="checkbox" name="option2" id="option2" value="option2"/><label for="option2">Option 2</label</li>
<li><input type="checkbox" name="option3" id="option3" value="option3"/><label for="option3">Option 3</label</li>
</ul>
</div>
@ljkbennett
ljkbennett / Blog post sharing hell
Created September 5, 2013 09:37
Recorded for posterity
scope :for_region, lambda { |region|
joins("LEFT OUTER JOIN blog_post_shared_regions ON blog_post_shared_regions.blog_post_id = blog_posts.id")
.where("blog_posts.region_id = ? OR (blog_post_shared_regions.region_id = ? AND blog_post_shared_regions.visible = true)", region.id, region.id)
.order( "CASE WHEN blog_post_shared_regions.shared_at IS NULL THEN blog_posts.updated_at WHEN blog_post_shared_regions.shared_at > blog_posts.updated_at THEN blog_post_shared_regions.shared_at ELSE blog_posts.updated_at END DESC")
}