Skip to content

Instantly share code, notes, and snippets.

# 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
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
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]
# 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]]
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
@davetapley
davetapley / 304-problem.rb
Last active December 14, 2015 13:18
When hitting the `index` action at `/places.json` I'm always getting a `200` and the expected JSON. But hitting the `map` action at `/places/map.json` always gives `304`.
# config/routes.rb
resources :places, only: [:index, :edit, :update], constraints: {format: :json} do
collection do
get 'map'
end
# app/controllers/places_controller.rb
@davetapley
davetapley / error.xml
Created March 21, 2013 18:18
pgbackups download error
<!-- Running command:
curl -o latest.dump `heroku pgbackups:url --app xxxxx`
At 100% we get: -->
<Error><Code>AccessDenied</Code><Message>Request has expired</Message><RequestId>xxxxxxx</RequestId><Expires>2013-03-21T16:02:26Z</Expires><HostId>xxxxxxx</HostId><ServerTime>2013-03-21T16:50:38Z</ServerTime></Error>
@davetapley
davetapley / lat_lngs.txt
Created March 26, 2013 01:45
Generated with: > r = Random.new; 50.times { puts r.location(34.393, -113.475, 10000).join ',' }
34.442688693689824,-113.53830101755034
34.33576843364106,-113.42973400738283
34.44648524918964,-113.52317003164038
34.41614244624507,-113.42110221380793
34.443154610235155,-113.49498783458732
34.43219600146056,-113.43444706591943
34.36054463655045,-113.53565715125131
34.427160925658754,-113.47398007995909
34.43880963756491,-113.43854457484196
34.37758904544772,-113.51267759234292
exports.routes = function() {
this.get('/', function() {
var users = db.collection('users');
this.res.setHeader("Content-Type", 'text/html');
this.res.writeHead(200, {});
var that = this;
var stream = users.find().forEach(function(err, userDoc) {
if(userDoc) {