Skip to content

Instantly share code, notes, and snippets.

node do
{ name: current_user.first_name,
visited_number: @all_places.length,
visited_period: Time.now - current_user.created_at }
end
child PlaceDecorator.decorate_collection(@all_places) => :table do
attributes :id, :name
end
# you have your list of the user's posts,
# these are just in whatever order your DB gets them, which is probably (but not guaranteed) to be ordered by the post id:
@posts = user.posts
@posts.map { |p| [p.id, p.category_id] } # [[1,3], [2,5], [3,3], [4,1]]
# you can specify that you'd like the order explicitly like this,
# here we ask for them ASCending, you have to pick one, but it probably doesn't matter in your case:
@posts = user.posts.order('category_id ASC')
@posts.map { |p| [p.id, p.category_id] } # [[4,1], [3,3], [1,3], [2,5]]
class PlaceEventsController < ApplicationController
before_filter :authenticate_user!
before_filter :weeks_ago_range
respond_to :json, only: [:edit, :update]
[snip edit and index]
def update
place_event = current_user.place_events.find params[:id]
def self.within_distance_from(*args)
return Place.within_distance_from_place args[0], args[1] if args.length == 2
return Place.within_distance_from_point args[0], args[1], args[2] if args.length == 3
raise ArgumentError, 'Call with either a Place or latitude, longitude; and distance'
end
# If you have a filter which is used in a lot of controllers and thus requires constant mocking
# implicitly skip before_filter in all controllers:
# in spec_helper.rb
config.before(:each) do
if defined? controller and not example.options[:with_before_filter] == :my_filter
controller.class.skip_before_filter :my_filter
end
end
@davetapley
davetapley / range_overlaps.rb
Created December 5, 2012 00:58
Extending Range with overlaps?
class Range
def overlaps?(other)
(self.first <= other.last) and (other.first <= self.last)
end
end
# Normal has_many / belongs_to:
>> a = c.permitted_ips
=> []
# What we got back was just an Array:
>> a.class
=> Array
# Now build a new record:
>> c.permitted_ips.build :address => '123123'
@davetapley
davetapley / gist:4033669
Created November 7, 2012 19:05
dig git.tddium.com
; <<>> DiG 9.8.1-P1 <<>> git.tddium.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 51959
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;git.tddium.com. IN A
;; ANSWER SECTION:
@davetapley
davetapley / Mittens.user.js
Created October 31, 2012 22:02
More theraputic democracy
// ==UserScript==
// @name Mittens
// @namespace http://userscripts.org/users/489276
// @include http*
// @version 1
// @grant None
// ==/UserScript==
// Thanks to: http://stackoverflow.com/a/5494405/21115http://stackoverflow.com/a/5494405/21115